My first Android app with Kotlin

My first Android app with Kotlin

Intro slides for the workshop 'My first Android app with Kotlin'
More info: https://github.com/lgvalle/androidkotlinworkshop

8897931773317248080248df1250a8ea?s=128

Luis G. Valle

June 16, 2019
Tweet

Transcript

  1. My first Android app with Kotlin https://github.com/lgvalle/androidkotlinworkshop Luis G. Valle

    - @lgvalle
  2. #SVQTECH

  3. JVM https://kotlinlang.org

  4. 1. Efficiency 2. Read-only variables 3. Null-safety 4. Smart Cast

    5. Higher-order functions 6. Extension functions Why should you care?
  5. 1. Efficiency fun sum(a: Int, b: Int): Int { return

    a + b }
  6. 1. Efficiency fun sum(a: Int, b: Int): Int = a

    + b
  7. 1. Efficiency fun sum(a: Int, b: Int) = a +

    b
  8. 2. Read-only variables val a: Int = 1 // Read-only

    Int val b = 2 // Int type is inferred b = 3 // Compiler error! var x = 5 // Read-write Int x += 1 // Can be reassigned
  9. 3. Null-safety val name: String = "John" val last: String?

    = "Wick" // ... println(name.length) println(last.length)
  10. 3. Null-safety val name: String = "John" val last: String?

    = "Wick" // ... println(name.length) println(last?.length)
  11. 3. Null-safety println(user?.name?.length)

  12. 3. Null-safety println(user?.name?.length) == println( if (user != null &&

    user.name != null) { user.name.length } else { null })
  13. 4. Smart Cast open class Animal class Dog: Animal() {

    fun bark() } class Cat: Animal() { fun meow() }
  14. 4. Smart Cast open class Animal class Dog: Animal() {

    fun bark() } class Cat: Animal() { fun meow() } val animal: Animal = fetch() when(animal) { is Dog -> animal.bark() is Cat -> animal.meow() }
  15. 5. Higher-order functions (InputType) -> ReturnType “A higher-order function is

    a function that takes or returns another function”
  16. 5. Higher-order functions ... fun parseName(parser: (String) -> Int): Int

    { // ... return parser(name) }
  17. 5. Higher-order functions ... fun parseName(parser: (String) -> Int): Int

    { // ... return parser(name) }
  18. 5. Higher-order functions fun parseName(parser: (String) -> Int): Int {

    // ... return parser(name) } val x: Int = parseName({ input: String -> input.length }) println(x)
  19. 5. Higher-order functions fun parseName(parser: (String) -> Int): Int {

    // ... return parser(name) } val x: Int = parseName { input: String -> input.length } println(x)
  20. 5. Higher-order functions fun parseName(parser: (String) -> Int): Int {

    // ... return parser(name) } val x = parseName { input -> input.length } println(x)
  21. 4. Higher-order functions fun parseName(parser: (String) -> Int): Int {

    // ... return parser(name) } val x = parseName { it.length } println(x)
  22. 5. Extension functions fun String.doubleLength(): Int { return this.length *

    2 } // ... val x: Int = "John".doubleLength()
  23. Bonus: kotlin-stdlib fun Iterable<T>.filter(predicate: (T) -> Boolean) { val out

    = ArrayList<T>() for (element in this) { if (predicate(element)) out.add(element) } return out }
  24. Bonus: kotlin-stdlib val fruits = listOf("banana", "avocado", "apple", "kiwi") fruits

    .filter { it.startsWith("a") } .sortedBy { it } .map { it.toUpperCase() } .forEach { println(it) }
  25. None
  26. 0. Android Project Main screen, entry point App descriptor Main

    screen interface Compile and build instructions
  27. An activity is the entry point for interacting with the

    user 1. Activities
  28. 1. Activities An activity represents a single screen with a

    user interface
  29. 1. Activities An Android app can have multiple entry points

    and activities
  30. 1. Activities Any Android app can start another app’s activity

  31. (Resources) Unique int ID for every resource cookie_oreo.png -> R.drawable.cookie_oreo

    activity_main.xml -> R.layout.activity_main Alternative resources for different device configurations res / values-en / strings.xml res / values-fr / strings.xml
  32. 2. Layout

  33. None
  34. 2. Layout <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout> <EditText android:id="@+id/msgText" android:layout_width="wrap_content" app:layout_constraintLeft_toLeftOf="parent"

    ... /> <Button android:id="@+id/sendBtn" android:text="Send" app:layout_constraintLeft_toRightOf="@id/msgText" ... /> </androidx.constraintlayout.widget.ConstraintLayout>
  35. 2.1. Activity <-> Layout class MainActivity: AppCompatActivity() { override fun

    onCreate(...) { ... setContentView(R.layout.activity_main) val msgText = findViewById(R.id.msgText) val sendButton = findViewById(R.id.sendBtn) sendButton.setOnClickListener { // Do something when user clicks } } }
  36. 3. Manifest App components ‍♀ Required user permissions Required hardware/software

  37. 3. Manifest <?xml version="1.0" encoding="utf-8"?> <manifest ... > <uses-feature android:name="android.hardware.camera"/>

    <uses-permission android:name="android.permission.SEND_SMS"/> <application ...> <activity android:name="com.example.MainActivity" android:label="@string/main_title"/> <activity android:name="com.example.DetailsActivity" android:label="@string/details_title"/> </application> </manifest>
  38. 3. Manifest <?xml version="1.0" encoding="utf-8"?> <manifest ... > <uses-feature android:name="android.hardware.camera"/>

    <uses-permission android:name="android.permission.SEND_SMS"/> <application ...> <activity android:name="com.example.MainActivity" android:label="@string/main_title"/> <activity android:name="com.example.DetailsActivity" android:label="@string/details_title"/> </application> </manifest> ♀
  39. github.com/lgvalle/androidkotlinworkshop