Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

#SVQTECH

Slide 3

Slide 3 text

JVM https://kotlinlang.org

Slide 4

Slide 4 text

1. Efficiency 2. Read-only variables 3. Null-safety 4. Smart Cast 5. Higher-order functions 6. Extension functions Why should you care?

Slide 5

Slide 5 text

1. Efficiency fun sum(a: Int, b: Int): Int { return a + b }

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

3. Null-safety println(user?.name?.length)

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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() }

Slide 15

Slide 15 text

5. Higher-order functions (InputType) -> ReturnType “A higher-order function is a function that takes or returns another function”

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

5. Extension functions fun String.doubleLength(): Int { return this.length * 2 } // ... val x: Int = "John".doubleLength()

Slide 23

Slide 23 text

Bonus: kotlin-stdlib fun Iterable.filter(predicate: (T) -> Boolean) { val out = ArrayList() for (element in this) { if (predicate(element)) out.add(element) } return out }

Slide 24

Slide 24 text

Bonus: kotlin-stdlib val fruits = listOf("banana", "avocado", "apple", "kiwi") fruits .filter { it.startsWith("a") } .sortedBy { it } .map { it.toUpperCase() } .forEach { println(it) }

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

0. Android Project Main screen, entry point App descriptor Main screen interface Compile and build instructions

Slide 27

Slide 27 text

An activity is the entry point for interacting with the user 1. Activities

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

1. Activities An Android app can have multiple entry points and activities

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

(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

Slide 32

Slide 32 text

2. Layout

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

2. Layout

Slide 35

Slide 35 text

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 } } }

Slide 36

Slide 36 text

3. Manifest App components ‍♀ Required user permissions Required hardware/software

Slide 37

Slide 37 text

3. Manifest

Slide 38

Slide 38 text

3. Manifest ♀

Slide 39

Slide 39 text

github.com/lgvalle/androidkotlinworkshop