Kotlin, Zero to Kotlin Hero

Zero to Kotlin Hero: An introduction to the Anko library

Let’s take a look at the Anko Library. The Beyonce of Kotlin Android libraries. Don’t judge me, it’s my opinion lol.

The Anko library is an android library written and maintained by JetBrains. Anko helps you work faster and smarter when building android apps.

From what I gathered, that’s how the name Anko came about.

Anko has four parts:

  1. Anko Commons
  2. Anko Layouts
  3. Anko SQLite
  4. Anko Coroutines

To add the dependency for Anko to your project, simply go to your project level build.gradle file and add this:

buildscript{
   ...
   ext.anko_version = '0.10.8'  // this new line
   repositories{
      ...
   }
   dependencies{
      ...
   }
}

You should add the latest stable Anko version to your project. At the time this article was written, the latest was  0.10.8.

After this, go to the app level build.gradle to add the Anko dependency.

...
dependencies{
   implementation "org.jetbrains.anko:anko:$anko_version"
}

If you do not want the whole library, you can install the modules you need

...
dependencies{
   // Anko commons
   implementation "org.jetbrains.anko:anko-commons:$anko_version"

   // Snackbars
   implementation "org.jetbrains.anko:anko-design:$anko_version"
   // Anko sqlite
   implementation "org.jetbrains.anko:anko-sqlite:$anko_version"
   
   // Anko Coroutines
   implementation "org.jetbrains.anko:anko-coroutines:$anko_version"
   
   // Anko Layouts
   implementation "org.jetbrains.anko:anko-sdk25:$anko_version"
   implementation "org.jetbrains.anko:anko-appcompat-v7:$anko_version"

   // Coroutine listeners for Anko Layouts
   implementation "org.jetbrains.anko:anko-sdk25-coroutines:$anko_version"
   implementation "org.jetbrains.anko:anko-appcompat-v7-coroutines:$anko_version"
}

 

Anko Commons

Anko commons is a lightweight library that contains a lot of helpers for the Android SDK.

Anko commons contains helpers for intents, dialogs & toasts, logging, resources & dimensions.

Anko Commons for Intents

Anko Commons can be used for explicit or implicit intents.

With the regular Kotlin android code, you can move from one activity to another (e.g. SecondActivity) by doing:

val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)

With Anko, it’s done this way:

startActivity<SecondActivity>()

That’s it!

You can also pass some data while launching intents.

// Anko version
startActivity<SecondActivity>("firstname" to "Nenne", "surname" to "Nwodo")
// Normal Kotlin version
val intent = Intent(this, SecondActivity::class.java) 
intent.putExtra("firstname", "Nenne")
intent.putExtra("surname", "Nwodo")
startActivity(intent)

Anko reduced four lines to one line.

Let’s also try to open a link in an external browser. Using the default Kotlin code, we would have three lines

val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse("https://adoranwodo.com")
startActivity(intent)

Let’s call on the Anko magician to transform this. We would now have:

browse("https://adoranwodo.com")

Anko has other wrappers for Intents and I would list them below. These methods return boolean values. If the intent was sent, true is returned and false is returned otherwise:

If you want to make a call, you can simply call:

makeCall(number)

If you want to send a text:

sendSMS(number, [text]) // [text] is optional

If you would like to share text:

share(text, [subject]) // [subject] is optional

If you would like to send an email:

email(email, [subject], [text]) // [subject] and [text] are optional

 

Anko Commons for Dialogs & Toasts

As we have established when talking about intents, Anko makes code shorter and more readable. When using the default syntax, we can launch toast messages like this:

Toast.makeText(this, "Hello!", Toast.LENGTH_SHORT).show()

With Anko however, we can launch the same message like this.

toast("Hello!")

Anko also has a longToast() method if we want the duration of the toast message to be longer.

Similarly, if we want to show a snackbar using Anko, all we have to do is

view.snackbar("Hello!")

 

Showing alerts using Anko Commons

Using the AlertDialog in Android can get messy when you have to call setTitle(), setMessage(), create() etc.

By default, creating an alert dialog can be done using the syntax below:

val alertdialog = AlertDialog.Builder(this)
alertdialog.setTitle("Hello")
alertdialog.setMessage("Welcome to this section, would you like to continue?")
alertdialog.setPositiveButton("Ok"){ _, _ ->
   Toast.makeText(this, "Thanks for clicking yes.", Toast.LENGTH_SHORT).show()
}
alertdialog.setNegativeButton("Cancel"){ _, _ ->
   Toast.makeText(this, "We are sad you clicked no.", Toast.LENGTH_SHORT).show()
}
alertdialog.create().show()  // The frustrating part. If you forget this, your dialog won't show

However, Anko made it easier for us by giving us the power to do this:

alert("Welcome to this section, would you like to continue?", "Hello"){
   yesButton{
      toast("Thanks for clicking yes.")
   }
   noButton{
      toast("We are sad you clicked no.")
   }
}.show()

The Anko version seems cleaner to me 🤷🏼‍♀️.

 

Conclusion

This could go on, but I’d like to finish up Anko Commons in the next article so that this is a lot easier to read.

In the next article, we would cover logging, resources & dimensions. See you there!

 

Spread the love

1 thought on “Zero to Kotlin Hero: An introduction to the Anko library

Leave a Reply

Your email address will not be published. Required fields are marked *