Basic of Kotlin to get started in Android Part-5

Sharing is caring!

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
Basic of Kotlin to get started in Android Part-5
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Scroll to top
0
Would love your thoughts, please comment.x
()
x