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

Android KTX (HWSW free! 34)

Android KTX (HWSW free! 34)

A lightning talk showing off some of the syntax gains that Google’s Android KTX library offers.

Talk recording (Hungarian): https://www.youtube.com/watch?v=R-oIo4A02CU

Marton Braun

June 19, 2018
Tweet

More Decks by Marton Braun

Other Decks in Programming

Transcript

  1. Smoother Android development with Kotlin
    Márton Braun • AutSoft • [email protected]

    View full-size slide

  2. Android APIs are sometimes hard to use
    Kotlin allows us to reshape existing API
    Extension functions and properties
    Named and default parameters
    Operator overloading
    Lambdas, higher order functions, and inline functions
    Everyone has their own collection of utilities…

    View full-size slide

  3. Google takes a stance, creates their own
    First appeared Feb 2, 2018
    Goals
    Only provide uniquely Kotlin APIs
    Don’t provide new implementation, just new syntax
    Minimize overhead, focus on zero overhead abstractions

    View full-size slide

  4. Extension properties
    view.setVisibility(View.VISIBLE)
    view.visibility = View.VISIBLE
    view.isVisible = true

    View full-size slide

  5. Default and named parameters
    view.setPadding(20, view.topPadding,
    20, view.bottomPadding)
    view.updatePadding(left = 20, right = 20)

    View full-size slide

  6. Operator overloading and extension functions
    linearLayout.addView(view)
    linearLayout += view
    linearLayout.removeView(view)
    linearLayout -= view

    View full-size slide

  7. Higher order, inline extension functions
    for (i in 0 until linearLayout.childCount) {
    val view = linearLayout.get(i)
    Timber.d(view.tag)
    }
    linearLayout.forEach { view ->
    Timber.d(view.tag)
    }

    View full-size slide

  8. Extension functions
    val bitmap = Bitmap.createBitmap(
    view.width, view.height, Bitmap.Config.ARGB_8888)
    view.draw(Canvas(bitmap))
    val bitmap = view.toBitmap()

    View full-size slide

  9. Operator overloading
    val color = bitmap.getPixel(100, 50)
    val color = bitmap[100, 50]
    bitmap.setPixel(100, 50, color)
    bitmap[100, 50] = color

    View full-size slide

  10. Extension functions, default parameters
    Toast.makeText(this, "Hello world", Toast.LENGTH_SHORT)
    .show()
    toast("Hello world")

    View full-size slide

  11. Extension functions, default parameters
    Toast.makeText(this, "Hello world", Toast.LENGTH_LONG)
    .show()
    toast("Hello world", duration = Toast.LENGTH_LONG)

    View full-size slide

  12. Higher order, inline extension functions
    sharedPreferences.edit()
    .putBoolean("drawerOpened", true)
    .apply()
    sharedPreferences.edit {
    putBoolean("drawerOpened", true)
    }

    View full-size slide

  13. Higher order, inline extension functions
    db.beginTransaction()
    db.insert("users", null, values)
    db.setTransactionSuccessful()
    db.endTransaction()
    db.transaction {
    insert("users", null, values)
    }

    View full-size slide

  14. inline fun SQLiteDatabase.transaction(...): T {
    beginTransaction()
    try {
    val result = body()
    setTransactionSuccessful()
    return result
    } finally {
    endTransaction()
    }
    }

    View full-size slide

  15. Destructuring
    val lat = location.latitude
    val lon = location.longitude
    val (lat, lon) = location

    View full-size slide

  16. Explore Android KTX yourself and use it in your projects
    https://github.com/android/android-ktx/
    https://android.github.io/android-ktx/core-ktx/
    Write your own extensions for APIs that don’t fit your needs
    If these are on Android, consider submitting a PR to Android KTX

    View full-size slide

  17. Questions?
    Márton Braun • AutSoft • [email protected]

    View full-size slide