Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Playing with Anko and Android KTX

Playing with Anko and Android KTX

Kotlin is an amazing language and we all love building Android apps using Kotlin. But still we write our code in "kind of" Java fashion and don't leverage powerful features of Kotlin like Extension function.

In this talks my goal is to show how you can make your Android application development faster and easier by leveraging Anko and Android KTX library in more clean and pleasant way.

Outline:

- Refresher about Kotlin for Android
- What is Anko
- Adding Anko to your project
- Amazing APIs by Anko
- What is Android KTX
- Adding Android KTX to your project
- Showcasing Android KTX APIs
- Conclusion

Akshay Chordiya

April 22, 2018
Tweet

More Decks by Akshay Chordiya

Other Decks in Programming

Transcript

  1. ext.anko_version='0.10.4' dependencies { // Commons, Layouts, SQLite implementation "org.jetbrains.anko:anko:$anko_version" }

    // Commons implementation "org.jetbrains.anko:anko-commons:$anko_version" // Coroutines implementation "org.jetbrains.anko:anko-coroutines:$anko_version" // SQLite implementation "org.jetbrains.anko:anko-sqlite:$anko_version" Adding Anko * 0.10.4 is latest version at the moment
  2. dependencies { // Commons, Layouts, SQLite implementation "org.jetbrains.anko:anko:$anko_version" } //

    Commons implementation "org.jetbrains.anko:anko-commons:$anko_version" // Coroutines implementation "org.jetbrains.anko:anko-coroutines:$anko_version" // SQLite implementation "org.jetbrains.anko:anko-sqlite:$anko_version" Adding Anko
  3. Anko Common - Intent // Standard Kotlin way val intent

    = Intent(this, BoringActivity::class.java) // Anko way val intent = intentFor<BoringActivity>
  4. Anko Common - Intent // Standard Kotlin way val intent

    = Intent(this, BoringActivity::class.java) intent.putExtra("id", 1) // Anko way val intent = intentFor<BoringActivity>("id" to 1)
  5. Anko Common - Intent // Standard Kotlin way val intent

    = Intent(this, BoringActivity::class.java) intent.putExtra("id", 1) intent.setFlag(Intent.FLAG_ACTIVITY_SINGLE_TOP) // Anko way val intent = intentFor<BoringActivity>("id" to 1).singleTop()
  6. Anko Common - Intent // Standard Kotlin way val intent

    = Intent(this, BoringActivity::class.java) intent.putExtra("id", 1) startActivity(intent) // Anko way startActivity<BoringActivity>("id" to 1)
  7. Make a call makeCall(number) Send a text sendSMS(number, [text]) Browse

    the web browse(url) Share some text share(text, [subject]) Send a email email(email, [subject], [text]) Some handy intents
  8. Anko Common - Toast // Standard Kotlin way Toast.makeText(this, "Go

    Kotlin!", Toast.LENGTH_SHORT) // Anko way toast("Go Kotlin!") // Standard Kotlin way Toast.makeText(this, "Go Kotlin!", Toast.LENGTH_SHORT).show() And we forget calling “show” // For toast with LENGTH_LONG longToast("Go Kotlin!")
  9. Anko Common - Toast // Anko way toast("Go Kotlin!") inline

    fun Context.toast(message: CharSequence): Toast = Toast .makeText(this, message, Toast.LENGTH_SHORT) .apply { show() }
  10. Anko Common - Toast // Anko way toast("Go Kotlin!") inline

    fun Context.toast(message: CharSequence): Toast = Toast .makeText(this, message, Toast.LENGTH_SHORT) .apply { show() }
  11. Anko Common - Snackbar // Standard Kotlin way Snackbar.make(view, "Go

    Kotlin!", Snackbar.LENGTH_SHORT).show() // Anko way snackbar(view, "Go Kotlin!")
  12. Anko Common - Snackbar // Standard Kotlin way Snackbar.make(layout, "Go

    Kotlin!", Snackbar.LENGTH_SHORT) .setAction("Undo") { // Undo logic } .show() // Anko snackbar(layout, "Go Kotlin!", "Undo") { // Undo logic }
  13. Anko Common - Snackbar // Anko way snackbar(view, "Go Kotlin!")

    inline fun snackbar(view: View, message: Int) = Snackbar .make(view, message, Snackbar.LENGTH_SHORT) .apply { show() }
  14. Anko Common - Alert Dialog // Standard Kotlin way AlertDialog.Builder(this)

    .setTitle("Title") .setMessage("It's DSL magic") .setPositiveButton("Yes") { toast("Awesome") } .setNegativeButton("No") {} .show() // Anko DSL way alert("Title", "It's DSL magic") { yesButton { toast("Awesome") } noButton {} }.show()
  15. Anko Common - Logging class LoggingActivity : AppComaptActivity(), AnkoLogger {

    private fun doLogging() { info("Anko") debug("is") warn("amazing") } }
  16. Anko Common - Misc // DP -> PX val px

    = dip(40) // SP -> PX val px = sp(24) // PX -> DP val dp = px2dip(px) // PX -> SP val sp = px2sp(px)
  17. Anko Layout override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) verticalLayout {

    editText { hint = "Name" textSize = 18f } editText { hint = "Password" textSize = 18f } button("Login") { textSize = 14f } } } No setContentView()
  18. Basic // Kotlin val uri = Uri.parse(stringUri) // KTX val

    uri = stringUri.toUri() // Kotlin sharedPreferences.edit() .putString("kotlin", "awesome") .apply() // KTX sharedPreferences.edit { .putString("kotlin", "awesome") }
  19. Bundle // KTX val bundle = bundleOf("id" to 1, "style"

    to "fancy") // Kotlin val bundle = Bundle() bundle.putLong("id", 1) bundle.putString("style", "fancy")
  20. System Service // KTX val alarmManager = systemService<AlarmManager>() // Kotlin

    val alarmManager = getSystemService(AlarmManager::class.java)
  21. Further Resources • All the images and logos used are

    trademarks of respective companies and/or author • Anko • Introducing Android KTX • Android KTX • Exploring Android KTX - Joe Birch - Medium • Anko Layouts - Antonio Leiva • All Kotlin Extensions