5 Kotlin features for Android development you need.

5 Kotlin features for Android development you need.

Before talking about the characteristics of Kotlin, to remember a bit, Kotlin is a new programming language developed by JetBrains, like Java, Kotlin is a general-purpose programming language.

Let’s go with the Kotlin Features for Android development

1. Import of static design

One of the most repeated codes in the development of Android applications with Java is to use the function findViewById(), a function that allows us to obtain references to the views located in the Activities.

Kotlin allows us to import all references to the views from the design with a single import, to obtain these references we are going to use an extension of Anko for Kotlin.

Suppose we have a Layout resource with a TextView with id “message”, to make the reference to all views including the indicated TextView, in our code we write the following import declaration:

import kotlinx.android.synthetic.main.activity_main. *

With this we have the reference to all views and we avoid using the function findViewById() for each view, our code would like this:

import kotlinx.android.synthetic.main.activity_main. *

import android.support.v7.app.AppCompatActivity
import android.os.Bundle

import kotlinx.android.synthetic.main.activity_main. *

class MainActivity: AppCompatActivity () {

    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate (savedInstanceState)
        setContentView (R.layout.activity_main)

        variable.text = "Hello Mundo!"

Topic: 5 Kotlin features for Android development you need.

2. Write POJO classes with Kotlin

Something that saves us a lot Kotlin is the ability to define our POJO (Plain Old Java Object) classes used to store data. For example, consider the following class in Java:

public class User {
   private String firstName;
   private String lastName;
   private String lastName ; 

   public String getFirstName () { 
public String getFirstName () {  
       return firstName;return firstName ;

   public void setFirstName (String firstName) { public void setFirstName ( String firstName ) {  
       this.firstName = firstName;
this . firstName = firstName ;

   public String getLastName () {
public String getLastName () {  
       return lastName;
return lastName ;

   public void setLastName (String lastName) { public void setLastName ( String lastName ) {  
       this.lastName = lastName;
this . lastName = lastName ;

In Kotlin it would be as follows:

class User { User { 
   var firstName: String? = null
var firstName : String ? = null   

   var lastName: String? = null
var lastName : String ? = null   

We can even realize that the public instruction is not necessary before the class, since by default everything is public, we see a great saving of typing.

Topic: 5 Kotlin features for Android development you need.

3. Heritage of classes and constructors

Kotlin has a more concise syntax when it comes to constructors.

Builders in Kotlin

Classes in Kotlin have a primary constructor and one or more secondary constructors, an example of a primary constructor is:

class User constructor (firstName: String, lastName: String) { 
User constructor ( firstName : String , lastName : String ) {   


The primary constructor goes after the name of the class in the class definition. If the main constructor does not have any annotation or visibility modifiers, the constructor keyword can be omitted:

class Person (firstName: String) { Person ( firstName : String ) {  

Note that a primary constructor cannot have any code; any initialization must be done in the init block of the code:

class Person (firstName: String) { Person ( firstName : String ) {  
    init {{
         // perform primary constructor initialization here// perform primary constructor initialization here

Note that each secondary constructor must delegate to a primary constructor. This is similar to Java, which uses the keyword this:

class User (val firstName: String, val lastName: String) { User ( val firstName : String , val lastName : String ) {   
    constructor (firstName: String): this (firstName, "") {( firstName : String ) : this ( firstName , "" ) {     
       // ...
// ...

When creating instances of a class, keep in mind that Kotlin does not have the reserved word new, just like Java. To instantiate the User class, do it this way:

val user = User ("John", "Doe) = User ( "John" , "Doe)

Heritage in Kotlin

In Kotlin, all classes extend from Any, which is similar to Object in Java. By default, classes are closed, as final classes in Java. Then, to extend a class, it must be declared as open or abstract:

open class User (val firstName, val lastName)
class User ( val firstName , val lastName ) 
class Administrator (val firstName, val lastName): User (firstName, lastName)
class Administrator ( val firstName , val lastName ) : User ( firstName , lastName )

Note that you must delegate to the default constructor of the extended class, which is similar to the super() method for calling in the constructor of a new class in Java.

For more reference on the classes, check the official documentation of Kotlin

Topic: 5 Kotlin features for Android development you need.

4. The with() function

The with () function is useful and comes with the standard Kotlin library. It can be used to save some types if you need to access many properties of an object. For example:

with (message) {( message ) { 
    text = "Hello World!"= "Hello World !!!" 
    visibility = View.VISIBLE= View . VISIBLE 

In the previous example, we can see a code reduction to assign values to the TextView message that we used in the first example.

Topic: 5 Kotlin features for Android development you need.

5. Assigning an object to a map

A commonly used case is to store values of properties within a map. This often occurs in applications that work with RESTful APIs and analyze JSON objects. In this case, a map instance can be used as a delegate for a delegated property, then an example from the official documentation:

class User (val map: Map <String, Any?>) { User ( val map : Map < String ,   Any ?>) { 
    val name: String by map:  String by map 
    val age: Int by map:  Int by map     

In this example, User has a primary constructor that takes a map. The two properties will take the values of the map that are mapped under keys that are equal to the property names:

val user = User (mapOf (= User  ( mapOf (
    "name" to "John Doe","yam" to "John Doe" ,
    "age" to 25"age"   to 25

The name property of the new user instance will be assigned the value of “John Doe” and the property age the value 25.

This works for var properties in combination with MutableMap as well:

class MutableUser (val map: MutableMap <String, Any?>) { MutableUser ( val map : MutableMap < String , Any   ?>) { 
    var name: String by map 
var name : String   by map
    var age: Int by mapvar age : Int       by map

Topic: 5 Kotlin features for Android development you need.


We have just seen 5 Kotlin features to make our Android application development more efficient, there are other issues to consider such as Lambda expressions, null security, delegated properties and collections, to achieve a great improvement.

In summary, Kotlin offers you the possibility of saving time when writing native Android applications through an intuitive and concise syntax. It is still a young programming language, but in my opinion, it is now stable enough to be used in the creation of production applications.

The benefits (advantages) of using Kotlin:

  • Android Studio support is perfect and excellent.
  • It is easy to convert an existing Java project into Kotlin.
  • The Java and Kotlin code can coexist in the same project.
  • There is no speed overload in the application.

Disadvantages of kotlin programming language:

  • Kotlin will add its libraries when generating the .apk, so it can weigh a little more than generating the .apk with Java.
  • If the operator overload is abused, an illegible code can be reached.
  • The autocomplete of the IDE behaves a little slower when working with Kotlin than with pure Android projects with Java.
  • Compilation times can be a little longer.

Topic: 5 Kotlin features for Android development you need.

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!