Kotlin logo

Kotlin

A concise multiplatform language developed by JetBrains

Android Multiplatform

Anko 0.6 is Released

Today we are glad to present the new version of Anko — a library which facilitates Android application development. We are happy to have received lots of feedback, and some of the changes in 0.6 were actually proposed by the community.

WARNING: Package Name Changed

Well, we are sorry. For historical reasons, Anko package name used to be kotlinx.android.anko, and we changed it to org.jetbrains.anko in 0.6, to be consistent with the Maven artifact name.

New Listeners

Anko 0.5 introduced partially defined listeners that reduce code verbosity: when we only need to define one method of a multi-methods listener, we do not have to implement the methods we do not care about. Based on your feedback (thanks, SalomonBrys!) this feature has been redesigned in 0.6:

  • Partially defined listeners can be now used outside DSL layouts as well as inside;
  • Syntax is easier to understand;
  • The logic “under the hood” is simpler.

This is what it looks like now:

editText {
    textChangedListener {
        onTextChanged { text, start, before, count ->
            toast("New text: $text")
        }
    }
}

Configuration Qualifiers

Qualifiers are used to support different layouts for different devices, locales etc.

Anko’s DSL now supports the configuration() function that specifies qualifiers the layout is meant for:

configuration(screenSize = ScreenSize.LARGE, orientation = Orientation.LANDSCAPE) {
    /* 
      This code will be only executed
      if the screen is large and its orientation is landscape
    */
}

This code is equivalent to having your XML layout under the layout-large-land directory. Technically, it is implemented through checking the specified qualifiers and only executing the code inside the configuration() if their values match. Therefore, usages of configuration() are not limited to DSL only: for example, you can safely call Android SDK functions which are not present in older versions of system using configuration(fromSdk = <version>) { /* code */ }.

The full list of supported qualifiers is available here.

Custom View Creation

The neatest way of incorporating your own custom views into the DSL is to create your own builder-like functions, but since it is time-consuming, Anko now supports a quicker way:

frameLayout {
    customView<CustomView> {
        backgroundResource = R.drawable.custom_view_bg
    }.linearLayout(width = matchParent)
}

It is implemented via Java Reflection. Though it is slower than the normal DSL functions, it is much easier when you are prototyping.

appcompat.v7 Views and Properties

We have made an initial step to support appcompat.v7 Android library. Extension functions for View classes in the support package and extension properties for its attributes are added to Anko. Widget tinting is not supported yet, we are hoping to implement it in later versions.

Top-level DSL Functions for Simple Views are Removed

Since it is very unlikely to have a simple non-container view (such as TextView) as the content view of your activities, we removed DSL functions for such views for the Activity and Fragment receivers. In the unlikely case of needing such a view on the top level, use UI() wrapper function:

UI {
    textView(R.string.name)
}

Your Feedback is Welcome

Anko is licensed under Apache Licence 2.0, and the project is available on Github.
Your feedback and pull requests are welcome!

image description