Slide 1

Slide 1 text

Android Development Workshop Peter-John Welcome @pjapplez Mobile Engineering Lead

Slide 2

Slide 2 text

Day 1

Slide 3

Slide 3 text

What we will do Today: Intro: 1. Create our first Android app and run it. 2. Git Crash Course 3. Learn Kotlin for Android 4. Crash Course on Android Layouts. (Constraint Layouts) 5. RecyclerViews (View Holder Pattern)

Slide 4

Slide 4 text

Let’s open Android Studio

Slide 5

Slide 5 text

Android Dependencies dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support.constraint:constraint-layout:1.1.2' implementation 'com.android.support:recyclerview-v7:27.1.1' }

Slide 6

Slide 6 text

What is git?

Slide 7

Slide 7 text

What is git? ● Git is an example of a DVCS (hence Distributed Version Control System)

Slide 8

Slide 8 text

Kotlin

Slide 9

Slide 9 text

Google announced Kotlin will be an official language for Android

Slide 10

Slide 10 text

Why Kotlin over Java? ● Java is very verbose. ● Java Code is error prone with all its anonymous inner classes. ● Java lacks modern language features. ● Nullability issues.

Slide 11

Slide 11 text

Language Features

Slide 12

Slide 12 text

var/val var name : String = "Peter" name = "John" val surname = "Welcome" surname = "Johnson" // val can’t be reassigned

Slide 13

Slide 13 text

Number one reason Android Apps Crash?

Slide 14

Slide 14 text

Null Pointer Exception

Slide 15

Slide 15 text

Nullability var name : String = "Peter" name = null // null can’t be a value on a non-null String var surname : String? = "Peter" surname = null val otherName = surname ?: "" // Elvis operator ( ?? in other languages)

Slide 16

Slide 16 text

Classes

Slide 17

Slide 17 text

Classes class User { private val name: String private val surname : String constructor(name : String , surname: String) { this .name = name this.surname = surname } } var user = User( surname = "Welcome", name = "Peter") // named arguments

Slide 18

Slide 18 text

Data class @Parcelize data class Human (private var name: String, val age: Int) : Parcelable //copy, equal, hashcode, toString var human = Human("Peter",27)

Slide 19

Slide 19 text

Sealed Classes sealed class Result{ data class Success (val value : T) : Result() data class Error(val error : MyError) : Result() }

Slide 20

Slide 20 text

Sealed Classes fun resultsFromService(result : Result) : String { when (result) { is Result.Success -> "${result.value} World" is Result.Error -> return result.error.toString() } return "" } resultsFromService(Result.Success("Hello"))

Slide 21

Slide 21 text

Functions

Slide 22

Slide 22 text

First class citizens

Slide 23

Slide 23 text

High order functions

Slide 24

Slide 24 text

Functions fun hello(){ print("Hello") } val functionAsVariable = hello() val greetingPeter = fun (func : (name :String) -> String ) : String = func("Peter") greetingPeter { "Hello $it" }

Slide 25

Slide 25 text

Extension functions

Slide 26

Slide 26 text

Extension functions infix fun Int.add (value : Int) = this + value val result = 1.add(1) val result = 1 add 1 //Extension function fun Observable.applySchedulers() : Observable = this.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) //Usage disposable.add(this.viewModel.createPlant(Plant(name = "Best Plant Ever")).applySchedulers().subscribe ())

Slide 27

Slide 27 text

Function Literals with Receiver

Slide 28

Slide 28 text

Function Literals with Receiver inline fun SharedPreferences.edit(action: SharedPreferences.Editor.() -> Unit) { val editor = edit() editor.action() editor.apply() } getSharedPreferences("cache", Context.MODE_PRIVATE).edit { putString("username", "Peter") }

Slide 29

Slide 29 text

Async Programming

Slide 30

Slide 30 text

AsyncTasks class doAsyncTask(textView: TextView) : AsyncTask() { val innerTextView: TextView? = textView override fun doInBackground(vararg params: Unit?): String? { try { ... return data } catch (e: Exception) { return null } } override fun onPostExecute(result: String?) { super.onPostExecute(result) innerTextView?.text = JSONObject(result).toString() } }

Slide 31

Slide 31 text

AsyncTasks

Slide 32

Slide 32 text

RxJava disposable.add(viewModel.fetchDashboardPlants() .applySchedulers() .subscribe({ //onNext: ... }, { error -> //onError }) ) fun Observable.applySchedulers() : Observable = this.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())

Slide 33

Slide 33 text

Coroutines internal val background = newFixedThreadPoolContext(2, "background") launch (background) { val result = resultsFromService(Result.Success("Hello")) launch(UI) { this.textView.text = result } } // The async await way val someLongRunningTask = async(CommonPool) { return@async "" }.await()

Slide 34

Slide 34 text

Let’s build our app.

Slide 35

Slide 35 text

Life Cycles and Resources

Slide 36

Slide 36 text

Layouts (Constraint Layout)

Slide 37

Slide 37 text

RecyclerView

Slide 38

Slide 38 text

RecyclerView

Slide 39

Slide 39 text

RecyclerView

Slide 40

Slide 40 text

RecyclerView class AnimalAdapter(val items: ArrayList) : RecyclerView.Adapter() { // Gets the number of animals in the list override fun getItemCount(): Int { return items.size } // Inflates the item views override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.animal_list_item, parent, false)) } // Binds each animal in the ArrayList to a view override fun onBindViewHolder(holder: ViewHolder, position: Int) { holder.tvAnimalType.text = items.get(position) } } class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { // Holds the TextView that will add each animal to val tvAnimalType = view.tv_animal_type }

Slide 41

Slide 41 text

Slides https://tinyurl.com/ycb3cn7f Workshop Feedback https://bit.ly/2uFagmK