I might be way too late when it comes to starting app development with Kotlin, but i thought it would still be helpful to anyone who tries it for the first time. I will try to put in as much as I can about Kotlin and probable things or functions that are different, with of course the presence of several resources on Kotlin on the web. This blog would probably focus on how to create a kotlin project covering some of the things I came across

What has changed? Probably a lot 🙂 

  1. A file in Kotlin could either be a Kotlin Class or a Kotlin Interface
  2. In order to declare a variable you could use a “var” or “val” keyword. var would mean that the value of the variable can be modified, while val would behave just as though it were a final variable or constant in java. It’s own value cannot be changed, though you can change the value of its members.

You can create a Kotlin project via android studio, and the same applies for all the classes or interfaces needed.

Let’s dive into the first demo:
1. Looking at the extension syntax: In the normal java class, an activity would look as under (I have highlighted the changes in bold)

class MainActivity extends AppCompatActivity {
    @Override
    public void onCreated(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         
         MainView view = new MainViewImpl(this);
         MainInteractor interactor = new MainInteractorImpl();
         MainPresenter presenter = new MainPresenterImpl(view, interactor);
         presenter.handleOnViewCreated();
    }
}

while in Kotlin, it would look as under:

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

        val view = MainViewImpl(this)
        val interactor = MainInteractorImpl()
        val presenter = MainPresenterImpl(view, interactor)
        presenter.handleOnViewCreated()
    }
}

Since Kotlin handles null pointer checks, it uses a “?” to denote that the parameter could be nullable (Looking at savedInstanceState : Bundle? )

val view = MainViewImpl(this) can also be written as val view : MainView = MainViewImpl(this), where in you explicitly specify the type of the variable view (however, this is not required)

2. Looking at the implementation syntax: In a normal java class we would have it as under:

interface MainPresenter {
    void handleOnViewCreated();
}

While, looking at Kotlin, we would have:

interface MainPresenter {
    fun handleOnViewCreated()
}

every function in Kotlin would be defined by the keyword fun and we do not use semicolons anymore.

Simarly, the syntax of implementing an interface in Java, would be as:

class MainPresenterImpl implements MainPresenter {
    @Override public void handleOnViewCreated() { }
}

While, in Kotlin, it would be as:

class MainPresenterImpl : MainPresenter {
    override fun handleOnViewCreated() { }
}

3. Looking at constructors: In java we would explicitly declare a constructor, while in Kotlin, we would use the following syntax if the class has one primary constructor

class MainPresenterImpl constructor(private val view: MainView,
                        private val interactor: MainInteractor) : MainPresenter {
}

the constructor keyword can be omitted if the primary constructor does not have any annotations or visibility modifiers.

4. Looking at findViewById: In Android, we have to explicity use a findViewById in order to create an instance of a view and use the properties of the view, the same is not so with Kotlin if you use kotlin-android-extensions in the app’s build.gradle file

apply plugin: 'kotlin-android-extensions'

This, allows us to access the views directly based on the ids defined in the xml file, for instance, if we have :

<TextView
    android:id="@+id/txtUpdateText"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:gravity="center"
    android:padding="10dp"
    android:textSize="16sp"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="Hello world!"/>

in Kotlin, the following would then be imported automatically “import kotlinx.android.synthetic.main.activity_main.*” and hence, we can simply say txtUpdateText.text = “Hi There!”, HOW, WHY?

Because of the extension plugin, all the views are stored in some cache, thus when we write such a statement Kotlin internally converts it to : 

((TextView)this._$_findCachedViewById(id.txtUpdateText)).setText((CharSequence)"Hi There!");

You can however, use the other way to access the views, if you do not wish to do so directly using the extensions:

 txtUpdateText = findViewById(R.id.txtUpdateText) as TextView

where txtUpdateText can be declared as:

private var txtUpdateText: TextView? = null

5. Looking at the onClickListener (there are many other listeners you can look at, this blog only picks the onClickListener). In Kotlin, we declare a listener as under:

activity.btn_click_me.setOnClickListener {
    listener.onClick()
}

Of course, there are many other things that can be done, and experimented with in Kotlin, but i have just covered some tiny miny things, which might just be the tip of an ice-berg. You can view the source code here.

Have fun, and comments are welcome! 🙂

References:

  1. https://antonioleiva.com/kotlin-android-extensions/
  2. https://kotlinlang.org/docs/reference/classes.html
  3. https://kotlinlang.org/docs/tutorials/android-plugin.html
  4. https://developer.android.com/kotlin/get-started.html
Advertisements