For the More Curious: Android KTX and Anko Libraries

There are many open-source libraries designed to enhance the developer experience when working with Kotlin and Android. We will highlight two here to give an idea of what is possible.

The Android KTX project (github.com/​android/​android-ktx) provides a number of useful Kotlin extensions for Android app development, often also granting a more Kotlinesque interface to the Android Java APIs than would otherwise be possible. For example, consider the following code, which uses Android’s shared preferences to persist a small amount of data for later use:

    sharedPrefs.edit()
           .putBoolean(true, USER_SIGNED_IN)
           .putString("Josh", USER_CALLSIGN)
           .apply()

With Android KTX, you can shorten the expression and write it in a more idiomatic Kotlin style:

    sharedPrefs.edit {
        putBoolean(true, USER_SIGNED_IN)
        putString("Josh", USER_CALLSIGN)
    }

Android KTX enables many nice, if small, improvements to your Kotlin Android code, and it allows you to work with the Android framework in a style that is a closer match to Kotlin, rather than Java.

Another popular Kotlin project for use with Android, Anko (github.com/​Kotlin/​anko), provides a variety of enhancements for Kotlin Android development, including a DSL for defining Android UIs and a number of helpers for working with Android intents and dialogs, SQLite, and many other aspects of an Android project. For example, the following Anko layout code programmatically defines a vertically oriented linear layout containing a button that displays a toast (a pop-up message) when clicked:

    verticalLayout {
        val username = editText()
        button("Greetings") {
            onClick { toast("Hello, ${username.text}!") }
        }
    }

Compare this with the large amount of code to do the same programmatically in classic Java:

    LayoutParams params = new LinearLayout.LayoutParams(
                       LayoutParams.FILL_PARENT,
                       LayoutParams.WRAP_CONTENT);
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
    EditText name = new EditText(this);
    name.setLayoutParams(params);
    layout.addView(name);
    Button greetings = new Button(this);
    greetings.setText("Greetings");
    greetings.setLayoutParams(params);
    layout.addView(greetings);
    LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(
          LayoutParams.FILL_PARENT,
          LayoutParams.WRAP_CONTENT);
    this.addContentView(layout, layoutParam);
    greetings.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(this, "Hello, " + name.getText(),
                      Toast.LENGTH_SHORT).show();
        }
    }

Kotlin is still a relatively young language, and useful libraries are being developed every day. Keep your eye on kotlinlang.org for up-to-date news on developments in the language.