Professional Documents
Culture Documents
All Lessons Combined
All Lessons Combined
Development
with Kotlin
Android Development with Kotlin v1.0 This work is licensed under the Apache 2 license. 1
About this course
Android Development with Kotlin This work is licensed under the Apache 2 license. 2
Prerequisites
Android Development with Kotlin This work is licensed under the Apache 2 license. 3
What you'll learn
Android Development with Kotlin This work is licensed under the Apache 2 license. 4
The opportunity
● Mobile devices are becoming increasingly
commonplace
● Mobile apps connect users to information
and services that can improve their quality of
life
● Many industries have yet to be revolutionized
through mobile, and offer great opportunities
for new businesses and solutions
Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Android
Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Available across different form factors
Android Development with Kotlin This work is licensed under the Apache 2 license. 7
Build Android apps in Kotlin
Android Development with Kotlin This work is licensed under the Apache 2 license. 8
Kotlin
Android Development with Kotlin This work is licensed under the Apache 2 license. 9
Benefits of Kotlin
Android Development with Kotlin This work is licensed under the Apache 2 license. 10
Idiomatic Kotlin
Android Development with Kotlin This work is licensed under the Apache 2 license. 11
Learning experience
Android Development with Kotlin This work is licensed under the Apache 2 license. 12
Course structure
4 units with a total of 13 lessons across 13 weeks
Basics, Functions, First App, Layouts, App Architecture, Data App UI Design
Classes & Objects, Navigation Persistence, Display
Extensions Lists, Connect to
Internet, Background
Work
Android Development with Kotlin This work is licensed under the Apache 2 license. 13
Lectures
We’ll cover important topics together as a class.
Android Development with Kotlin This work is licensed under the Apache 2 license. 14
Learning pathways
Android Development with Kotlin This work is licensed under the Apache 2 license. 15
Accessing the pathways
Android Development with Kotlin This work is licensed under the Apache 2 license. 16
Pathway
Android Development with Kotlin This work is licensed under the Apache 2 license. 17
Codelab
Android Development with Kotlin This work is licensed under the Apache 2 license. 18
Earn badges for your developer profile
Android Development with Kotlin This work is licensed under the Apache 2 license. 19
What you need
To work through the Kotlin and Android examples in the Android
Development with Kotlin labs you'll need to install the following software
on your computer:
● Java Development Kit
● Java Runtime Engine (Windows only)
● IntelliJ IDEA
● Android Studio
Android Development with Kotlin This work is licensed under the Apache 2 license. 20
Resources
Android Development with Kotlin This work is licensed under the Apache 2 license. 21
Kotlin resources
Android Development with Kotlin This work is licensed under the Apache 2 license. 22
Android and other resources
● Official Android developer website
● @AndroidDev on Twitter
● Stack Overflow
Android Development with Kotlin This work is licensed under the Apache 2 license. 23
Lesson 1:
Kotlin basics
Android Development with Kotlin v1.0 This work is licensed under the Apache 2 license. 1
About this lesson
Lesson 1: Kotlin basics
○ Get started
○ Operators
○ Data types
○ Variables
○ Conditionals
○ Lists and arrays
○ Null safety
○ Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 2
Get started
Android Development with Kotlin This work is licensed under the Apache 2 license. 3
Open IntelliJ IDEA
Android Development with Kotlin This work is licensed under the Apache 2 license. 4
Create a new project
Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Name the project
Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Open REPL (Read-Eval-Print-Loop)
Android Development with Kotlin This work is licensed under the Apache 2 license. 7
Create a printHello() function
Press Control+Enter
(Command+Enter on
a Mac) to execute.
Android Development with Kotlin This work is licensed under the Apache 2 license. 8
Operators
Android Development with Kotlin This work is licensed under the Apache 2 license. 9
Operators
● Mathematical operators
● Increment and decrement operators
● Comparison operators
● Assignment operator
● Equality operators
Android Development with Kotlin This work is licensed under the Apache 2 license. 10
Math operators with integers
Android Development with Kotlin This work is licensed under the Apache 2 license. 11
Math operators with doubles
Android Development with Kotlin This work is licensed under the Apache 2 license. 12
Math operators
⇒ ⇒
⇒ indicates output
⇒ ⇒ from your code.
Android Development with Kotlin This work is licensed under the Apache 2 license. 13
Numeric operator methods
Kotlin keeps numbers as primitives, but lets you call methods on numbers
as if they were objects.
Android Development with Kotlin This work is licensed under the Apache 2 license. 14
Data types
Android Development with Kotlin This work is licensed under the Apache 2 license. 15
Integer types
Type Bits Notes
Android Development with Kotlin This work is licensed under the Apache 2 license. 16
Floating-point and other numeric types
Type Bits Notes
Android Development with Kotlin This work is licensed under the Apache 2 license. 17
Operand types
Results of operations keep the types of the operands
⇒ ⇒
⇒ ⇒
Android Development with Kotlin This work is licensed under the Apache 2 license. 18
Type casting
Assign an Int to a Byte
⇒
Convert Int to Byte with casting
Android Development with Kotlin This work is licensed under the Apache 2 license. 19
Underscores for long numbers
Android Development with Kotlin This work is licensed under the Apache 2 license. 20
Strings
Android Development with Kotlin This work is licensed under the Apache 2 license. 21
String concatenation
Android Development with Kotlin This work is licensed under the Apache 2 license. 22
String templates
A template expression starts with a dollar sign ($) and can be a simple value:
Android Development with Kotlin This work is licensed under the Apache 2 license. 23
String template expressions
Android Development with Kotlin This work is licensed under the Apache 2 license. 24
Variables
Android Development with Kotlin This work is licensed under the Apache 2 license. 25
Variables
Android Development with Kotlin This work is licensed under the Apache 2 license. 26
Specifying the variable type
Colon Notation
Important: Once a type has been assigned by you or the compiler, you can't
change the type or you get an error.
Android Development with Kotlin This work is licensed under the Apache 2 license. 27
Mutable and immutable variables
● Mutable (Changeable)
● Immutable (Unchangeable)
Android Development with Kotlin This work is licensed under the Apache 2 license. 28
var and val
Android Development with Kotlin This work is licensed under the Apache 2 license. 29
Conditionals
Android Development with Kotlin This work is licensed under the Apache 2 license. 30
Control flow
Android Development with Kotlin This work is licensed under the Apache 2 license. 31
if/else statements
Android Development with Kotlin This work is licensed under the Apache 2 license. 32
if statement with multiple cases
Android Development with Kotlin This work is licensed under the Apache 2 license. 33
Ranges
Android Development with Kotlin This work is licensed under the Apache 2 license. 34
Ranges in if/else statements
Note: There are no spaces around the "range to" operator (1..100)
Android Development with Kotlin This work is licensed under the Apache 2 license. 35
when statement
As well as a when statement, you can also define a when expression that
provides a return value.
Android Development with Kotlin This work is licensed under the Apache 2 license. 36
for loops
You don’t need to define an iterator variable and increment it for each pass.
Android Development with Kotlin This work is licensed under the Apache 2 license. 37
for loops: elements and indexes
Android Development with Kotlin This work is licensed under the Apache 2 license. 38
for loops: step sizes and ranges
⇒
for (i in 3..6 step 2) print(i)
⇒
Android Development with Kotlin This work is licensed under the Apache 2 license. 39
while loops
var bicycles = 0
while (bicycles < 50) {
bicycles++
}
println("$bicycles bicycles in the bicycle rack\n")
⇒
do {
bicycles--
} while (bicycles > 50)
println("$bicycles bicycles in the bicycle rack\n")
⇒
Android Development with Kotlin This work is licensed under the Apache 2 license. 40
repeat loops
Android Development with Kotlin This work is licensed under the Apache 2 license. 41
Lists and arrays
Android Development with Kotlin This work is licensed under the Apache 2 license. 42
Lists
Android Development with Kotlin This work is licensed under the Apache 2 license. 43
Immutable list using listOf()
Android Development with Kotlin This work is licensed under the Apache 2 license. 44
Mutable list using mutableListOf()
With a list defined with val, you can't change which list the variable refers to, but
you can still change the contents of the list.
Android Development with Kotlin This work is licensed under the Apache 2 license. 45
Arrays
Android Development with Kotlin This work is licensed under the Apache 2 license. 46
Array using arrayOf()
With an array defined with val, you can't change which array the variable refers
to, but you can still change the contents of the array.
Android Development with Kotlin This work is licensed under the Apache 2 license. 47
Arrays with mixed or single types
An array can also contain just one type (integers in this case).
Android Development with Kotlin This work is licensed under the Apache 2 license. 48
Combining arrays
Android Development with Kotlin This work is licensed under the Apache 2 license. 49
Null safety
Android Development with Kotlin This work is licensed under the Apache 2 license. 50
Null safety
Android Development with Kotlin This work is licensed under the Apache 2 license. 51
Variables cannot be null
Android Development with Kotlin This work is licensed under the Apache 2 license. 52
Safe call operator
The safe call operator (?), after the type indicates that a variable can be null.
Android Development with Kotlin This work is licensed under the Apache 2 license. 53
Testing for null
Check whether the numberOfBooks variable is not null. Then decrement that
variable.
Now look at the Kotlin way of writing it, using the safe call operator.
Android Development with Kotlin This work is licensed under the Apache 2 license. 54
The !! operator
If you’re certain a variable won’t be null, use !! to force the variable into a
non-null type. Then you can call methods/properties on it.
Android Development with Kotlin This work is licensed under the Apache 2 license. 55
Elvis operator
The ?: operator is sometimes called the "Elvis operator," because it's like a smiley
on its side with a pompadour hairstyle, like Elvis Presley styled his hair.
Android Development with Kotlin This work is licensed under the Apache 2 license. 56
Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 57
Summary
In Lesson 1, you learned how to:
● Create an IntelliJ IDEA project, opening REPL, and execute a function
● Use operators and numeric operator methods
● Use data types, type casting, strings, and string templates
● Use variables and type inference, and mutable and immutable
variables
● Use conditionals, control flow, and looping structures
● Use lists and arrays
● Use Kotlin's null safety features
Android Development with Kotlin This work is licensed under the Apache 2 license. 58
Pathway
Android Development with Kotlin This work is licensed under the Apache 2 license. 59
Lesson 2:
Functions
Android Development with Kotlin v1.0 This work is licensed under the Apache 2 license. 1
About this lesson
Lesson 2: Functions
○ Programs in Kotlin
○ (Almost) Everything has a value
○ Functions in Kotlin
○ Compact functions
○ Lambdas and higher-order functions
○ List filters
○ Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 2
Programs in Kotlin
Android Development with Kotlin This work is licensed under the Apache 2 license. 3
Setting up
Before you can write code and run programs, you need to:
Android Development with Kotlin This work is licensed under the Apache 2 license. 4
Create a new Kotlin file
In IntelliJ IDEA's Project pane, under Hello World, right-click the src folder.
● Select New > Kotlin File/Class.
● Select File, name the file Hello, and press Enter.
Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Create a Kotlin file
You should now see a file in the src folder called Hello.kt.
Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Create a main() function
main() is the entry point for execution for a Kotlin program.
Android Development with Kotlin This work is licensed under the Apache 2 license. 7
Run your Kotlin program
To run your program, click the Run icon ( ) to the left of the main() function.
IntelliJ IDEA runs the program, and displays the results in the console.
Android Development with Kotlin This work is licensed under the Apache 2 license. 8
Pass arguments to main()
Select Run > Edit Configurations to open the Run/Debug Configurations window.
Android Development with Kotlin This work is licensed under the Apache 2 license. 9
Use arguments in
Android Development with Kotlin This work is licensed under the Apache 2 license. 10
(Almost) Everything has a value
Android Development with Kotlin This work is licensed under the Apache 2 license. 11
(Almost) Everything is an expression
Android Development with Kotlin This work is licensed under the Apache 2 license. 12
Expression values
Android Development with Kotlin This work is licensed under the Apache 2 license. 13
Functions in Kotlin
Android Development with Kotlin This work is licensed under the Apache 2 license. 14
About functions
15
Android Development with Kotlin This work is licensed under the Apache 2 license.
Parts of a function
Android Development with Kotlin This work is licensed under the Apache 2 license. 16
Unit returning functions
If a function does not return any useful value, its return type is Unit.
Android Development with Kotlin This work is licensed under the Apache 2 license. 17
Unit returning functions
The Unit return type declaration is optional.
is equivalent to:
Android Development with Kotlin This work is licensed under the Apache 2 license. 18
Function arguments
● Default parameters
● Required parameters
● Named arguments
Android Development with Kotlin This work is licensed under the Apache 2 license. 19
Default parameters
Default values provide a fallback if no parameter value is passed.
Android Development with Kotlin This work is licensed under the Apache 2 license. 20
Required parameters
Required parameters
Android Development with Kotlin This work is licensed under the Apache 2 license. 21
Default versus required parameters
Functions can have a mix of default and required parameters.
Android Development with Kotlin This work is licensed under the Apache 2 license. 22
Named arguments
It's considered good style to put default arguments after positional arguments,
that way callers only have to specify the required arguments.
Android Development with Kotlin This work is licensed under the Apache 2 license. 23
Compact functions
Android Development with Kotlin This work is licensed under the Apache 2 license. 24
Single-expression functions
Complete version
Compact version
Android Development with Kotlin This work is licensed under the Apache 2 license. 25
Lambdas and higher-order
functions
Android Development with Kotlin This work is licensed under the Apache 2 license. 26
Kotlin functions are first-class
Android Development with Kotlin This work is licensed under the Apache 2 license. 27
Lambda functions
A lambda is an expression that makes a function that has no name.
⇒ Code to execute
Android Development with Kotlin This work is licensed under the Apache 2 license. 28
Syntax for function types
Kotlin's syntax for function types is closely related to its syntax for lambdas.
Declare a variable that holds a function.
Android Development with Kotlin This work is licensed under the Apache 2 license. 29
Higher-order functions
Higher-order functions take functions as parameters, or return a function.
The body of the code calls the function that was passed as the second argument,
and passes the first argument along to it.
Android Development with Kotlin This work is licensed under the Apache 2 license. 30
Higher-order functions
To call this function, pass it a string and a function.
Android Development with Kotlin This work is licensed under the Apache 2 license. 31
Passing a function reference
Use the :: operator to pass a named function as an argument to another function.
The :: operator lets Kotlin know that you are passing the function reference as an
argument, and not trying to call the function.
Android Development with Kotlin This work is licensed under the Apache 2 license. 32
Last parameter call syntax
Kotlin prefers that any parameter that takes a function is the last parameter.
You can pass a lambda as a function parameter without putting it inside the
parentheses.
Android Development with Kotlin This work is licensed under the Apache 2 license. 33
Using higher-order functions
Many Kotlin built-in functions are defined using last parameter call syntax.
Android Development with Kotlin This work is licensed under the Apache 2 license. 34
List filters
Android Development with Kotlin This work is licensed under the Apache 2 license. 35
List filters
Get part of a list based on some condition
Android Development with Kotlin This work is licensed under the Apache 2 license. 36
Iterating through lists
If a function literal has only one parameter, you can omit its declaration and the
"->". The parameter is implicitly declared under the name it.
Filter iterates through a collection, where it is the value of the element during the
iteration. This is equivalent to:
OR
Android Development with Kotlin This work is licensed under the Apache 2 license. 37
List filters
The filter condition in curly braces {} tests each item as the filter loops through.
If the expression returns true, the item is included.
Android Development with Kotlin This work is licensed under the Apache 2 license. 38
Eager and lazy filters
Evaluation of expressions in lists:
Lazy evaluation of lists is useful if you don't need the entire result, or if the list is
exceptionally large and multiple copies wouldn't wouldn't fit into RAM.
Android Development with Kotlin This work is licensed under the Apache 2 license. 39
Eager filters
Filters are eager by default. A new list is created each time you use a filter.
Android Development with Kotlin This work is licensed under the Apache 2 license. 40
Lazy filters
Sequences are data structures that use lazy evaluation, and can be used with
filters to make them lazy.
Android Development with Kotlin This work is licensed under the Apache 2 license. 41
Sequences -> lists
Android Development with Kotlin This work is licensed under the Apache 2 license. 42
Other list transformations
● map() performs the same transform on every item and returns the list.
Android Development with Kotlin This work is licensed under the Apache 2 license. 43
Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 44
Summary
In Lesson 2, you learned how to:
● Create a file and a main() function in your project, and run a program
● Pass arguments to the main() function
● Use the returned value of an expression
● Use default arguments to replace multiple versions of a function
● Use compact functions, to make code more readable
● Use lambdas and higher-order functions
● Use eager and lazy list filters
Android Development with Kotlin This work is licensed under the Apache 2 license. 45
Pathway
Android Development with Kotlin This work is licensed under the Apache 2 license. 46
Lesson 3:
Classes and
objects
Android Development with Kotlin v1.0 This work is licensed under the Apache 2 license. 1
About this lesson
Lesson 3: Classes and objects
○ Classes
○ Inheritance
○ Extension functions
○ Special classes
○ Organizing your code
○ Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 2
Classes
Android Development with Kotlin This work is licensed under the Apache 2 license. 3
Class
Object
● Classes are blueprints for objects
instances
● Classes define methods that operate
on their object instances
Class
Android Development with Kotlin This work is licensed under the Apache 2 license. 4
Class versus object instance
House Class Object Instances
Data
● House color (String)
● Number of windows (Int)
● Is for sale (Boolean)
Behavior
● updateColor()
● putOnSale() FOR SALE
Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Define and use a class
Class Definition Create New Object Instance
Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Constructors
When a constructor is defined in the class header, it can contain:
● No parameters
class A
● Parameters
○ Not marked with var or val → copy exists only within scope of the
constructor
class B(x: Int)
○ Marked var or val → copy exists in all instances of the class
class C(val y: Int)
Android Development with Kotlin This work is licensed under the Apache 2 license. 7
Constructor examples
Android Development with Kotlin This work is licensed under the Apache 2 license. 8
Default parameters
Class instances can have default values.
● Use default values to reduce the number of constructors needed
● Default parameters can be mixed with required parameters
● More concise (don’t need to have multiple constructor versions)
Android Development with Kotlin This work is licensed under the Apache 2 license. 9
Primary constructor
Declare the primary constructor within the class header.
Android Development with Kotlin This work is licensed under the Apache 2 license. 10
Initializer block
Android Development with Kotlin This work is licensed under the Apache 2 license. 11
Initializer block example
Use the init keyword:
Android Development with Kotlin This work is licensed under the Apache 2 license. 12
Multiple constructors
Android Development with Kotlin This work is licensed under the Apache 2 license. 13
Multiple constructors example
Android Development with Kotlin This work is licensed under the Apache 2 license. 14
Properties
Android Development with Kotlin This work is licensed under the Apache 2 license. 15
Person class with name property
Android Development with Kotlin This work is licensed under the Apache 2 license. 16
Custom getters and setters
If you don’t want the default get/set behavior:
Format:
Android Development with Kotlin This work is licensed under the Apache 2 license. 17
Custom getter
Android Development with Kotlin This work is licensed under the Apache 2 license. 18
Custom setter
Android Development with Kotlin This work is licensed under the Apache 2 license. 19
Member functions
Android Development with Kotlin This work is licensed under the Apache 2 license. 20
Inheritance
Android Development with Kotlin This work is licensed under the Apache 2 license. 21
Inheritance
If you don't want to be limited by only inheriting a single class, you can define an
interface since you can implement as many of those as you want.
Android Development with Kotlin This work is licensed under the Apache 2 license. 22
Interfaces
Format:
Android Development with Kotlin This work is licensed under the Apache 2 license. 23
Interface example
Android Development with Kotlin This work is licensed under the Apache 2 license. 24
Extending classes
To extend a class:
● Create a new class that uses an existing class as its core
(subclass)
● Add functionality to a class without creating a new one
(extension functions)
Android Development with Kotlin This work is licensed under the Apache 2 license. 25
Creating a new class
Android Development with Kotlin This work is licensed under the Apache 2 license. 26
Classes are final by default
Declare a class
Try to subclass
Android Development with Kotlin This work is licensed under the Apache 2 license. 27
Use keyword
Use open to declare a class so that it can be subclassed.
Declare a class
Subclass from C
Android Development with Kotlin This work is licensed under the Apache 2 license. 28
Overriding
● Must use open for properties and methods that can be overridden
(otherwise you get compiler error)
Android Development with Kotlin This work is licensed under the Apache 2 license. 29
Abstract classes
● Class is marked as abstract
● Cannot be instantiated, must be subclassed
● Similar to an interface with the added the ability to store state
● Properties and functions marked with abstract must be
overridden
● Can include non-abstract properties and functions
Android Development with Kotlin This work is licensed under the Apache 2 license. 30
Example abstract classes
abstract class Food {
abstract val kcal : Int
abstract val name : String
fun consume() = println("I'm eating ${name}")
}
class Pizza() : Food() {
override val kcal = 600
override val name = "Pizza"
}
fun main() {
Pizza().consume() // "I'm eating Pizza"
}
Android Development with Kotlin This work is licensed under the Apache 2 license. 31
When to use each
● Defining a broad spectrum of behavior or types? Consider an interface.
● You can extend only one class, but implement one or more interfaces.
Android Development with Kotlin This work is licensed under the Apache 2 license. 32
Extension functions
Android Development with Kotlin This work is licensed under the Apache 2 license. 33
Extension functions
Add functions to an existing class that you cannot modify directly.
Format:
Android Development with Kotlin This work is licensed under the Apache 2 license. 34
Why use extension functions?
● Separate out core API from helper methods for classes you
own
Define extension functions in an easily discoverable place such as in the same file
as the class, or a well-named function.
Android Development with Kotlin This work is licensed under the Apache 2 license. 35
Extension function example
Add isOdd() to Int class:
Android Development with Kotlin This work is licensed under the Apache 2 license. 36
Special classes
Android Development with Kotlin This work is licensed under the Apache 2 license. 37
Data class
● Special class that exists just to store a set of data
● Generates getters for each property (and setters for vars too)
Format:
Android Development with Kotlin This work is licensed under the Apache 2 license. 38
Data class example
Define the data class:
Android Development with Kotlin This work is licensed under the Apache 2 license. 39
Pair and Triple
Android Development with Kotlin This work is licensed under the Apache 2 license. 40
Pair and Triple examples
Android Development with Kotlin This work is licensed under the Apache 2 license. 41
Pair
Pair's special to variant lets you omit parentheses and periods (infix function).
Android Development with Kotlin This work is licensed under the Apache 2 license. 42
Enum class
User-defined data type for a set of named values
Format: …
Referenced via
Android Development with Kotlin This work is licensed under the Apache 2 license. 43
Enum class example
Define an enum with red, green, and blue colors.
Android Development with Kotlin This work is licensed under the Apache 2 license. 44
Object/singleton
Android Development with Kotlin This work is licensed under the Apache 2 license. 45
Object/singleton example
Android Development with Kotlin This work is licensed under the Apache 2 license. 46
Companion objects
Android Development with Kotlin This work is licensed under the Apache 2 license. 47
Companion object example
Android Development with Kotlin This work is licensed under the Apache 2 license. 48
Organizing your code
Android Development with Kotlin This work is licensed under the Apache 2 license. 49
Single file, multiple entities
● You can and should group related structures in the same file
Android Development with Kotlin This work is licensed under the Apache 2 license. 50
Packages
package org.example.game
Android Development with Kotlin This work is licensed under the Apache 2 license. 51
Example class hierarchy
org.example.vehicle
Vehicle
org.example.vehicle.moped org.example.vehicle.car
Moped Car
Moped50cc Sedan
Moped100cc Hatchback
Android Development with Kotlin This work is licensed under the Apache 2 license. 52
Visibility modifiers
Use visibility modifiers to limit what information you expose.
● private means it will only be visible in that class (or source file if you are
working with functions).
● protected is the same as private, but it will also be visible to any
subclasses.
Android Development with Kotlin This work is licensed under the Apache 2 license. 53
Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 54
Summary
In Lesson 3, you learned about:
● Classes, constructors, and getters and setters
● Inheritance, interfaces, and how to extend classes
● Extension functions
● Special classes: data classes, enums, object/singletons, companion
objects
● Packages
● Visibility modifiers
Android Development with Kotlin This work is licensed under the Apache 2 license. 55
Pathway
Android Development with Kotlin This work is licensed under the Apache 2 license. 56
Lesson 4:
Build your first
Android app
Android Development with Kotlin v1.0 This work is licensed under the Apache 2 license. 1
About this lesson
Lesson 4: Build your first Android app
● Your first app
● Anatomy of an Android app
● Layouts and resources in Android
● Activities
● Make an app interactive
● Gradle: Building an Android app
● Accessibility
● Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 2
Android Studio
Official IDE for building Android apps
Android Development with Kotlin This work is licensed under the Apache 2 license. 3
Your first app
Android Development with Kotlin This work is licensed under the Apache 2 license. 4
Open Android Studio
Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Create new project
Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Enter your project details
Android Development with Kotlin This work is licensed under the Apache 2 license. 7
Android releases and API levels
Android Development with Kotlin This work is licensed under the Apache 2 license. 8
Choose API levels for your app
The API level identifies the framework API version of the Android SDK.
Android Development with Kotlin This work is licensed under the Apache 2 license. 9
Tour of Android Studio
Android Development with Kotlin This work is licensed under the Apache 2 license. 10
Run your app
Android Development with Kotlin This work is licensed under the Apache 2 license. 11
Android Virtual Device (AVD) Manager
Android Development with Kotlin This work is licensed under the Apache 2 license. 12
Anatomy of an Android App
project
Android Development with Kotlin This work is licensed under the Apache 2 license. 13
Anatomy of a basic app project
● Activity
● Resources (layout files, images, audio files, themes, and colors)
● Gradle files
Android Development with Kotlin This work is licensed under the Apache 2 license. 14
Android app project structure
├──
│ ├──
│ └──
│ ├──
│ ├──
│ │ ├──
│ │ ├──
│ │ └──
│ └──
├──
└──
Android Development with Kotlin This work is licensed under the Apache 2 license. 15
Browse files in Android Studio
Android Development with Kotlin This work is licensed under the Apache 2 license. 16
Layouts and resources in
Android
Android Development with Kotlin This work is licensed under the Apache 2 license. 17
Views
Android Development with Kotlin This work is licensed under the Apache 2 license. 18
Layout Editor
Android Development with Kotlin This work is licensed under the Apache 2 license. 19
XML Layouts
Android Development with Kotlin This work is licensed under the Apache 2 license. 20
XML for a TextView
Hello World!
Android Development with Kotlin This work is licensed under the Apache 2 license. 21
Size of a View
● wrap_content
android:layout_width="wrap_content"
● match_parent
android:layout_width="match_parent"
● Fixed value (use dp units)
android:layout_width="48dp"
Android Development with Kotlin This work is licensed under the Apache 2 license. 22
ViewGroups
A ViewGroup is a container that determines how views are displayed.
TextView
TextView TextView
TextView TextView
Button
Button
The ViewGroup is the parent and the views inside it are its children.
Android Development with Kotlin This work is licensed under the Apache 2 license. 23
FrameLayout example
A FrameLayout generally holds a single child View.
TextView
Android Development with Kotlin This work is licensed under the Apache 2 license. 24
LinearLayout example
● Aligns child views in a row or column
● Set android:orientation to horizontal or vertical
TextView
TextView
Button
Android Development with Kotlin This work is licensed under the Apache 2 license. 25
View hierarchy
LinearLayout
ImageView
Button Button
Button Button
Android Development with Kotlin This work is licensed under the Apache 2 license. 26
App resources
Static content or additional files that your code uses
● Layout files
● Images
● Audio files
● User interface strings
● App icon
Android Development with Kotlin This work is licensed under the Apache 2 license. 27
Common resource directories
Add resources to your app by including them in the appropriate resource directory
under the parent res folder.
├──
└──
├──
├──
├──
└──
Android Development with Kotlin This work is licensed under the Apache 2 license. 28
Resource IDs
● Each resource has a resource ID to access it.
● When naming resources, the convention is to use all lowercase with
underscores (for example, activity_main.xml).
● Android autogenerates a class file named R.java with references to all
resources in the app.
● Individual items are referenced with:
R.<resource_type>.<resource_name>
Examples: R.drawable.ic_launcher (res/drawable/ic_launcher.xml)
R.layout.activity_main (res/layout/activity_main.xml)
Android Development with Kotlin This work is licensed under the Apache 2 license. 29
Resource IDs for views
Individual views can also have resource IDs.
Add the android:id attribute to the View in XML. Use @+id/name syntax.
Within your app, you can now refer to this specific TextView using:
R.id.helloTextView
Android Development with Kotlin This work is licensed under the Apache 2 license. 30
Activities
Android Development with Kotlin This work is licensed under the Apache 2 license. 31
What’s an Activity?
● An Activity is a means for the user to
accomplish one main goal.
● An Android app is composed of one or more
activities.
Android Development with Kotlin This work is licensed under the Apache 2 license. 32
MainActivity.kt
Android Development with Kotlin This work is licensed under the Apache 2 license. 33
How an Activity runs
Activity launched
App is running
Android Development with Kotlin This work is licensed under the Apache 2 license. 34
Implement the onCreate() callback
Android Development with Kotlin This work is licensed under the Apache 2 license. 35
Layout inflation
Activity
ViewGroup
Layout files
LayoutInflater View1
layout1 layout2 layout3 ViewGroup
View2 View3
Android Development with Kotlin This work is licensed under the Apache 2 license. 36
Make an app interactive
Android Development with Kotlin This work is licensed under the Apache 2 license. 37
Define app behavior in Activity
Modify the Activity so the app responds to user input, such as a button tap.
Android Development with Kotlin This work is licensed under the Apache 2 license. 38
Modify a View dynamically
Within MainActivity.kt:
Android Development with Kotlin This work is licensed under the Apache 2 license. 39
Set up listeners for specific events
User interacts with a View
An event is fired
No Yes
Android Development with Kotlin This work is licensed under the Apache 2 license. 40
View.OnClickListener
Android Development with Kotlin This work is licensed under the Apache 2 license. 41
SAM (single abstract method)
Converts a function into an implementation of an interface
Format: InterfaceName { lambda body }
is equivalent to
Android Development with Kotlin This work is licensed under the Apache 2 license. 42
View.OnClickListener as a SAM
A more concise way to declare a click listener
Android Development with Kotlin This work is licensed under the Apache 2 license. 43
Late initialization
Android Development with Kotlin This work is licensed under the Apache 2 license. 44
Lateinit example in Activity
Android Development with Kotlin This work is licensed under the Apache 2 license. 45
Gradle: Building an
Android app
Android Development with Kotlin This work is licensed under the Apache 2 license. 46
What is Gradle?
● Builds automation system
● Manages the build cycle via a series of tasks (for example,
compiles Kotlin sources, runs tests, installs app to device)
● Determines the proper order of tasks to run
● Manages dependencies between projects and third-party
libraries
Android Development with Kotlin This work is licensed under the Apache 2 license. 47
Gradle build file
● Declare plugins
● Define Android properties
● Handle dependencies
● Connect to repositories
Android Development with Kotlin This work is licensed under the Apache 2 license. 48
Plugins
Android Development with Kotlin This work is licensed under the Apache 2 license. 49
Android configuration
Android Development with Kotlin This work is licensed under the Apache 2 license. 50
Dependencies
Android Development with Kotlin This work is licensed under the Apache 2 license. 51
Repositories
Android Development with Kotlin This work is licensed under the Apache 2 license. 52
Common Gradle tasks
● Clean
● Tasks
● InstallDebug
Android Development with Kotlin This work is licensed under the Apache 2 license. 53
Accessibility
Android Development with Kotlin This work is licensed under the Apache 2 license. 54
Accessibility
Android Development with Kotlin This work is licensed under the Apache 2 license. 55
Make apps more accessible
● Increase text visibility with foreground and background color
contrast ratio:
○ At least 4.5:1 for small text against the background
○ At least 3.0:1 for large text against the background
● Use large, simple controls
○ Touch target size should be at least 48dp x 48dp
● Describe each UI element
○ Set content description on images and controls
Android Development with Kotlin This work is licensed under the Apache 2 license.
Accessibility Scanner
Android Development with Kotlin This work is licensed under the Apache 2 license. 57
Accessibility Scanner example
Android Development with Kotlin This work is licensed under the Apache 2 license. 58
Add content labels
● Set contentDescription attribute → read aloud by screen
reader
Android Development with Kotlin This work is licensed under the Apache 2 license. 59
No content label needed
Android Development with Kotlin This work is licensed under the Apache 2 license. 60
TalkBack
Android Development with Kotlin This work is licensed under the Apache 2 license. 61
TalkBack example
Reads text
aloud as user
navigates the
screen
Android Development with Kotlin This work is licensed under the Apache 2 license. 62
Switch access
Android Development with Kotlin This work is licensed under the Apache 2 license. 63
Android Accessibility Suite
Collection of accessibility apps that help you use
your Android device eyes-free, or with a switch
device. It includes:
Android Development with Kotlin This work is licensed under the Apache 2 license. 64
Accessibility Resources
Android Development with Kotlin This work is licensed under the Apache 2 license. 65
Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 66
Summary
In Lesson 4, you learned how to:
● Use Views and ViewGroups to build the user interface of your app
● Access resources in your app from
R.<resource_type>.<resource_name>
● Define app behavior in the Activity (for example, register
OnClickListener)
● Use Gradle as the build system to build your app
● Follow best practices to make your apps more accessible
Android Development with Kotlin This work is licensed under the Apache 2 license. 67
Learn more
● Layouts
● LinearLayout
● Input events overview
● View
● ViewGroup
Android Development with Kotlin This work is licensed under the Apache 2 license. 68
Pathway
Android Development with Kotlin This work is licensed under the Apache 2 license. 69
Lesson 5:
Layouts
Android Development with Kotlin v1.0 This work is licensed under the Apache 2 license. 1
About this lesson
Lesson 5: Layouts
● Layouts in Android
● ConstraintLayout
● Additional topics for ConstraintLayout
● Data binding
● Displaying lists with RecyclerView
● Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 2
Layouts in Android
Android Development with Kotlin This work is licensed under the Apache 2 license. 3
Android devices
● Android devices come in many
different form factors.
● More and more pixels per inch
are being packed into device
screens.
● Developers need the ability to
specify layout dimensions that
are consistent across devices.
Android Development with Kotlin This work is licensed under the Apache 2 license. 4
Density-independent pixels (dp)
Use dp when specifying sizes in your layout, such as the width or height of views.
density-independent pixels.
● dp = (width in pixels * 160)
160dp
screen density
Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Screen-density buckets
Density qualifier Description DPI estimate
Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Android View rendering cycle
Measure
Layout
Draw
Android Development with Kotlin This work is licensed under the Apache 2 license. 7
Drawing region
What we see:
Android Development with Kotlin This work is licensed under the Apache 2 license. 8
View margins and padding
View View
View
Android Development with Kotlin This work is licensed under the Apache 2 license. 9
ConstraintLayout
Android Development with Kotlin This work is licensed under the Apache 2 license. 10
Deeply nested layouts are costly
Android Development with Kotlin This work is licensed under the Apache 2 license. 11
What is ConstraintLayout?
Android Development with Kotlin This work is licensed under the Apache 2 license. 12
What is a constraint?
Android Development with Kotlin This work is licensed under the Apache 2 license. 13
Relative positioning constraints
Can set up a constraint relative to the parent container
Format: layout_constraint<SourceConstraint>_to<TargetConstraint>Of
Android Development with Kotlin This work is licensed under the Apache 2 license. 14
Relative positioning constraints
Top
Hello!
Baseline
Bottom
Android Development with Kotlin This work is licensed under the Apache 2 license. 15
Relative positioning constraints
Start End
Android Development with Kotlin This work is licensed under the Apache 2 license. 16
Simple ConstraintLayout example
Android Development with Kotlin This work is licensed under the Apache 2 license. 17
Layout Editor in Android Studio
You can click and drag to add constraints to a View.
Android Development with Kotlin This work is licensed under the Apache 2 license. 18
Constraint Widget in Layout Editor
Fixed
Wrap content
Match constraints
Android Development with Kotlin This work is licensed under the Apache 2 license. 19
Wrap content for width and height
Android Development with Kotlin This work is licensed under the Apache 2 license. 20
Wrap content for width, fixed height
Android Development with Kotlin This work is licensed under the Apache 2 license. 21
Center a view horizontally
Android Development with Kotlin This work is licensed under the Apache 2 license. 22
Use match_constraint
Can’t use match_parent on a child view, use match_constraint instead
Android Development with Kotlin This work is licensed under the Apache 2 license. 23
Chains
Android Development with Kotlin This work is licensed under the Apache 2 license. 24
Create a Chain in Layout Editor
Android Development with Kotlin This work is licensed under the Apache 2 license. 25
Chain styles
Adjust space between views with these different chain styles.
Spread Chain
Weighted Chain
Packed Chain
Android Development with Kotlin This work is licensed under the Apache 2 license. 26
Additional topics for
ConstraintLayout
Android Development with Kotlin This work is licensed under the Apache 2 license. 27
Guidelines
Android Development with Kotlin This work is licensed under the Apache 2 license. 28
Guidelines in Android Studio
Android Development with Kotlin This work is licensed under the Apache 2 license. 29
Example Guideline
Android Development with Kotlin This work is licensed under the Apache 2 license. 30
Creating Guidelines
●
●
●
Android Development with Kotlin This work is licensed under the Apache 2 license. 31
Groups
Android Development with Kotlin This work is licensed under the Apache 2 license. 32
Example group
Android Development with Kotlin This work is licensed under the Apache 2 license. 33
Groups app code
Android Development with Kotlin This work is licensed under the Apache 2 license. 34
Data binding
Android Development with Kotlin This work is licensed under the Apache 2 license. 35
Current approach: findViewById()
Traverses the View hierarchy each time
MainActivity.kt activity_main.xml
…
…
…
…
Android Development with Kotlin This work is licensed under the Apache 2 license. 36
Use data binding instead
Bind UI components in your layouts to data sources in your app.
MainActivity.kt activity_main.xml
initialize binding
…
Val binding:ActivityMainBinding
binding.name.text = …
binding.age.text = …
binding.loc.text = …
Android Development with Kotlin This work is licensed under the Apache 2 license. 37
Modify build.gradle file
Android Development with Kotlin This work is licensed under the Apache 2 license. 38
Add layout tag
Android Development with Kotlin This work is licensed under the Apache 2 license. 39
Layout inflation with data binding
Replace this
with this
Android Development with Kotlin This work is licensed under the Apache 2 license. 40
Data binding layout variables
In MainActivity.kt:
Android Development with Kotlin This work is licensed under the Apache 2 license. 41
Data binding layout expressions
Android Development with Kotlin This work is licensed under the Apache 2 license. 42
Displaying lists with
RecyclerView
Android Development with Kotlin This work is licensed under the Apache 2 license. 43
RecyclerView
Android Development with Kotlin This work is licensed under the Apache 2 license. 44
RecyclerView.Adapter
Android Development with Kotlin This work is licensed under the Apache 2 license. 45
View recycling in RecyclerView
Boston, Massachusetts If item is scrolled
offscreen, it isn’t
Chicago, Illinois
destroyed. Item is put in
Mountain View, California a pool to be recycled.
Miami, Florida
Seattle, Washington
Reno, Nevada
onBindViewHolder binds
Nashville, Tennessee the view with the new
values, and then the view
Little Rock, Arkansas gets reinserted in the list.
Android Development with Kotlin This work is licensed under the Apache 2 license. 46
Add RecyclerView to your layout
Android Development with Kotlin This work is licensed under the Apache 2 license. 47
Create a list item layout
res/layout/item_view.xml
Android Development with Kotlin This work is licensed under the Apache 2 license. 48
Create a list adapter
Android Development with Kotlin This work is licensed under the Apache 2 license. 49
Set the adapter on the RecyclerView
In MainActivity.kt:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Android Development with Kotlin This work is licensed under the Apache 2 license. 50
Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 51
Summary
In Lesson 5, you learned how to:
● Specify lengths in dp for your layout
● Work with screen densities for different Android devices
● Render Views to the screen of your app
● Layout views within a ConstraintLayout using constraints
● Simplify getting View references from layout with data binding
● Display a list of text items using a RecyclerView and custom adapter
Android Development with Kotlin This work is licensed under the Apache 2 license. 52
Learn more
Android Development with Kotlin This work is licensed under the Apache 2 license. 53
Pathway
Android Development with Kotlin This work is licensed under the Apache 2 license. 54
Lesson 6:
App navigation
Android Development with Kotlin v1.0 This work is licensed under the Apache 2 license. 1
About this lesson
Lesson 6: App navigation
● Multiple activities and intents
● App bar, navigation drawer, and menus
● Fragments
● Navigation in an app
● More custom navigation behavior
● Navigation UI
● Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 2
Multiple activities and
intents
Android Development with Kotlin This work is licensed under the Apache 2 license. 3
Multiple screens in an app
Sometimes app functionality may be separated into multiple screens.
Examples:
● View details of a single item (for example, product in a shopping app)
● Create a new item (for example, new email)
● Show settings for an app
● Access services in other apps (for example, photo gallery or browse
documents)
Android Development with Kotlin This work is licensed under the Apache 2 license. 4
Intent
Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Explicit intent
Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Explicit intent examples
Navigate between activities in your app:
Android Development with Kotlin This work is licensed under the Apache 2 license. 7
Implicit intent
Android Development with Kotlin This work is licensed under the Apache 2 license. 8
Implicit intent example
Android Development with Kotlin This work is licensed under the Apache 2 license. 9
App bar, navigation drawer,
and menus
Android Development with Kotlin This work is licensed under the Apache 2 license. 10
App bar
Android Development with Kotlin This work is licensed under the Apache 2 license. 11
Navigation drawer
Android Development with Kotlin This work is licensed under the Apache 2 license. 12
Menu
Android Development with Kotlin This work is licensed under the Apache 2 license. 13
More menu options
Android Development with Kotlin This work is licensed under the Apache 2 license. 14
Options menu example
Android Development with Kotlin This work is licensed under the Apache 2 license. 15
Inflate options menu
Android Development with Kotlin This work is licensed under the Apache 2 license. 16
Handle menu options selected
Android Development with Kotlin This work is licensed under the Apache 2 license. 17
Fragments
Android Development with Kotlin This work is licensed under the Apache 2 license. 18
Fragments for tablet layouts
Android Development with Kotlin This work is licensed under the Apache 2 license. 19
Fragment
Android Development with Kotlin This work is licensed under the Apache 2 license. 20
Note about fragments
Android Development with Kotlin This work is licensed under the Apache 2 license. 21
Navigation within an app
Android Development with Kotlin This work is licensed under the Apache 2 license. 22
Navigation component
● Collection of libraries and tooling, including an integrated editor, for
creating navigation paths through an app
● Assumes one Activity per graph with many Fragment
destinations
● Consists of three major parts:
○ Navigation graph
○ Navigation Host (NavHost)
○ Navigation Controller (NavController)
Android Development with Kotlin This work is licensed under the Apache 2 license. 23
Add dependencies
Android Development with Kotlin This work is licensed under the Apache 2 license. 24
Navigation host (NavHost)
Android Development with Kotlin This work is licensed under the Apache 2 license. 25
Navigation graph
New resource type located in res/navigation
directory
● XML file containing all of your navigation
destinations and actions
● Lists all the (Fragment/Activity) destinations that
can be navigated to
● Lists the associated actions to traverse between
them
● Optionally lists animations for entering or exiting
Android Development with Kotlin This work is licensed under the Apache 2 license. 26
Navigation Editor in Android Studio
Android Development with Kotlin This work is licensed under the Apache 2 license. 27
Creating a Fragment
● Extend Fragment class
● Override onCreateView()
● Inflate a layout for the Fragment that you have defined in XML
Android Development with Kotlin This work is licensed under the Apache 2 license. 28
Specifying Fragment destinations
Android Development with Kotlin This work is licensed under the Apache 2 license. 29
Example fragment destination
Android Development with Kotlin This work is licensed under the Apache 2 license. 30
Navigation Controller (NavController)
Android Development with Kotlin This work is licensed under the Apache 2 license. 31
Example NavController
Android Development with Kotlin This work is licensed under the Apache 2 license. 32
More custom navigation
behavior
Android Development with Kotlin This work is licensed under the Apache 2 license. 33
Passing data between destinations
Using Safe Args:
● Ensures arguments have a valid type
● Lets you provide default values
● Generates a <SourceDestination>Directions class with methods for
every action in that destination
● Generates a class to set arguments for every named action
● Generates a <TargetDestination>Args class providing access to the
destination's arguments
Android Development with Kotlin This work is licensed under the Apache 2 license. 34
Setting up Safe Args
In the project build.gradle file:
Android Development with Kotlin This work is licensed under the Apache 2 license. 35
Sending data to a Fragment
Android Development with Kotlin This work is licensed under the Apache 2 license. 36
Destination arguments
Android Development with Kotlin This work is licensed under the Apache 2 license. 37
Supported argument types
Type Type Syntax Supports Default Supports Null
app:argType=<type> Values Values
Integer "integer" Yes No
Float "float" Yes No
Long "long" Yes No
Boolean "boolean" Yes ("true" or "false") No
String "string" Yes Yes
Array above type + "[]" Yes (only "@null") Yes
(for example, "string[]" "long[]")
Enum Fully qualified name of the enum Yes No
Resource reference "reference" Yes No
Android Development with Kotlin This work is licensed under the Apache 2 license. 38
Supported argument types: Custom classes
Android Development with Kotlin This work is licensed under the Apache 2 license. 39
Create action from source to destination
In nav_graph.xml:
Android Development with Kotlin This work is licensed under the Apache 2 license. 40
Navigating with actions
In InputFragment.kt:
Android Development with Kotlin This work is licensed under the Apache 2 license. 41
Retrieving Fragment arguments
Android Development with Kotlin This work is licensed under the Apache 2 license. 42
Navigation UI
Android Development with Kotlin This work is licensed under the Apache 2 license. 43
Menus revisited
Android Development with Kotlin This work is licensed under the Apache 2 license. 44
DrawerLayout for navigation drawer
Android Development with Kotlin This work is licensed under the Apache 2 license. 45
Finish setting up navigation drawer
Connect DrawerLayout to navigation graph:
Android Development with Kotlin This work is licensed under the Apache 2 license. 46
Understanding the back stack
State 1 State 2 State 3
Activity 3
Android Development with Kotlin This work is licensed under the Apache 2 license.
Fragments and the back stack
State 1 State 2 State 3
Fragment 2
Android Development with Kotlin This work is licensed under the Apache 2 license.
Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 49
Summary
In Lesson 6, you learned how to:
● Use explicit and implicit intents to navigate between Activities
● Structure apps using fragments instead of putting all UI code in the
Activity
● Handle navigation with NavGraph, NavHost, and NavController
● Use Safe Args to pass data between fragment destinations
● Use NavigationUI to hook up top app bar, navigation drawer, and bottom
navigation
● Android keeps a back stack of all the destinations you’ve visited, with
each new destination being pushed onto the stack.
Android Development with Kotlin This work is licensed under the Apache 2 license. 50
Learn more
● Principles of navigation
● Navigation component
● Pass data between destinations
● NavigationUI
Android Development with Kotlin This work is licensed under the Apache 2 license. 51
Pathway
Android Development with Kotlin This work is licensed under the Apache 2 license. 52
Lesson 7:
Activity and
fragment lifecycles
Android Development with Kotlin v1.0 This work is licensed under the Apache 2 license. 1
About this lesson
Lesson 7: Activity and fragment lifecycles
● Activity lifecycle
● Logging
● Fragment lifecycle
● Lifecycle-aware components
● Tasks and back stack
● Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 2
Activity lifecycle
Android Development with Kotlin This work is licensed under the Apache 2 license. 3
Why it matters
Android Development with Kotlin This work is licensed under the Apache 2 license. 4
Simplified activity lifecycle
Activity launched
App is running
Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Activity lifecycle
Activity launched
Activity running
Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Activity states
CREATED
STARTED
RESUMED
Activity is running
PAUSED
STOPPED
DESTROYED
Android Development with Kotlin This work is licensed under the Apache 2 license. 7
onCreate()
Android Development with Kotlin This work is licensed under the Apache 2 license. 8
onStart()
Android Development with Kotlin This work is licensed under the Apache 2 license. 9
onResume()
Android Development with Kotlin This work is licensed under the Apache 2 license. 10
onPause()
Android Development with Kotlin This work is licensed under the Apache 2 license. 11
onStop()
Android Development with Kotlin This work is licensed under the Apache 2 license. 12
onDestroy()
Android Development with Kotlin This work is licensed under the Apache 2 license. 13
Summary of activity states
State Callbacks Description
Android Development with Kotlin This work is licensed under the Apache 2 license. 14
Save state
User expects UI state to stay the same after a config change or if the app
is terminated when in the background.
● Activity is destroyed and restarted, or app is terminated and activity is
started.
● Store user data needed to reconstruct app and activity Lifecycle
changes:
○ Use Bundle provided by onSaveInstanceState().
○ onCreate() receives the Bundle as an argument when activity
is created again.
Android Development with Kotlin This work is licensed under the Apache 2 license. 15
Logging
Android Development with Kotlin This work is licensed under the Apache 2 license. 16
Logging in Android
● Monitor the flow of events or state of your app.
● Use the built-in Log class or third-party library.
● Example Log method call: Log.d(TAG, "Message")
Android Development with Kotlin This work is licensed under the Apache 2 license. 17
Write logs
Android Development with Kotlin This work is licensed under the Apache 2 license. 18
Fragment lifecycle
Android Development with Kotlin This work is licensed under the Apache 2 license. 19
Fragment states
CREATED
STARTED
RESUMED
Fragment is running
PAUSED
STOPPED
DESTROYED
Android Development with Kotlin This work is licensed under the Apache 2 license. 20
Fragment lifecycle diagram
Fragment is
()
added
Fragment is Fragment is
active destroyed
Android Development with Kotlin This work is licensed under the Apache 2 license. 21
onAttach()
Android Development with Kotlin This work is licensed under the Apache 2 license. 22
onCreateView()
Android Development with Kotlin This work is licensed under the Apache 2 license. 23
onViewCreated()
Android Development with Kotlin This work is licensed under the Apache 2 license. 24
onDestroyView() and onDetach()
Android Development with Kotlin This work is licensed under the Apache 2 license. 25
Summary of fragment states
State Callbacks Description
Android Development with Kotlin This work is licensed under the Apache 2 license. 26
Save fragment state across config changes
Android Development with Kotlin This work is licensed under the Apache 2 license. 27
Lifecycle-aware
components
Android Development with Kotlin This work is licensed under the Apache 2 license. 28
Lifecycle-aware components
Android Development with Kotlin This work is licensed under the Apache 2 license. 29
LifecycleOwner
Android Development with Kotlin This work is licensed under the Apache 2 license. 30
LifecycleObserver
Implement LifecycleObserver interface:
Android Development with Kotlin This work is licensed under the Apache 2 license. 31
Tasks and back stack
Android Development with Kotlin This work is licensed under the Apache 2 license. 32
Back stack of activities
EmailActivity
Back stack
Android Development with Kotlin This work is licensed under the Apache 2 license. 33
Add to the back stack
ComposeActivity
EmailActivity
Back stack
Android Development with Kotlin This work is licensed under the Apache 2 license. 34
Add to the back stack again
AttachFileActivity
ComposeActivity
EmailActivity
Back stack
Android Development with Kotlin This work is licensed under the Apache 2 license. 35
Tap Back button
AttachFileActivity
ComposeActivity
EmailActivity
Back stack
Android Development with Kotlin This work is licensed under the Apache 2 license. 36
Tap Back button again
ComposeActivity
EmailActivity
Back stack
Android Development with Kotlin This work is licensed under the Apache 2 license. 37
First destination in the back stack
First
fragment
FirstFragment
Back stack
Android Development with Kotlin This work is licensed under the Apache 2 license. 38
Add a destination to the back stack
Second
fragment
SecondFragment
FirstFragment
Back stack
Android Development with Kotlin This work is licensed under the Apache 2 license. 39
Tap Back button
SecondFragment
First
fragment popped off the stack
FirstFragment
Back stack
Android Development with Kotlin This work is licensed under the Apache 2 license. 40
Another back stack example
ResultFragment
Question3Fragment
Result Question2Fragment
fragment
Question1Fragment
WelcomeFragment
Back stack
Android Development with Kotlin This work is licensed under the Apache 2 license. 41
Modify Back button behavior
ResultFragment
WelcomeFragment
Back stack
Android Development with Kotlin This work is licensed under the Apache 2 license. 42
Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 43
Summary
In Lesson 7, you learned how to:
● Understand how an activity instance transitions through different lifecycle
states as the user interacts with or leaves your app
● Reserve UI state across configuration changes using a Bundle
● Fragment lifecycle callback methods similar to activity, but with additions
● Use lifecycle-aware components help organize your app code
● Use default or custom back stack behavior
● Use logging to help debug and track the state of the app
Android Development with Kotlin This work is licensed under the Apache 2 license. 44
Learn more
Android Development with Kotlin This work is licensed under the Apache 2 license. 45
Pathway
Android Development with Kotlin This work is licensed under the Apache 2 license. 46
Lesson 8:
App architecture
(UI layer)
Android Development with Kotlin v1.0 This work is licensed under the Apache 2 license. 1
About this lesson
Lesson 8: App architecture (UI layer)
● Android app architecture
● ViewModel
● Data binding
● LiveData
● Transform LiveData
● Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 2
Android app architecture
Android Development with Kotlin This work is licensed under the Apache 2 license. 3
Avoid short-term hacks
Android Development with Kotlin This work is licensed under the Apache 2 license. 4
Examples of short-term hacks
Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Why you need good app architecture
Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Android Jetpack
Android Development with Kotlin This work is licensed under the Apache 2 license. 7
Separation of concerns
res/layout
UI Controller
(Activity/Fragment) ViewModel
LiveData
Data Layer
Android Development with Kotlin This work is licensed under the Apache 2 license. 8
Architecture components
Android Development with Kotlin This work is licensed under the Apache 2 license. 9
ViewModel
Android Development with Kotlin This work is licensed under the Apache 2 license. 10
Gradle: lifecycle extensions
In app/build.gradle file:
Android Development with Kotlin This work is licensed under the Apache 2 license. 11
ViewModel
Android Development with Kotlin This work is licensed under the Apache 2 license. 12
Lifetime of a ViewModel
Android Development with Kotlin This work is licensed under the Apache 2 license. 13
Kabaddi Kounter
Android Development with Kotlin This work is licensed under the Apache 2 license. 14
ViewModel class
abstract class ViewModel
Android Development with Kotlin This work is licensed under the Apache 2 license. 15
Implement a ViewModel
Android Development with Kotlin This work is licensed under the Apache 2 license. 16
Load and use a ViewModel
Android Development with Kotlin This work is licensed under the Apache 2 license. 17
Using a ViewModel
Android Development with Kotlin This work is licensed under the Apache 2 license. 18
Data binding
Android Development with Kotlin This work is licensed under the Apache 2 license. 19
ViewModels and data binding
● App architecture without data binding
UI Controller
Views
ViewModel (activity/fragment with
(defined in XML layout)
click listeners)
Views
ViewModel (defined in XML layout)
Android Development with Kotlin This work is licensed under the Apache 2 license. 20
Data binding in XML revisited
Android Development with Kotlin This work is licensed under the Apache 2 license. 21
Attaching a ViewModel to a data binding
Android Development with Kotlin This work is licensed under the Apache 2 license. 22
Using a ViewModel from a data binding
In activity_main.xml:
Android Development with Kotlin This work is licensed under the Apache 2 license. 23
ViewModels and data binding
Android Development with Kotlin This work is licensed under the Apache 2 license. 24
LiveData
Android Development with Kotlin This work is licensed under the Apache 2 license. 25
Observer design pattern
Android Development with Kotlin This work is licensed under the Apache 2 license. 26
Observer design pattern diagram
Observable
Observer Observer
Android Development with Kotlin This work is licensed under the Apache 2 license. 27
LiveData
● A lifecycle-aware data holder that can be observed
● Wrapper that can be used with any data including lists
(for example, LiveData<Int> holds an Int)
● Often used by ViewModels to hold individual data fields
● Observers (activity or fragment) can be added or removed
○ observe(owner: LifecycleOwner, observer: Observer)
removeObserver(observer: Observer)
Android Development with Kotlin This work is licensed under the Apache 2 license. 28
LiveData versus MutableLiveData
LiveData<T> MutableLiveData<T>
● getValue() ● getValue()
● postValue(value: T)
● setValue(value: T)
Android Development with Kotlin This work is licensed under the Apache 2 license. 29
Use LiveData in ViewModel
Android Development with Kotlin This work is licensed under the Apache 2 license. 30
Add an observer on LiveData
Set up click listener to increment ViewModel score:
Android Development with Kotlin This work is licensed under the Apache 2 license. 31
Two-way data binding
Android Development with Kotlin This work is licensed under the Apache 2 license. 32
Example layout XML
Android Development with Kotlin This work is licensed under the Apache 2 license. 33
Example Activity
Android Development with Kotlin This work is licensed under the Apache 2 license. 34
Example ViewModel
Android Development with Kotlin This work is licensed under the Apache 2 license. 35
Transform LiveData
Android Development with Kotlin This work is licensed under the Apache 2 license. 36
Manipulating LiveData with transformations
Android Development with Kotlin This work is licensed under the Apache 2 license. 37
Example LiveData with transformations
Android Development with Kotlin This work is licensed under the Apache 2 license. 38
Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 39
Summary
In Lesson 8, you learned how to:
● Follow good app architecture design, and the separation-of-concerns
principle to make apps more maintainable and reduce technical debt
● Create a ViewModel to hold data separately from a UI controller
● Use ViewModel with data binding to make a responsive UI with less
code
● Use observers to automatically get updates from LiveData
Android Development with Kotlin This work is licensed under the Apache 2 license. 40
Learn More
Android Development with Kotlin This work is licensed under the Apache 2 license. 41
Pathway
Android Development with Kotlin This work is licensed under the Apache 2 license. 42
Lesson 9:
App architecture
(persistence)
Android Development with Kotlin v1.0 This work is licensed under the Apache 2 license. 1
About this lesson
Lesson 9: App architecture (persistence)
● Storing data
● Room persistence library
● Asynchronous programming
● Coroutines
● Testing databases
● Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 2
Storing data
Android Development with Kotlin This work is licensed under the Apache 2 license. 3
Ways to store data in an Android app
● App-specific storage
● Shared storage (files to be shared with other apps)
● Preferences
● Databases
Android Development with Kotlin This work is licensed under the Apache 2 license. 4
What is a database?
Collection of structured data that can be easily accessed,
searched, and organized, consisting of:
person car
● Rows
_id _id
● Columns name make
age model
email year
5
Android Development with Kotlin This work is licensed under the Apache 2 license.
Structured Query Language (SQL)
Use SQL to access and modify a relational database.
Android Development with Kotlin This work is licensed under the Apache 2 license. 6
SQLite in Android
Store data
Your app
SQLite database
Android Development with Kotlin This work is licensed under the Apache 2 license. 7
Example SQLite commands
Android Development with Kotlin This work is licensed under the Apache 2 license. 8
Interacting directly with a database
Android Development with Kotlin This work is licensed under the Apache 2 license. 9
Room persistence library
Android Development with Kotlin This work is licensed under the Apache 2 license. 10
Add Gradle dependencies
Android Development with Kotlin This work is licensed under the Apache 2 license. 11
Room
Color("#FF0000", "red")
Room
Android Development with Kotlin This work is licensed under the Apache 2 license. 12
ColorValue app
Android Development with Kotlin This work is licensed under the Apache 2 license. 13
Room
● Entity Color
● DAO ColorDao
● Database ColorDatabase
Android Development with Kotlin This work is licensed under the Apache 2 license. 14
Color class
Android Development with Kotlin This work is licensed under the Apache 2 license. 15
Annotations
Android Development with Kotlin This work is licensed under the Apache 2 license. 16
Entity
Android Development with Kotlin This work is licensed under the Apache 2 license. 17
Example entity
colors
Android Development with Kotlin This work is licensed under the Apache 2 license. 18
Data access object (DAO)
Android Development with Kotlin This work is licensed under the Apache 2 license. 19
Example DAO
Android Development with Kotlin This work is licensed under the Apache 2 license. 20
Query
Android Development with Kotlin This work is licensed under the Apache 2 license. 21
Insert
Android Development with Kotlin This work is licensed under the Apache 2 license. 22
Update
Android Development with Kotlin This work is licensed under the Apache 2 license. 23
Delete
Android Development with Kotlin This work is licensed under the Apache 2 license. 24
Create a Room database
Android Development with Kotlin This work is licensed under the Apache 2 license. 25
Example Room database
Android Development with Kotlin This work is licensed under the Apache 2 license. 26
Create database instance
Android Development with Kotlin This work is licensed under the Apache 2 license. 27
Get and use a DAO
Android Development with Kotlin This work is licensed under the Apache 2 license. 28
Asynchronous
programming
Android Development with Kotlin This work is licensed under the Apache 2 license. 29
Long-running tasks
● Download information
● Sync with a server
● Write to a file
● Heavy computation
● Read from, or write to, a database
Android Development with Kotlin This work is licensed under the Apache 2 license. 30
Need for async programming
Android Development with Kotlin This work is licensed under the Apache 2 license. 31
Async programming on Android
● Threading
● Callbacks
● Plus many other options
Android Development with Kotlin This work is licensed under the Apache 2 license. 32
Coroutines
Android Development with Kotlin This work is licensed under the Apache 2 license. 33
Coroutines
Android Development with Kotlin This work is licensed under the Apache 2 license. 34
Benefits of coroutines
● Lightweight
● Fewer memory leaks
● Built-in cancellation support
● Jetpack integration
Android Development with Kotlin This work is licensed under the Apache 2 license. 35
Suspend functions
Android Development with Kotlin This work is licensed under the Apache 2 license. 36
Suspend and resume
● suspend
Pauses execution of current coroutine and saves local variables
● resume
Automatically loads saved state and continues execution from
the point the code was suspended
Android Development with Kotlin This work is licensed under the Apache 2 license. 37
Example
Android Development with Kotlin This work is licensed under the Apache 2 license. 38
Add suspend modifier to DAO methods
Android Development with Kotlin This work is licensed under the Apache 2 license. 39
Control where coroutines run
Android Development with Kotlin This work is licensed under the Apache 2 license. 40
withContext
Android Development with Kotlin This work is licensed under the Apache 2 license. 41
CoroutineScope
Coroutines must run in a CoroutineScope:
● Keeps track of all coroutines started in it (even suspended ones)
● Provides a way to cancel coroutines in a scope
● Provides a bridge between regular functions and coroutines
Examples: GlobalScope
ViewModel has viewModelScope
Lifecycle has lifecycleScope
Android Development with Kotlin This work is licensed under the Apache 2 license. 42
Start new coroutines
Android Development with Kotlin This work is licensed under the Apache 2 license. 43
ViewModelScope
Android Development with Kotlin This work is licensed under the Apache 2 license. 44
Example viewModelScope
Android Development with Kotlin This work is licensed under the Apache 2 license. 45
Testing databases
Android Development with Kotlin This work is licensed under the Apache 2 license. 46
Add Gradle dependencies
Android Development with Kotlin This work is licensed under the Apache 2 license. 47
Testing Android code
● @RunWith(AndroidJUnit4::class)
● @Before
● @After
● @Test
Android Development with Kotlin This work is licensed under the Apache 2 license. 48
Create test class
Android Development with Kotlin This work is licensed under the Apache 2 license. 49
Create and close database for each test
In DatabaseTest.kt:
Android Development with Kotlin This work is licensed under the Apache 2 license. 50
Test insert and retrieve from a database
In DatabaseTest.kt:
Android Development with Kotlin This work is licensed under the Apache 2 license. 51
Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 52
Summary
In Lesson 9, you learned how to:
● Set up and configure a database using the Room library
● Use coroutines for asynchronous programming
● Use coroutines with Room
● Test a database
Android Development with Kotlin This work is licensed under the Apache 2 license. 53
Learn more
● 7 Pro-tips for Room
● Room Persistence Library
● SQLite Home Page
● Save data using SQLite
● Coroutines Guide
● Dispatchers - kotlinx-coroutines-core
● Coroutines on Android (part I): Getting the background
● Coroutines on Android (part II): Getting started
● Easy Coroutines in Android: viewModelScope
● Kotlin Coroutines 101
Android Development with Kotlin This work is licensed under the Apache 2 license. 54
Pathway
Android Development with Kotlin This work is licensed under the Apache 2 license. 55
Lesson 10:
Advanced
RecyclerView
use cases
Android Development with Kotlin v1.0 This work is licensed under the Apache 2 license. 1
About this lesson
Lesson 10: Advanced RecyclerView use cases
● RecyclerView recap
● Advanced binding
● Multiple item view types
● Headers
● Grid layout
● Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 2
RecyclerView recap
Android Development with Kotlin This work is licensed under the Apache 2 license. 3
RecyclerView overview
Android Development with Kotlin This work is licensed under the Apache 2 license. 4
View recycling in RecyclerView
Boston, Massachusetts If item is scrolled
offscreen, it isn’t
Chicago, Illinois
destroyed. Item is put in
Mountain View, California a pool to be recycled.
Miami, Florida
Seattle, Washington
Reno, Nevada
onBindViewHolder binds
Nashville, Tennessee the view with the new
values, and then the view
Little Rock, Arkansas gets reinserted in the list.
Android Development with Kotlin This work is licensed under the Apache 2 license. 5
RecyclerViewDemo app
1
2
3
4
5
6
7
8
9
10
11
12
13
Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Adapter for RecyclerViewDemo
Android Development with Kotlin This work is licensed under the Apache 2 license. 7
Functions for RecyclerViewDemo
Android Development with Kotlin This work is licensed under the Apache 2 license. 8
Set the adapter onto the RecyclerView
In MainActivity.kt:
Android Development with Kotlin This work is licensed under the Apache 2 license. 9
Make items in the list clickable
In NumberListAdapter.kt:
Android Development with Kotlin This work is licensed under the Apache 2 license. 10
ListAdapter
● RecyclerView.Adapter
○ Disposes UI data on every update
○ Can be costly and wasteful
● ListAdapter
○ Computes the difference between what is currently shown
and what needs to be shown
○ Changes are calculated on a background thread
Android Development with Kotlin This work is licensed under the Apache 2 license. 11
Sort using RecyclerView.Adapter
Starting state Ending state
1 1 1
5 2 2
2 3 3
6 4 4
3 5 5
8 deletions 8 insertions 16 actions:
7 6 6
8 deletions
4 7 8 insertions 7
8 8 8
Android Development with Kotlin This work is licensed under the Apache 2 license. 12
Sort using ListAdapter
Starting state 1 Ending state
5
1 1
2
5 6 2
2 3 3
6 7 4
3 4 5
3 insertions 6 actions:
3 deletions 5 3 insertions
7 6
6 3 deletions
4 7
7
8 8 8
Android Development with Kotlin This work is licensed under the Apache 2 license. 13
ListAdapter example
Android Development with Kotlin This work is licensed under the Apache 2 license. 14
DiffUtil.ItemCallback
Android Development with Kotlin This work is licensed under the Apache 2 license. 15
DiffUtil.ItemCallback example
Android Development with Kotlin This work is licensed under the Apache 2 license. 16
Advanced binding
Android Development with Kotlin This work is licensed under the Apache 2 license. 17
ViewHolders and data binding
Android Development with Kotlin This work is licensed under the Apache 2 license. 18
Using the ViewHolder in a ListAdapter
Android Development with Kotlin This work is licensed under the Apache 2 license. 19
Binding adapters
Android Development with Kotlin This work is licensed under the Apache 2 license. 20
Custom attribute
Add another TextView in the list item layout that uses a custom attribute:
5 101
Android Development with Kotlin This work is licensed under the Apache 2 license. 21
Add a binding adapter
Declare binding adapter:
In NumberListAdapter.kt:
Android Development with Kotlin This work is licensed under the Apache 2 license. 22
Updated RecyclerViewDemo app
Android Development with Kotlin This work is licensed under the Apache 2 license. 23
Multiple item view types
Android Development with Kotlin This work is licensed under the Apache 2 license. 24
Add a new item view type
1. Create a new list item layout XML file.
2. Modify underlying adapter to hold the new type.
3. Override getItemViewType in adapter.
4. Create a new ViewHolder class.
5. Add conditional code in onCreateViewHolder and
onBindViewHolder to handle the new type.
Android Development with Kotlin This work is licensed under the Apache 2 license. 25
Declare new color item layout
Android Development with Kotlin This work is licensed under the Apache 2 license. 26
New view type
Android Development with Kotlin This work is licensed under the Apache 2 license. 27
Override getItemViewType
In NumberListAdapter.kt:
Android Development with Kotlin This work is licensed under the Apache 2 license. 28
Define new ViewHolder
Android Development with Kotlin This work is licensed under the Apache 2 license. 29
Update onCreateViewHolder()
Android Development with Kotlin This work is licensed under the Apache 2 license. 30
Update onBindViewHolder()
Android Development with Kotlin This work is licensed under the Apache 2 license. 31
Headers
Android Development with Kotlin This work is licensed under the Apache 2 license. 32
Headers Example
Entrees ● 2 item view types:
Burger $5.00 ○ header item
Salad $3.00 Drinks
Sandwich $4.00
○ food menu item
Drinks
Coffee $2.00
Coffee $2.00
Soda $1.00
Android Development with Kotlin This work is licensed under the Apache 2 license. 33
Grid layout
Android Development with Kotlin This work is licensed under the Apache 2 license. 34
List versus grid
Android Development with Kotlin This work is licensed under the Apache 2 license. 35
Specifying a LayoutManager
Android Development with Kotlin This work is licensed under the Apache 2 license. 36
GridLayoutManager
Android Development with Kotlin This work is licensed under the Apache 2 license. 37
Set span size for an item
Android Development with Kotlin This work is licensed under the Apache 2 license. 38
Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 39
Summary
In Lesson 10, you learned how to:
● Use ListAdapter to make RecyclerView more efficient at
updating lists
● Create a binding adapter with custom logic to set View values from an
XML attribute
● Handle multiple ViewHolders in the same RecyclerView to show
multiple item types
● Use GridLayoutManager to display items as a grid
● Specify span size for an item in a grid with SpanSizeLookup
Android Development with Kotlin This work is licensed under the Apache 2 license. 40
Learn More
● Create a List with RecyclerView
● RecyclerView
● ListAdapter
● Binding adapters
● GridLayoutManager
● DiffUtil and ItemCallback
Android Development with Kotlin This work is licensed under the Apache 2 license. 41
Pathway
Android Development with Kotlin This work is licensed under the Apache 2 license. 42
Lesson 11:
Connect to the
internet
Android Development with Kotlin v1.0 This work is licensed under the Apache 2 license. 1
About this lesson
Lesson 11: Connect to the internet
● Android permissions
● Connect to, and use, network resources
● Connect to a web service
● Display images
● Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 2
Android permissions
Android Development with Kotlin This work is licensed under the Apache 2 license. 3
Permissions
Android Development with Kotlin This work is licensed under the Apache 2 license. 4
Permissions granted to your app
Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Permission protection levels
Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Add permissions to the manifest
In AndroidManifest.xml:
Android Development with Kotlin This work is licensed under the Apache 2 license. 7
Internet access permissions
In AndroidManifest.xml:
Android Development with Kotlin This work is licensed under the Apache 2 license. 8
Request dangerous permissions
Android Development with Kotlin This work is licensed under the Apache 2 license. 9
Prompt for dangerous permission
Android Development with Kotlin This work is licensed under the Apache 2 license. 10
App permissions best practices
Android Development with Kotlin This work is licensed under the Apache 2 license. 11
Connect to, and use,
network resources
Android Development with Kotlin This work is licensed under the Apache 2 license. 12
Retrofit
● Networking library that turns your HTTP API into a Kotlin and
Java interface
● Enables processing of requests and responses into objects for
use by your apps
○ Provides base support for parsing common response types,
such as XML and JSON
○ Can be extended to support other response types
Android Development with Kotlin This work is licensed under the Apache 2 license. 13
Why use Retrofit?
Android Development with Kotlin This work is licensed under the Apache 2 license. 14
Add Gradle dependencies
Android Development with Kotlin This work is licensed under the Apache 2 license. 15
Connect to a web service
Android Development with Kotlin This work is licensed under the Apache 2 license. 16
HTTP methods
● GET
● POST
● PUT
● DELETE
Android Development with Kotlin This work is licensed under the Apache 2 license. 17
Example web service API
Android Development with Kotlin This work is licensed under the Apache 2 license. 18
Define a Retrofit service
Android Development with Kotlin This work is licensed under the Apache 2 license. 19
Create a Retrofit object for network access
Android Development with Kotlin This work is licensed under the Apache 2 license. 20
End-to-end diagram
Retrofit Service
HTTP Request
App UI Server
ViewModel HTTP Response Web API
Converter
(JSON)
Moshi
Android Development with Kotlin This work is licensed under the Apache 2 license. 21
Converter.Factory
Android Development with Kotlin This work is licensed under the Apache 2 license. 22
Moshi
● JSON library for parsing JSON into objects and back
● Add Moshi library dependencies to your app’s Gradle file.
● Configure your Moshi builder to use with Retrofit.
List of JSON
Moshi
Post objects response
Android Development with Kotlin This work is licensed under the Apache 2 license. 23
Moshi JSON encoding
Android Development with Kotlin This work is licensed under the Apache 2 license. 24
JSON code
Android Development with Kotlin This work is licensed under the Apache 2 license. 25
Set up Retrofit and Moshi
Android Development with Kotlin This work is licensed under the Apache 2 license. 26
Use Retrofit with coroutines
Android Development with Kotlin This work is licensed under the Apache 2 license. 27
Display images
Android Development with Kotlin This work is licensed under the Apache 2 license. 28
Glide
Android Development with Kotlin This work is licensed under the Apache 2 license. 29
Add Gradle dependency
Android Development with Kotlin This work is licensed under the Apache 2 license. 30
Load an image
Android Development with Kotlin This work is licensed under the Apache 2 license. 31
Customize a request with RequestOptions
Android Development with Kotlin This work is licensed under the Apache 2 license. 32
RequestOptions example
Android Development with Kotlin This work is licensed under the Apache 2 license. 33
Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 34
Summary
In Lesson 11, you learned how to:
● Declare permissions your app needs in AndroidManifest.xml
● Use the three protection levels for permissions: normal, signature, and
dangerous (prompt the user at runtime for dangerous permissions)
● Use the Retrofit library to make web service API calls from your app
● Use the Moshi library to parse JSON response into class objects
● Load and display images from the internet using the Glide library
Android Development with Kotlin This work is licensed under the Apache 2 license. 35
Learn More
Android Development with Kotlin This work is licensed under the Apache 2 license. 36
Pathway
Android Development with Kotlin This work is licensed under the Apache 2 license. 37
Lesson 11:
Connect to the
internet
Android Development with Kotlin v1.0 This work is licensed under the Apache 2 license. 1
About this lesson
Lesson 11: Connect to the internet
● Android permissions
● Connect to, and use, network resources
● Connect to a web service
● Display images
● Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 2
Android permissions
Android Development with Kotlin This work is licensed under the Apache 2 license. 3
Permissions
Android Development with Kotlin This work is licensed under the Apache 2 license. 4
Permissions granted to your app
Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Permission protection levels
Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Add permissions to the manifest
In AndroidManifest.xml:
Android Development with Kotlin This work is licensed under the Apache 2 license. 7
Internet access permissions
In AndroidManifest.xml:
Android Development with Kotlin This work is licensed under the Apache 2 license. 8
Request dangerous permissions
Android Development with Kotlin This work is licensed under the Apache 2 license. 9
Prompt for dangerous permission
Android Development with Kotlin This work is licensed under the Apache 2 license. 10
App permissions best practices
Android Development with Kotlin This work is licensed under the Apache 2 license. 11
Connect to, and use,
network resources
Android Development with Kotlin This work is licensed under the Apache 2 license. 12
Retrofit
● Networking library that turns your HTTP API into a Kotlin and
Java interface
● Enables processing of requests and responses into objects for
use by your apps
○ Provides base support for parsing common response types,
such as XML and JSON
○ Can be extended to support other response types
Android Development with Kotlin This work is licensed under the Apache 2 license. 13
Why use Retrofit?
Android Development with Kotlin This work is licensed under the Apache 2 license. 14
Add Gradle dependencies
Android Development with Kotlin This work is licensed under the Apache 2 license. 15
Connect to a web service
Android Development with Kotlin This work is licensed under the Apache 2 license. 16
HTTP methods
● GET
● POST
● PUT
● DELETE
Android Development with Kotlin This work is licensed under the Apache 2 license. 17
Example web service API
Android Development with Kotlin This work is licensed under the Apache 2 license. 18
Define a Retrofit service
Android Development with Kotlin This work is licensed under the Apache 2 license. 19
Create a Retrofit object for network access
Android Development with Kotlin This work is licensed under the Apache 2 license. 20
End-to-end diagram
Retrofit Service
HTTP Request
App UI Server
ViewModel HTTP Response Web API
Converter
(JSON)
Moshi
Android Development with Kotlin This work is licensed under the Apache 2 license. 21
Converter.Factory
Android Development with Kotlin This work is licensed under the Apache 2 license. 22
Moshi
● JSON library for parsing JSON into objects and back
● Add Moshi library dependencies to your app’s Gradle file.
● Configure your Moshi builder to use with Retrofit.
List of JSON
Moshi
Post objects response
Android Development with Kotlin This work is licensed under the Apache 2 license. 23
Moshi JSON encoding
Android Development with Kotlin This work is licensed under the Apache 2 license. 24
JSON code
Android Development with Kotlin This work is licensed under the Apache 2 license. 25
Set up Retrofit and Moshi
Android Development with Kotlin This work is licensed under the Apache 2 license. 26
Use Retrofit with coroutines
Android Development with Kotlin This work is licensed under the Apache 2 license. 27
Display images
Android Development with Kotlin This work is licensed under the Apache 2 license. 28
Glide
Android Development with Kotlin This work is licensed under the Apache 2 license. 29
Add Gradle dependency
Android Development with Kotlin This work is licensed under the Apache 2 license. 30
Load an image
Android Development with Kotlin This work is licensed under the Apache 2 license. 31
Customize a request with RequestOptions
Android Development with Kotlin This work is licensed under the Apache 2 license. 32
RequestOptions example
Android Development with Kotlin This work is licensed under the Apache 2 license. 33
Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 34
Summary
In Lesson 11, you learned how to:
● Declare permissions your app needs in AndroidManifest.xml
● Use the three protection levels for permissions: normal, signature, and
dangerous (prompt the user at runtime for dangerous permissions)
● Use the Retrofit library to make web service API calls from your app
● Use the Moshi library to parse JSON response into class objects
● Load and display images from the internet using the Glide library
Android Development with Kotlin This work is licensed under the Apache 2 license. 35
Learn More
Android Development with Kotlin This work is licensed under the Apache 2 license. 36
Pathway
Android Development with Kotlin This work is licensed under the Apache 2 license. 37
Lesson 12:
Repository pattern
and WorkManager
Android Development with Kotlin v1.0 This work is licensed under the Apache 2 license. 1
About this lesson
Lesson 12: Repository pattern and WorkManager
● Repository pattern
● WorkManager
● Work input and output
● WorkRequest constraints
● Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 2
Repository pattern
Android Development with Kotlin This work is licensed under the Apache 2 license. 3
Existing app architecture
UI Controller
ViewModel
Room
Android Development with Kotlin This work is licensed under the Apache 2 license. 4
Relative data speeds
Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Cache network responses
Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Repository pattern
Android Development with Kotlin This work is licensed under the Apache 2 license. 7
App architecture with repository pattern
UI Controller
ViewModel
Repository
Android Development with Kotlin This work is licensed under the Apache 2 license. 8
Implement a repository class
Android Development with Kotlin This work is licensed under the Apache 2 license. 9
WorkManager
Android Development with Kotlin This work is licensed under the Apache 2 license. 10
WorkManager
Android Development with Kotlin This work is licensed under the Apache 2 license. 11
When to use WorkManager
Task
Requires active
Immediate user
Yes No
Exact Deferred
Android Development with Kotlin This work is licensed under the Apache 2 license. 12
Declare WorkManager dependencies
Android Development with Kotlin This work is licensed under the Apache 2 license. 13
Important classes to know
Android Development with Kotlin This work is licensed under the Apache 2 license. 14
Define the work
Android Development with Kotlin This work is licensed under the Apache 2 license. 15
Extend CoroutineWorker instead of Worker
Android Development with Kotlin This work is licensed under the Apache 2 license. 16
WorkRequests
Android Development with Kotlin This work is licensed under the Apache 2 license. 17
Schedule a OneTimeWorkRequest
Create WorkRequest:
Android Development with Kotlin This work is licensed under the Apache 2 license. 18
Schedule a PeriodicWorkRequest
● Set a repeat interval
● Set a flex interval (optional)
...
Interval 1 Interval 2 Interval N
Android Development with Kotlin This work is licensed under the Apache 2 license. 19
Flex interval
Work could And then again
happen here soon after
Example 1
Day 1 Day 2
1 hr 1 hr
Example 2
11 PM 12 AM 11 PM 12 AM
Day 1 Day 2
Android Development with Kotlin This work is licensed under the Apache 2 license. 20
PeriodicWorkRequest example
Android Development with Kotlin This work is licensed under the Apache 2 license. 21
Enqueue periodic work
Android Development with Kotlin This work is licensed under the Apache 2 license. 22
Work input and output
Android Development with Kotlin This work is licensed under the Apache 2 license. 23
Define Worker with input and output
Android Development with Kotlin This work is licensed under the Apache 2 license. 24
Result output from doWork()
Android Development with Kotlin This work is licensed under the Apache 2 license. 25
Send input data to Worker
Android Development with Kotlin This work is licensed under the Apache 2 license. 26
WorkRequest constraints
Android Development with Kotlin This work is licensed under the Apache 2 license. 27
Constraints
●
●
●
●
●
Android Development with Kotlin This work is licensed under the Apache 2 license. 28
Constraints example
Android Development with Kotlin This work is licensed under the Apache 2 license. 29
Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 30
Summary
In Lesson 12, you learned how to:
● Use a repository to abstract the data layer from the rest of the app
● Schedule background tasks efficiently and optimize them using
WorkManager
● Create custom Worker classes to specify the work to be done
● Create and enqueue one-time or periodic work requests
Android Development with Kotlin This work is licensed under the Apache 2 license. 31
Learn more
● Fetch data
● Schedule tasks with WorkManager
● Define work requests
● Connect ViewModel and the repository
● Use WorkManager for immediate background execution
Android Development with Kotlin This work is licensed under the Apache 2 license. 32
Pathway
Android Development with Kotlin This work is licensed under the Apache 2 license. 33
Lesson 13:
App UI design
Android Development with Kotlin v1.0 This work is licensed under the Apache 2 license. 1
About this lesson
Lesson 13: App UI design
● Android styling
● Typography
● Material Design
● Material Components
● Localization
● Example apps
● Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 2
Android styling
Android Development with Kotlin This work is licensed under the Apache 2 license. 3
Android styling system
Android Development with Kotlin This work is licensed under the Apache 2 license. 4
Precedence of each method of styling
View
attributes Overrides this
Style
Overrides this
Theme
Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Themes
Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Declare a theme
In res/values/themes.xml:
Android Development with Kotlin This work is licensed under the Apache 2 license. 7
Apply a theme
In AndroidManifest.xml:
In layout file:
…
Android Development with Kotlin This work is licensed under the Apache 2 license. 8
Refer to theme attribute in a layout
In layout file:
Android Development with Kotlin This work is licensed under the Apache 2 license. 9
Styles
Android Development with Kotlin This work is licensed under the Apache 2 license. 10
Declare a style
In res/values/styles.xml:
Android Development with Kotlin This work is licensed under the Apache 2 license. 11
Apply a style
Android Development with Kotlin This work is licensed under the Apache 2 license. 12
Refer to theme attribute in a style
In res/values/styles.xml:
Android Development with Kotlin This work is licensed under the Apache 2 license. 13
View attributes
Android Development with Kotlin This work is licensed under the Apache 2 license. 14
Resources directory
└── res
├── drawable
├── drawable-*
├── layout
├── menu
├── mipmap-*
├── navigation
├── values
│ ├── colors.xml
│ ├── dimens.xml
│ ├── strings.xml
│ ├── styles.xml
│ └── themes.xml
└── values-*
Android Development with Kotlin This work is licensed under the Apache 2 license. 15
Provide alternative resources
└── res
├── values
│ ├── colors.xml
│ ├── strings.xml
│ ├── styles.xml
│ └── themes.xml
└── values-b+es
│ ├── strings.xml Use when device locale is set to Spanish
└── values-night
└── themes.xml Use when night mode is turned on
Android Development with Kotlin This work is licensed under the Apache 2 license. 16
Color resources
A way to name and standardize colors throughout your app
In res/values/colors.xml:
Android Development with Kotlin This work is licensed under the Apache 2 license. 17
Dimension resources
A way to name and standardize dimension values in your layouts
Android Development with Kotlin This work is licensed under the Apache 2 license. 18
Typography
Android Development with Kotlin This work is licensed under the Apache 2 license. 19
Scale-independent pixels (sp)
Android Development with Kotlin This work is licensed under the Apache 2 license. 20
Type scale
Android Development with Kotlin This work is licensed under the Apache 2 license. 21
TextAppearance
A TextAppearance style often alters one or more of these attributes:
● typeface (android:fontFamily)
● weight (android:textStyle)
● text size (android:textSize)
● capitalization (android:textAllCaps)
● letter spacing (android:letterSpacing)
Android Development with Kotlin This work is licensed under the Apache 2 license. 22
Examples using TextAppearance
Android Development with Kotlin This work is licensed under the Apache 2 license. 23
Customize your own TextAppearance
Android Development with Kotlin This work is licensed under the Apache 2 license. 24
Use a custom TextAppearance in a theme
Android Development with Kotlin This work is licensed under the Apache 2 license. 25
Material Design
Android Development with Kotlin This work is licensed under the Apache 2 license. 26
Intro to Material
Adaptable system of
guidelines, components, and
tools that support best
practices for UI design
Material Design homepage
Android Development with Kotlin This work is licensed under the Apache 2 license. 27
Material Components
Android Development with Kotlin This work is licensed under the Apache 2 license. 28
Material color tool
Android Development with Kotlin This work is licensed under the Apache 2 license. 29
Baseline Material color theme
Android Development with Kotlin This work is licensed under the Apache 2 license. 30
Material Components for Android Library
Android Development with Kotlin This work is licensed under the Apache 2 license. 31
Material Themes
●
●
●
●
●
●
●
●
Android Development with Kotlin This work is licensed under the Apache 2 license. 32
Material theme example
Android Development with Kotlin This work is licensed under the Apache 2 license. 33
Dark theme
A low-light UI that displays mostly dark surfaces
● Replaces light-tinted surfaces and dark text
with dark-tinted surfaces and light text
● Makes it easier for anyone to use a device in
lower-light environments
● Improves visibility for users with low vision
and those sensitive to bright light
● Can significantly reduce power usage
(depending on the device)
Android Development with Kotlin This work is licensed under the Apache 2 license. 34
Support dark theme
In values/themes.xml:
In values-night/themes.xml:
Android Development with Kotlin This work is licensed under the Apache 2 license. 35
Material Components
Android Development with Kotlin This work is licensed under the Apache 2 license. 36
Material Components
Component library provided for Android and design guidelines
Android Development with Kotlin This work is licensed under the Apache 2 license. 38
Text field example
Android Development with Kotlin This work is licensed under the Apache 2 license. 39
Bottom navigation
Android Development with Kotlin This work is licensed under the Apache 2 license. 40
Bottom navigation example
Android Development with Kotlin This work is licensed under the Apache 2 license. 41
Bottom navigation listener
Android Development with Kotlin This work is licensed under the Apache 2 license. 42
Snackbar
Android Development with Kotlin This work is licensed under the Apache 2 license. 43
Snackbar examples
Show a simple message:
Android Development with Kotlin This work is licensed under the Apache 2 license. 44
Floating Action Button (FAB)
● Perform the most-common action for a screen (for example, creating a new
email)
● Come in multiple sizes (regular, mini, and extended)
Android Development with Kotlin This work is licensed under the Apache 2 license. 45
CoordinatorLayout
Android Development with Kotlin This work is licensed under the Apache 2 license. 46
FAB example
Android Development with Kotlin This work is licensed under the Apache 2 license. 47
Cards
Android Development with Kotlin This work is licensed under the Apache 2 license. 48
MaterialCardView example
Android Development with Kotlin This work is licensed under the Apache 2 license. 49
Localization
Android Development with Kotlin This work is licensed under the Apache 2 license. 50
Localize your app
● Separate the localized aspects of your app (for example, text, audio
files, currency, and numbers) as much as possible from the core Kotlin
functionality of the app.
Example: Extract the user facing strings into strings.xml.
● When a user runs your app, the Android system selects which
resources to load based on the device's locale.
● If locale-specific resources are not found, Android falls back to default
resources you defined.
Android Development with Kotlin This work is licensed under the Apache 2 license. 51
Support different languages and cultures
● Decide which locales to support.
● Create locale-specific directories in res directory:
<resource type>-b+<language code>
[+<country code>]
Examples: layout-b+en+US
values-b+es
● Provide locale-specific resources (such as strings
and drawables) in those directories.
Android Development with Kotlin This work is licensed under the Apache 2 license. 52
Support languages that use RTL scripts
Android Development with Kotlin This work is licensed under the Apache 2 license. 53
Example apps
Android Development with Kotlin This work is licensed under the Apache 2 license. 54
Check out other apps
Android Development with Kotlin This work is licensed under the Apache 2 license. 55
Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 56
Summary
In Lesson 13, you learned how to:
● Customize the visual look of your app using styles and themes
● Choose from predefined type scales for the text in your app (or create
your own text appearance)
● Select theme colors for your app using Material color tool
● Use Material Components library to speed up UI development
● Localize your app to support different languages and cultures
Android Development with Kotlin This work is licensed under the Apache 2 license. 57
Learn more
● Material Design
● Material Components
● Tools for picking colors
● Dark theme
● Localize your app
● Blog posts: Themes vs Styles, Common Theme Attributes, Prefer Theme
Attributes, Themes Overlay
● Sample code: Sunflower app, Google I/O app, Android GitHub repo
Android Development with Kotlin This work is licensed under the Apache 2 license. 58
Pathway
Android Development with Kotlin This work is licensed under the Apache 2 license. 59