Tuesday, April 26, 2016

Android Kotlin with Anko

I recently found an interest in Android, this time with Kotlin and Anko. Kotlin is an alternative language to build Android App and it's a more modern language therefore it has its own advantage and possibly cleaner way to program then Java. Jetbrains made an amazing job with IntelliJ in making sure that you can create Android Apps. Having said that, here is a tutorial on how to start your first Kotlin Android App. Getting started with Android and Kotlin.

Incase you encounter an error, open your app's build.gradle and replace the contents with the generated content from kotlin build.gradle generator. Note that this has anko in its dependencies.

So what is Anko? From its github page Anko is a library which makes Android application development faster and easier. It makes your code clean and easy to read, and lets you forget about rough edges of Android SDK for Java. It's mostly known for its DSL for making UI but there are few view related extensions that are quite helpful in developing new apps.

Let's see how Kotlin and Anko works. Here is an example of a sharing fragment class
class ShareFragment: Fragment() {
    companion object {
        val FACEBOOK_URL = "https://m.facebook.com/pages/somePage"
    }

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
 savedInstanceState: Bundle?): View? {
        return UI {
            linearLayout {
                orientation = LinearLayout.VERTICAL
                gravity = Gravity.CENTER_HORIZONTAL

                textView("Share to FB")

                imageButton {
                    imageResource = R.drawable.facebook
                    onClick {
                        owner.gotoUrl(FACEBOOK_URL)
                    }
                }

                textView("Sharing to other network will appear later")
            }
        }.view
    }
}

Just reading from the UI part you would know that this is a linearLayout with gravity horizontally centered and its children is stacked vertically. The first child is textView with text Share to FB, below it is an imageButton with resource drawable named facebook. Then below it is also a textView with the text Sharing to other network.

owner.gotoUrl(FACEBOOK_URL);
You might be wondering what is this, this is called extensions. In kotlin, you can extend base class with your custom functions. With this specific code the owner is a Fragment. So i would have a code like
fun Fragment.gotoUrl(url: String) {
    val intent: Intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
    startActivity(intent)
}

The advantage of doing this is that if you decided to put a share button in another fragment you wont have to duplicate the code. Kotlin advices to use this approach then using an utility based approach. And for me, this way is a lot cleaner.

You could put the Fragment.gotoUrl at the end of the class or a more organised approach is to create an class with all your view related extensions, i called mine ViewExtensions.kt. Here is a gist.

I have just started to learn kotlin and I found it to be interesting. Hope this helps :)
Post a Comment