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

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

Luis G. Valle

June 16, 2019
Tweet

More Decks by Luis G. Valle

Other Decks in Programming

Transcript

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

    5. Higher-order functions 6. Extension functions Why should you care?
  2. 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
  3. 3. Null-safety val name: String = "John" val last: String?

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

    = "Wick" // ... println(name.length) println(last?.length)
  5. 3. Null-safety println(user?.name?.length) == println( if (user != null &&

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

    fun bark() } class Cat: Animal() { fun meow() }
  7. 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() }
  8. 5. Higher-order functions (InputType) -> ReturnType “A higher-order function is

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

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

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

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

    // ... return parser(name) } val x = parseName { it.length } println(x)
  13. 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 }
  14. Bonus: kotlin-stdlib val fruits = listOf("banana", "avocado", "apple", "kiwi") fruits

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

    screen interface Compile and build instructions
  16. (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
  17. 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>
  18. 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 } } }
  19. 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>
  20. 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> ♀