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 🙂
I am a very enthusiastic Android developer to build solid Android apps. I have a keen interest in developing for Android and have published apps to the Google Play Store. I always open to learning new technologies. For any help drop us a line anytime at contact@mobologicplus.com
Hi everyone, In this article, we are going to learn how to hide the production… Read More
Hello everyone, Today in this article, we are going to learn about localisation to support… Read More
Hello everyone, In this article, we are going to learn something to handle the callback… Read More
In this article, we are learning about the run time permissions for request permission launchers.… Read More
Hello everyone. In my last tutorial, we learned about the Jetpack Compose introduction and about applying the… Read More
Hello everyone, In this article, we are going to learn about the Jetpack Compose with… Read More