Kotlin - Android

Basic of Kotlin to get started in Android Part-5

Kotlin is an awesome language which is implemented by JetBrains. We have practiced many features of Kotlin to build the awesome android application quickly in our last post. If you want to learn how to quick start to using Kotlin language for building Android application.

So do not worry I am here to help you. I would be recommended to checking all my post of Basic of Kotlin to get started in Android part -1, Part-2, part-3, part-4.

In this series today we will learn new hot feature Extensions which is offered by Kotlin. It avoids much boilerplate code which exists in Java. Extensions do not actually insert members into classes, there’s no efficient way for an extension property to have a backing field. This is why initializers are not allowed for extension properties.

Their behavior can only be defined by explicitly providing getters/setters. To get more detail of extensions please check the official documentation of Koltin.

How can We define extensions property in Koltin? Let’s see few example to understand.

 override fun showProgress(show: Boolean) {
        if (!show){
            progressbar!!.visibility = View.GONE
        }else{
            progressbar!!.visibility = View.VISIBLE
       }
    }

In above code, we used extensions feature to set the visibility of progress bar. Generally, we used setter method to set the visibility on Progressbar. But extensions features avoid boilerplate code.

Let’s see few more examples of using.

fun showToast(message : String){
        Toast.makeText(activity, message, Toast.LENGTH_LONG).show()
    }

and create the new fragment to commit to an activity.

 val friendsDetailFragment = FriendsDetailFragment().newInstance()
        val bundle = Bundle()
        bundle.putString("UserId", userId)
        friendsDetailFragment.arguments = bundle
        ActivityUtil().addFragmentToActivity(fragmentManager, friendsDetailFragment, R.id.frame, "FriendsDetailFragment")

Let’s see how we can use extensions feature in Adapter class to set the data model value on Textview.

override fun onBindViewHolder(holder: FriendViewHolder?, position: Int) {
        var friends = friendsList[position]
        holder!!.name!!.text = friends.name
        holder.email!!.text = friends.email
      
    }

Ok Great. I loved this extension property feature of Kotlin. I hoping that you also start using this Extensions property feature in the application. Please check another awesome post of Kotlin. Here are details of post API call by using Retrofit, RxJava and RxAndroid in Kotlin Android, Integration of Anko SQLite database in Kotlin Android and MVP design pattern with Dagger2, Retrofit, Anko and RxJava in Kotlin Android.

Please do subscribe your email to get the updated newsletters from this blog and if you feel that this post will help you to understand then do not forget to share and comment below

Happy Coding 🙂

0 0 votes
Article Rating

Recent Posts

Hide your production API key or any sensitive data in Android

Hi everyone, In this article, we are going to learn how to hide the production… Read More

2 years ago

How to handle the localisation or multi language support in android with examples?

Hello everyone, Today in this article, we are going to learn about localisation to support… Read More

2 years ago

How to convert any callback to Coroutines and use them in Kotlin Android?

Hello everyone, In this article, we are going to learn something to handle the callback… Read More

2 years ago

Request Permission Launcher with Kotlin in Android

In this article, we are learning about the run time permissions for request permission launchers.… Read More

2 years ago

Implement the SMS User Consent API and SMS Retriever API in Android

Hello everyone. In my last tutorial, we learned about the Jetpack Compose introduction and about applying the… Read More

3 years ago

Jetpack Compose Coroutine flow with LiveData/ViewModel in Android

Hello everyone, In this article, we are going to learn about the Jetpack Compose with… Read More

3 years ago