Skip to content

Awesome Kotlin tricks and tips!

Kotlin has taken 33% of google play store apps worldwide on last month. When devs started developing in Kotlin, It was like they were experiencing a new world of freedom to develop and to express things more beautifully and more efficiently. Kotlin comes with some cool ideas and concepts that we haven’t used in Java and also some of the existing boilerplates in Java were removed in Kotlin. This post will help you to go through some of the cool and useful concepts of Kotlin.

Let’s get into code…

In Java we will be using a bunch of setter function to the object of builder pattern. This will look like kind of weird thing when we want to set more than 10 properties. In Kotlin we can simply use the with/ apply concepts. Here is some of the methods used for initiating the class with setters in Kotlin.

//apply function for applying  params to a class

  Intent(this, MyActivity::class.java).apply {
    putExtra("extra1", "extra1")
    putExtra("extra2", "extra2")
    startActivity(this)
  }

//run will run the applied object on appropirate thread

  Intent(this, MyActivity::class.java).apply {
    putExtra("extra1", "extra1")
    putExtra("extra2", "extra2")
  }.run {
    startActivity(this)
  }


//More simpler!

  startActivity(Intent(this, MyActivity::class.java).apply {
    putExtra("extra1", "extra1")
  })

 

with() will allow you modify function parameter instances.Basically, like in the example.

//java method

  public void updateButton(Button button){

    button.setText("Hello world");
    button.setTextColor(blue);
    button.setOnClickListner();
  }

//Kotlin 

  fun updateButton(button: Button) {
    with (button) {
      textColor = blue
      text = "Hello world"
      setOnClickListener {
        
      }
    }
  }

 

Adding arraylist to existing one is made so easy in Kotlin. You can use + operator for this operation instead of calling methods in ArrayList class.

//java method

  carList.add(jaguarCarList);
  carList.addAll(lambCarsList);
  carList.addAll(hyundaiCarsList);
  carList.add(beaconCarList);

//Kotlin 

  carList=listOf (jaguarCarList+lambCarsList+hyundaiCarsList+beaconCarList);

Defining collections is easier than Java in Kotlin. Simply,

val map = mapOf(

    "A" to "A-value",
    "B" to "B-value",
    "C" to "C-value"
  )

Filtering of collections are also made easier in Kotlin. A simple example is given below.

val users = api.getUsers()

paidUsers=users.filter{
  it.paid //it refers to current single object in users list.
}

Kotlin will not support static keyword of Java. In a sense, It doesn’t have such a concept. Btw, Companion object of Kotlin will resolve the problem.Those are singleton objects of the current class which will work as a static fashion.

class MyClass {

   companion object {
       const val DEFAULT_VALUE = 20
   }
 }

 // later access
 myClass.age = MyClass.DEFAULT_VALUE

Default param value is a cool concept of Kotlin. You might have did this in Python language. But Java does not support this cool features yet. This concept will allow you to pass the parameter value if it is not null and can avoid if it is null.

fun addValues(value1:int=1,value2:int=1){

  println(value1+value2)
}

addValues(2,3) //prints 5
addValues() //prints 2 using default values in the method

This post will be updated with more tricks and tips soon.
Happy coding!

Published inAndroidKotlin

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *