Download as pdf or txt
Download as pdf or txt
You are on page 1of 710

Android

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

● Experience in an object-oriented programming language


● Comfortable using an IDE
● Familiar with using GitHub
● Access to a computer and internet connection
● (Optional) Android device and USB cable

Android Development with Kotlin This work is licensed under the Apache 2 license. 3
What you'll learn

● How to build a variety of Android


apps in Kotlin
● Kotlin language essentials
● Best practices for app development
● Resources to keep learning

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

● Open-source mobile platform


● 11 major platform releases so far
● 2.5 billion monthly active Android devices
● 2+ billion monthly active Google Play users

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

A modern programming language


that helps developers be more
productive.

Android Development with Kotlin This work is licensed under the Apache 2 license. 9
Benefits of Kotlin

● Expressive and concise


● Safer code
● Interoperable
● Structured Concurrency

Android Development with Kotlin This work is licensed under the Apache 2 license. 10
Idiomatic Kotlin

● Kotlin is at its best when used idiomatically


● Avoid just translating Java into Kotlin
● As you learn more Kotlin, you'll find easier, more concise ways
to do things
● For a list of common Kotlin idioms, refer to the Kotlin Language
Guide on Idioms

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

Unit 1 (3 weeks) Unit 2 (3 weeks) Unit 3 (6 weeks) Unit 4 (1 week)

Get Started with Introduction to Android App App Design


Kotlin Android Architecture

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

After each class, complete


the corresponding learning
pathway with articles and
codelabs to practice what
you learned.

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

● Learn Kotlin for a list of official reference materials


● Kotlin Language Documentation (downloadable PDF)
● Kotlin Koans for more snippets to practice with
● Coding Conventions for a coding style guide for the Kotlin language
● Learn Kotlin by Example for a set of small and simple annotated
examples

Android Development with Kotlin This work is licensed under the Apache 2 license. 22
Android and other resources
● Official Android developer website

● Android Developers Blog

● Android Developers Medium blog

● Android Developers YouTube channel

● @AndroidDev on Twitter

● Android Developer Newsletter

● Stack Overflow

● Offline documentation through SDK Manager

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)

It may take a few


moments before the
Kotlin menu appears
under Tools.

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.

Result includes the


type (kotlin.Int).

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

Long 64 From -263 to 263-1

Int 32 From -231 to 231-1

Short 16 From -32768 to 32767

Byte 8 From -128 to 127

Android Development with Kotlin This work is licensed under the Apache 2 license. 16
Floating-point and other numeric types
Type Bits Notes

Double 64 16 - 17 significant digits


Float 32 6 - 7 significant digits
Char 16 16-bit Unicode character

Boolean 8 True or false. Operations include:


- lazy disjunction, - lazy conjunction,
- negation

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

Use underscores to make long numeric constants more readable.

Android Development with Kotlin This work is licensed under the Apache 2 license. 20
Strings

Strings are any sequence of characters enclosed by double quotes.

String literals can contain escape characters

Or any arbitrary text delimited by a triple quote (""")

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:

Or an expression inside curly braces:

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

● Powerful type inference


● Let the compiler infer the type
● You can explicitly declare the type if needed
● Mutable and immutable variables
● Immutability not enforced, but recommended

Kotlin is a statically-typed language. The type is resolved at compile time and


never changes.

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)

Although not strictly enforced, using immutable variables is recommended in


most cases.

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

Kotlin features several ways to implement conditional logic:


● If/Else statements
● When statements
● For loops
● While loops

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

● Data type containing a span of comparable values (e.g.,


integers from 1 to 100 inclusive)
● Ranges are bounded
● Objects within a range can be mutable or immutable

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)

for (i in 'd'..'g') 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

● Lists are ordered collections of elements


● List elements can be accessed programmatically through their
indices
● Elements can occur more than once in a list

An example of a list is a sentence: it's a group of words, their order is important,


and they can repeat.

Android Development with Kotlin This work is licensed under the Apache 2 license. 43
Immutable list using listOf()

Declare a list using and print it out.

⇒ [trumpet, piano, violin]

Android Development with Kotlin This work is licensed under the Apache 2 license. 44
Mutable list using mutableListOf()

Lists can be changed 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

● Arrays store multiple items

● Array elements can be accessed programmatically through


their indices

● Array elements are mutable


● Array size is fixed

Android Development with Kotlin This work is licensed under the Apache 2 license. 46
Array using arrayOf()

An array of strings can be created 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 contain different 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

Use the + operator.

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

● In Kotlin, variables cannot be null by default


● You can explicitly assign a variable to null using the safe call
operator
● Allow null-pointer exceptions using the !! operator
● You can test for null using the elvis (?:) operator

Android Development with Kotlin This work is licensed under the Apache 2 license. 51
Variables cannot be null

In Kotlin, null variables are not allowed by default.

Declare an Int and assign null to it.

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.

Declare an Int? as nullable

In general, do not set a variable to null as it may have unwanted consequences.

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.

throws NullPointerException if s is null

Warning: Because !! will throw an exception, it should only be used when it


would be exceptional to hold a null value.

Android Development with Kotlin This work is licensed under the Apache 2 license. 55
Elvis operator

Chain null tests with the ?: 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

Practice what you’ve learned by


completing the pathway:
Lesson 1: Kotlin basics

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:

● Create a file in your project


● Create a main() function
● Pass arguments to main()(Optional)
● Use any passed arguments in function calls (Optional)
● Run your program

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.

In the Hello.kt file:

The args in the main() function are optional.

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

Use args[0] to access the first input argument passed to main().

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

In Kotlin, almost everything is an expression and has a value. Even an if


expression has a value.

Android Development with Kotlin This work is licensed under the Apache 2 license. 12
Expression values

Sometimes, that value is kotlin.Unit.

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

● A block of code that performs a specific task

● Breaks a large program into smaller modular chunks

● Declared using the fun keyword

● Can take arguments with either named or default values

15
Android Development with Kotlin This work is licensed under the Apache 2 license.
Parts of a function

Earlier, you created a simple function that printed "Hello World".

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.

Unit is a type with only one value: 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

Functions may have:

● 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.

Use "=" after the type


to define default values


Android Development with Kotlin This work is licensed under the Apache 2 license. 20
Required parameters

If no default is specified for a parameter, the corresponding argument is required.

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.

Has default value

Pass in required arguments.

Android Development with Kotlin This work is licensed under the Apache 2 license. 22
Named arguments

To improve readability, use named arguments for required 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

Compact functions, or single-expression functions, make your code more concise


and readable.

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

● Kotlin functions can be stored in variables and data structures

● They can be passed as arguments to, and returned from, other


higher-order functions

● You can use higher-order functions to create new "built-in"


functions

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.

Parameter and type


Function arrow

⇒ 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.

Variable name Data type of variable Function


(function type)

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.

Using a function type separates its implementation from its usage.

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.

Passing a named function,


not a lambda

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

red red-orange dark red orange bright orange saffron

Apply filter() on list


Condition: element contains “red”

red red-orange dark red

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:

● Eager: occurs regardless of whether the result is ever used

● Lazy: occurs only if necessary at runtime

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

Sequences can be turned back into lists using toList().

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.

● flatten() returns a single list of all the elements of nested collections.

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

Practice what you’ve learned by


completing the pathway:
Lesson 2: Functions

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.

This is technically equivalent to:

Android Development with Kotlin This work is licensed under the Apache 2 license. 10
Initializer block

● Any required initialization code is run in a special init block


● Multiple init blocks are allowed
● init blocks become the body of the primary constructor

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

● Use the constructor keyword to define secondary constructors


● Secondary constructors must call:
○ The primary constructor using this keyword
OR

○ Another secondary constructor that calls the primary constructor


● Secondary constructor body is not required

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

● Define properties in a class using val or var


● Access these properties using
dot . notation with property name
● Set these properties using
dot . notation with property name (only if declared a var)

Android Development with Kotlin This work is licensed under the Apache 2 license. 15
Person class with name property

Access with <property name>


Set with <property name>

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:

● Override get() for a property


● Override set() for a property (if defined as a var)

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

● Classes can also contain functions


● Declare functions as shown in Functions in Lesson 2
○ fun keyword
○ Can have default or required parameters
○ Specify return type (if not Unit)

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

● Kotlin has single-parent class inheritance


● Each class has exactly one parent class, called a superclass
● Each subclass inherits all members of its superclass including
ones that the superclass itself has inherited

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

● Provide a contract all implementing classes must adhere to

● Can contain method signatures and property names

● Can derive from other 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

● Kotlin classes by default are not subclassable

● Use open keyword to allow subclassing

● Properties and functions are redefined with the override


keyword

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)

● Must use override when overriding properties and methods

● Something marked override can be overridden in subclasses


(unless marked final)

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.

● Will the behavior be specific to that type? Consider a class.

● Need to inherit from multiple classes? Consider refactoring code to see


if some behavior can be isolated into an interface.

● Want to leave some properties / methods abstract to be defined by


subclasses? Consider an abstract class.

● 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.

● Appears as if the implementer added it

● Not actually modifying the existing class

● Cannot access private instance variables

Format:

Android Development with Kotlin This work is licensed under the Apache 2 license. 34
Why use extension functions?

● Add functionality to classes that are not

● Add functionality to classes you don’t own

● 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:

Call isOdd() on an Int:

Extension functions are very powerful in Kotlin!

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

● Mark the class with the data keyword

● Generates getters for each property (and setters for vars too)

● Generates toString(), equals(), hashCode(), copy()


methods, and destructuring operators

Format:

Android Development with Kotlin This work is licensed under the Apache 2 license. 38
Data class example
Define the data class:

Use the data class:

Data classes make your code much more concise!

Android Development with Kotlin This work is licensed under the Apache 2 license. 39
Pair and Triple

● Pair and Triple are predefined data classes that store


2 or 3 pieces of data respectively

● Access variables with .first, .second, .third respectively

● Usually named data classes are a better option


(more meaningful names for your use case)

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).

It allows for more readable code

Also used in collections like Map and HashMap

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

● Use this to require instances be one of several constant values


● The constant value is, by default, not visible to you
● Use enum before the class keyword

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

● Sometimes you only want one instance of a class to ever exist

● Use the object keyword instead of the class keyword

● Accessed with NameOfObject.<function or variable>

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

● Lets all instances of a class share a single instance of a set of


variables or functions

● Use companion keyword

● Referenced via ClassName.PropertyOrFunction

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

● Kotlin DOES NOT enforce a single entity (class/interface)


per file convention

● You can and should group related structures in the same file

● Be mindful of file length and clutter

Android Development with Kotlin This work is licensed under the Apache 2 license. 50
Packages

● Provide means for organization

● Identifiers are generally lower case words separated by periods

● Declared in the first non-comment line of code in a file


following the package keyword

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.

● public means visible outside the class. Everything is public by default,


including variables and methods of the class.

● 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

Practice what you’ve learned by


completing the pathway:
Lesson 3: Classes and objects

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

● Minimum SDK: Device needs at least this API level to install


● Target SDK: API version and highest Android version tested
● Compile SDK: Android OS library version compiled with
minSdkVersion <= targetSdkVersion <= compileSdkVersion

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 device (phone, tablet)


● Emulator on your computer

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

● Views are the user interface building blocks in Android


○ Bounded by a rectangular area on the screen
○ Responsible for drawing and event handling
○ Examples: TextView, ImageView, Button
● Can be grouped to form more complex user interfaces

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

You can also edit your layout in XML.


● Android uses XML to specify the layout of user interfaces
(including View attributes)

● Each View in XML corresponds to a class in Kotlin that controls


how that View functions

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.

FrameLayout LinearLayout ConstraintLayout

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

ImageView TextView LinearLayout TextView

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

Activity shut down

Android Development with Kotlin This work is licensed under the Apache 2 license. 34
Implement the onCreate() callback

Called when the system creates your Activity

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:

Get a reference to the View in the view hierarchy:


val resultTextView: TextView = findViewById(R.id.textView)

Change properties or call methods on the View instance:


resultTextView.text = "Goodbye!"

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

Did developer register a callback?

No Yes

Ignore the event Execute the callback

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

Provide libraries and infrastructure needed by your app

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

● Refers to improving the design and functionality of your


app to make it easier for more people, including those
with disabilities, to use
● Making your app more accessible leads to an overall
better user experience and benefits all your users

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

Tool that scans your screen and suggests


improvements to make your app more
accessible, based on:
● Content labels
● Touch target sizes
● Clickable views
● Text and image contrast

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

● Text in TextView already provided to accessibility services,


no additional label needed

Android Development with Kotlin This work is licensed under the Apache 2 license. 59
No content label needed

● For graphical elements that are purely for decorative


purposes, you can set
android:importantForAccessibility="no"

● Removing unnecessary announcements is better for the user

Android Development with Kotlin This work is licensed under the Apache 2 license. 60
TalkBack

● Google screen reader included on Android devices


● Provides spoken feedback so you don’t have to look at the
screen to use your device
● Lets you navigate the device using gestures
● Includes braille keyboard for Unified English Braille

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

● Allows for controlling the device using one or more switches


instead of the touchscreen
● Scans your app UI and highlights each item until you make a
selection
● Use with external switch, external keyboard, or buttons on the
Android device (e.g., volume buttons)

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:

● Talkback screen reader


● Switch Access
● Accessibility Menu
● Select to Speak

Android Development with Kotlin This work is licensed under the Apache 2 license. 64
Accessibility Resources

● Build more accessible apps


● Principles for improving app accessibility
● Basic Android Accessibility codelab
● Material Design best practices on accessibility

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

Practice what you’ve learned by


completing the pathway:
Lesson 4: Build your first Android app

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)


take screen density into account.
● Android views are measured in Hello 80dp

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

ldpi (mostly unused) Low density ~120dpi

mdpi (baseline density) Medium density ~160dpi

hdpi High density ~240dpi

xhdpi Extra-high density ~320dpi

xxhdpi Extra-extra-high density ~480dpi

xxxhdpi Extra-extra-extra-high density ~640dpi

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:

How it's drawn:

Android Development with Kotlin This work is licensed under the Apache 2 license. 8
View margins and padding

View with margin View with margin 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

● Deeply nested ViewGroups require more computation


● Views may be measured multiple times
● Can cause UI slowdown and lack of responsiveness

Use ConstraintLayout to avoid some of these issues!

Android Development with Kotlin This work is licensed under the Apache 2 license. 11
What is ConstraintLayout?

● Recommended default layout for Android


● Solves costly issue of too many nested layouts, while
allowing complex behavior
● Position and size views within it using a set of constraints

Android Development with Kotlin This work is licensed under the Apache 2 license. 12
What is a constraint?

A restriction or limitation on the


properties of a View that the layout
attempts to respect

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

Left Hello! Right

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

● Let you position views in relation to each other


● Can be linked horizontally or vertically
● Provide much of LinearLayout functionality

Android Development with Kotlin This work is licensed under the Apache 2 license. 24
Create a Chain in Layout Editor

1. Select the objects you want to be in


the chain.
2. Right-click and select Chains.
3. Create a horizontal or vertical chain.

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

Spread Inside 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

● Let you position multiple views relative to a single guide


● Can be vertical or horizontal
● Allow for greater collaboration with design/UX teams
● Aren't drawn on the device

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

● Control the visibility of a set of widgets


● Group visibility can be toggled in code

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

● Widget for displaying lists of data


● "Recycles" (reuses) item views to make scrolling more
performant
● Can specify a list item layout for each item in the dataset
● Supports animations and transitions

Android Development with Kotlin This work is licensed under the Apache 2 license. 44
RecyclerView.Adapter

● Supplies data and layouts that the RecyclerView displays


● A custom Adapter extends from RecyclerView.Adapter and
overrides these three functions:
● getItemCount
● onCreateViewHolder
● onBindViewHolder

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)

val rv: RecyclerView = findViewById(R.id.rv)


rv.layoutManager = LinearLayoutManager(this)

rv.adapter = MyAdapter(IntRange(0, 100).toList())


}

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

● Pixel density on Android


● Spacing
● Device metrics
● Type scale
● Build a Responsive UI with ConstraintLayout
● Data Binding Library
● Create dynamic lists with RecyclerView

Android Development with Kotlin This work is licensed under the Apache 2 license. 53
Pathway

Practice what you’ve learned by


completing the pathway:
Lesson 5: Layouts

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

Requests an action from another app component, such as another Activity

● An Intent usually has two primary pieces of information:


○ Action to be performed (for example, ACTION_VIEW,
ACTION_EDIT, ACTION_MAIN)
○ Data to operate on (for example, a person’s record in the contacts
database)
● Commonly used to specify a request to transition to another Activity

Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Explicit intent

● Fulfills a request using a specific component


● Navigates internally to an Activity in your app
● Navigates to a specific third-party app or another app you've
written

Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Explicit intent examples
Navigate between activities in your app:

Navigate to a specific external app:

Android Development with Kotlin This work is licensed under the Apache 2 license. 7
Implicit intent

● Provides generic action the app can perform


● Resolved using mapping of the data type and action to known
components
● Allows any app that matches the criteria to handle the request

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

Define menu items in XML menu resource (located in res/menu folder)

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

● Represents a behavior or portion of the UI in an activity


("microactivity")
● Must be hosted in an activity
● Lifecycle tied to host activity's lifecycle
● Can be added or removed at runtime

Android Development with Kotlin This work is licensed under the Apache 2 license. 20
Note about fragments

Use the AndroidX version of the Fragment class.


(androidx.fragment.app.Fragment).

Don't use the platform version of the Fragment class


(android.app.Fragment), which was deprecated.

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

In build.gradle, under 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

● Fragment destinations are denoted by the action tag in the


navigation graph.
● Actions can be defined in XML directly or in the Navigation Editor by
dragging from source to destination.
● Autogenerated action IDs take the form of
action_<sourceFragment>_to_<destinationFragment>.

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)

NavController manages UI navigation in a navigation host.


● Specifying a destination path only names the action, but it
doesn’t execute it.
● To follow a path, use 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:

In the app's or module's build.gradle file:

Android Development with Kotlin This work is licensed under the Apache 2 license. 35
Sending data to a Fragment

1. Create arguments the destination fragment will expect.


2. Create action to link from source to destination.
3. Set the arguments in the action method on
<Source>FragmentDirections.
4. Navigate according to that action using the Navigation Controller.
5. Retrieve the arguments in the destination 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

Type Type Syntax Supports Default Supports Null


app:argType=<type> Values Values

Serializable Fully qualified class name Yes (only "@null") Yes

Parcelable Fully qualified class name Yes (only "@null") Yes

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:

Set up NavigationView for use with a NavController:

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

Activity 2 Activity 2 Activity 2

Activity 1 Activity 1 Activity 1

Back stack Back stack Back stack

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

Fragment 1 Fragment 1 Fragment 1

Activity 2 Activity 2 Activity 2

Activity 1 Activity 1 Activity 1

Back stack Back stack Back stack

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

Practice what you’ve learned by


completing the pathway:
Lesson 6: App navigation

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

● Preserve user data and state if:


○ User temporarily leaves app and then returns
○ User is interrupted (for example, a phone call)
○ User rotates device
● Avoid memory leaks and app crashes.

Android Development with Kotlin This work is licensed under the Apache 2 license. 4
Simplified activity lifecycle
Activity launched

App is running

Activity shut down

Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Activity lifecycle
Activity launched

Activity running

Activity shut down

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

● Activity is created and other initialization work occurs


● You must implement this callback
● Inflate activity UI and perform other app startup logic

Android Development with Kotlin This work is licensed under the Apache 2 license. 8
onStart()

● Activity becomes visible to the user


● Called after activity:
○ onCreate()
or
○ onRestart() if activity was previously stopped

Android Development with Kotlin This work is licensed under the Apache 2 license. 9
onResume()

● Activity gains input focus:


○ User can interact with the activity
● Activity stays in resumed state until system triggers activity to
be paused

Android Development with Kotlin This work is licensed under the Apache 2 license. 10
onPause()

● Activity has lost focus (not in foreground)


● Activity is still visible, but user is not actively interacting with it
● Counterpart to onResume()

Android Development with Kotlin This work is licensed under the Apache 2 license. 11
onStop()

● Activity is no longer visible to the user


● Release resources that aren’t needed anymore
● Save any persistent state that the user is in the process of
editing so they don’t lose their work

Android Development with Kotlin This work is licensed under the Apache 2 license. 12
onDestroy()

● Activity is about to be destroyed, which can be caused by:


○ Activity has finished or been dismissed by the user
○ Configuration change
● Perform any final cleanup of resources.
● Don’t rely on this method to save user data (do that earlier)

Android Development with Kotlin This work is licensed under the Apache 2 license. 13
Summary of activity states
State Callbacks Description

Created onCreate() Activity is being initialized.

Started onStart() Activity is visible to the user.

Resumed onResume() Activity has input focus.

Paused onPause() Activity does not have input focus.

Stopped onStop() Activity is no longer visible.

Destroyed onDestroy() Activity is destroyed.

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

Priority level Log method


Verbose Log.v(String, String)
Debug Log.d(String, String)
Info Log.i(String, String)
Warning Log.w(String, String)
Error Log.e(String, String)

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

● Called when a fragment is attached to a context


● Immediately precedes onCreate()

Android Development with Kotlin This work is licensed under the Apache 2 license. 22
onCreateView()

● Called to create the view hierarchy associated with the


fragment
● Inflate the fragment layout here and return the root view

Android Development with Kotlin This work is licensed under the Apache 2 license. 23
onViewCreated()

● Called when view hierarchy has already been created


● Perform any remaining initialization here (for example, restore
state from Bundle)

Android Development with Kotlin This work is licensed under the Apache 2 license. 24
onDestroyView() and onDetach()

● onDestroyView() is called when view hierarchy of fragment


is removed.
● onDetach() is called when fragment is no longer attached to
the host.

Android Development with Kotlin This work is licensed under the Apache 2 license. 25
Summary of fragment states
State Callbacks Description

Initialized onAttach() Fragment is attached to host.

Created onCreate(), onCreateView(), Fragment is created and layout is being


onViewCreated() initialized.
Started onStart() Fragment is started and visible.

Resumed onResume() Fragment has input focus.

Paused onPause() Fragment no longer has input focus.

Stopped onStop() Fragment is not visible.

Destroyed onDestroyView(), onDestroy(), Fragment is removed from host.


onDetach()

Android Development with Kotlin This work is licensed under the Apache 2 license. 26
Save fragment state across config changes

Preserve UI state in fragments by storing state in Bundle:


● onSaveInstanceState(outState: Bundle)

Retrieve that data by receiving the Bundle in these fragment


callbacks:
● onCreate()
● onCreateView()
● onViewCreated()

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

Adjust their behavior based on activity or fragment lifecycle


● Use the androidx.lifecycle library
● Lifecycle tracks the lifecycle state of an activity or fragment
○ Holds current lifecycle state
○ Dispatches lifecycle events (when there are state changes)

Android Development with Kotlin This work is licensed under the Apache 2 license. 29
LifecycleOwner

● Interface that says this class has a lifecycle


● Implementers must implement getLifecycle() method
Examples: Fragment and AppCompatActivity are
implementations of LifecycleOwner

Android Development with Kotlin This work is licensed under the Apache 2 license. 30
LifecycleObserver
Implement LifecycleObserver interface:

Add the observer to the lifecycle:

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

popped off the stack

ComposeActivity

EmailActivity

Back stack

Android Development with Kotlin This work is licensed under the Apache 2 license. 36
Tap Back button again

ComposeActivity

popped off the stack

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

pop additional destinations Question3Fragment


off the back stack
Welcome Question2Fragment
fragment
Question1Fragment

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

● Understand the Activity Lifecycle


● Activity class
● Fragments guide and lifecycle
● Fragment class

Android Development with Kotlin This work is licensed under the Apache 2 license. 45
Pathway

Practice what you’ve learned by


completing the pathway:
Lesson 7: Activity and Fragment
Lifecycles

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

● External factors, such as tight deadlines, can lead to poor


decisions about app design and structure.
● Decisions have consequences for future work (app can be
harder to maintain long-term).
● Need to balance on-time delivery and future maintenance
burden.

Android Development with Kotlin This work is licensed under the Apache 2 license. 4
Examples of short-term hacks

● Tailoring your app to a specific device


● Blindly copying and pasting code into your files
● Placing all business logic in activity file
● Hardcoding user-facing strings in your code

Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Why you need good app architecture

● Clearly defines where specific business logic belongs


● Makes it easier for developers to collaborate
● Makes your code easier to test
● Lets you benefit from already-solved problems
● Saves time and reduces technical debt as you extend your app

Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Android Jetpack

● Android libraries that incorporate best practices and provide


backward compatibility in your apps
● Jetpack comprises the androidx.* package libraries

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

● Architecture design patterns, like MVVM and MVI, describe a


loose template for what the structure of your app should be.
● Jetpack architecture components help you design robust,
testable, and maintainable apps.

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

● Prepares data for the UI


● Must not reference activity, fragment, or views in view hierarchy
● Scoped to a lifecycle (which activity and fragment have)
● Enables data to survive configuration changes
● Survives as long as the scope is alive

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

Within MainActivity onCreate():

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)

● ViewModels can work in concert with data binding

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

Specify ViewModels in the data tag of a binding.

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

● Subject maintains list of observers to notify when state changes.


● Observers receive state changes from subject and execute
appropriate code.
● Observers can be added or removed at any time.

Android Development with Kotlin This work is licensed under the Apache 2 license. 26
Observer design pattern diagram

Observable

notify observe() notify observe()

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)

T is the type of data that’s stored in LiveData or MutableLiveData.

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:

Create observer to update team A score on screen:

Add the observer onto scoreA LiveData in ViewModel:

Android Development with Kotlin This work is licensed under the Apache 2 license. 31
Two-way data binding

● We already have two-way binding with ViewModel and


LiveData.
● Binding to LiveData in XML eliminates need for an observer in
code.

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

LiveData can be transformed into a new LiveData object.


● map()
● switchMap()

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

● Guide to app architecture


● Android Jetpack
● ViewModel Overview
● Android architecture sample app
● ViewModelProvider
● Lifecycle Aware Data Loading with Architecture Components
● ViewModels and LiveData: Patterns + AntiPatterns

Android Development with Kotlin This work is licensed under the Apache 2 license. 41
Pathway

Practice what you’ve learned by


completing the pathway:
Lesson 8: App architecture (UI layer)

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:

● Tables Example Database

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.

● Create new tables


● Query for data
● Insert new data
● Update data
● Delete data

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

Create INSERT INTO colors VALUES ("red", "#FF0000");

Read SELECT * from colors;

Update UPDATE colors SET hex="#DD0000" WHERE name="red";

Delete DELETE FROM colors WHERE name = "red";

Android Development with Kotlin This work is licensed under the Apache 2 license. 8
Interacting directly with a database

● No compile-time verification of raw SQL queries

● Need lots of boilerplate code to convert between


SQL queries data objects

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

Rest of the app code

Color("#FF0000", "red")
Room

Colors Data access object Color("#4CAF50", "green")


database
Color("#1155CC", "blue")

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

● Provide extra information to the compiler


@Entity marks entity class, @Dao for DAO, @Database for database
● Can take parameters
@Entity(tableName = "colors")
● Can autogenerate code for you

Android Development with Kotlin This work is licensed under the Apache 2 license. 16
Entity

Class that maps to a SQLite database table


● @Entity
● @PrimaryKey
● @ColumnInfo

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)

Work with DAO classes instead of accessing database directly:


● Define database interactions in the DAO.
● Declare DAO as an interface or abstract class.
● Room creates DAO implementation at compile time.
● Room verifies all of your DAO queries at compile-time.

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

● Annotate class with @Database and include list of entities:


@Database(entities = [Color::class], version = 1)

● Declare abstract class that extends RoomDatabase:


abstract class ColorDatabase : RoomDatabase() {

○ Declare abstract method with no args that returns the DAO:


abstract fun colorDao(): ColorDao

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

Get the DAO from the database:


val colorDao = ColorDatabase.getInstance(application).colorDao()

Create new Color and use DAO to insert it into database:


val newColor = Color(hex = "#6200EE", name = "purple")
colorDao.insert(newColor)

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

● Limited time to do tasks and remain responsive


● Balanced with the need to execute long-running tasks
● Control over how and where tasks are executed

Android Development with Kotlin This work is licensed under the Apache 2 license. 31
Async programming on Android

● Threading
● Callbacks
● Plus many other options

What is the recommended way?

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

● Keep your app responsive while managing long-running tasks.


● Simplify asynchronous code in your Android app.
● Write code in sequential way
● Handle exceptions with try/catch block

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

● Add suspend modifier


● Must be called by other suspend functions or coroutines

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

Dispatcher Description of work Examples of work

Dispatchers.Main UI and nonblocking Updating LiveData, calling


(short) tasks suspend functions

Dispatchers.IO Network and disk tasks Database, file IO

Dispatchers.Default CPU intensive Parsing JSON

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

● launch - no result needed

● async - can return a result

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

Practice what you’ve learned by


completing the pathway:
Lesson 9: App architecture
(persistence)

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

● Widget for displaying lists of data


● "Recycles" (reuses) item views to make scrolling more
performant
● Can specify a list item layout for each item in the dataset
● Supports animations and transitions

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

Determines the transformations needed to translate one list into another



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

Let you map a function to an attribute in your XML

● Override existing framework behavior:


android:text = "foo" → TextView.setText("foo") is called

● Create your own custom attributes:


app:base2Number = "5" → TextView.setBase2Number("5") is called

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:

Example list item

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

● Adapter should know about two item view types:


○ Item that displays a number
○ Item that displays a color
enum class ITEM_VIEW_TYPE { NUMBER, COLOR }

● Modify getItemViewType() to return the appropriate type (as Int):


override fun getItemViewType(position: Int): Int

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

In MainActivity onCreate(), once you have a reference to the RecyclerView


● Display a list with LinearLayoutManager:
recyclerView.layoutManager = LinearLayoutManager(this)

● Display a grid with GridLayoutManager:


recyclerView.layoutManager = GridLayoutManager(this, 2)

● Use a different layout manager (or create your own)

Android Development with Kotlin This work is licensed under the Apache 2 license. 36
GridLayoutManager

● Arranges items in a grid as a table of rows and columns.


● Orientation can be vertically or horizontally scrollable.
● By default, each item occupies 1 span.
● You can vary the number of spans an item takes up (span size).

Android Development with Kotlin This work is licensed under the Apache 2 license. 37
Set span size for an item

Create SpanSizeLookup instance and override getSpanSize(position):

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

Practice what you’ve learned by


completing the pathway:
Lesson 10: Advanced RecyclerView
use cases

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

● Protect the privacy of an Android user


● Declared with the <uses-permission> tag in the
AndroidManifest.xml

Android Development with Kotlin This work is licensed under the Apache 2 license. 4
Permissions granted to your app

● Permissions can be granted during installation or runtime,


depending on protection level.
● Each permission has a protection level: normal, signature, or
dangerous.
● For permissions granted during runtime, prompt users to
explicitly grant or deny access to your app.

Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Permission protection levels

Protection Level Granted when? Must prompt Examples


before use?

Normal Install time No

Signature Install time No N/A

Dangerous Runtime Yes

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

● Prompt the user to grant the permission when they try to


access functionality that requires a dangerous permission.
● Explain to the user why the permission is needed.
● Fall back gracefully if the user denies the permission (app
should still function).

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

● Only use the permissions necessary for your app to work.


● Pay attention to permissions required by libraries.
● Be transparent.
● Make system accesses explicit.

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?

● Builds on industry standard libraries, like OkHttp, that provide:


○ HTTP/2 support
○ Connection pooling
○ Response caching and enhanced security
● Frees the developer from the scaffolding setup needed to run a
request

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

URL DESCRIPTION METHOD

Get a list of all posts

Get a list of posts by


user
Search posts using a
filter
Create a new post

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

Helps convert from a response type into class objects


● JSON (Gson or Moshi)
● XML (Jackson, SimpleXML, JAXB)
● Protocol buffers
● Scalars (primitives, boxed, and Strings)

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

Launch a new coroutine in the view model:

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

● Third-party image-loading library in Android


● Focused on performance for smoother scrolling
● Supports images, video stills, and animated GIFs

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

● Apply a crop to an image


● Apply transitions
● Set options for placeholder image or error image
● Set caching policies

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

● App permissions best practices


● Retrofit
● Moshi
● Glide

Android Development with Kotlin This work is licensed under the Apache 2 license. 36
Pathway

Practice what you’ve learned by


completing the pathway:
Lesson 11: Connect to the internet

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

● Protect the privacy of an Android user


● Declared with the <uses-permission> tag in the
AndroidManifest.xml

Android Development with Kotlin This work is licensed under the Apache 2 license. 4
Permissions granted to your app

● Permissions can be granted during installation or runtime,


depending on protection level.
● Each permission has a protection level: normal, signature, or
dangerous.
● For permissions granted during runtime, prompt users to
explicitly grant or deny access to your app.

Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Permission protection levels

Protection Level Granted when? Must prompt Examples


before use?

Normal Install time No

Signature Install time No N/A

Dangerous Runtime Yes

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

● Prompt the user to grant the permission when they try to


access functionality that requires a dangerous permission.
● Explain to the user why the permission is needed.
● Fall back gracefully if the user denies the permission (app
should still function).

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

● Only use the permissions necessary for your app to work.


● Pay attention to permissions required by libraries.
● Be transparent.
● Make system accesses explicit.

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?

● Builds on industry standard libraries, like OkHttp, that provide:


○ HTTP/2 support
○ Connection pooling
○ Response caching and enhanced security
● Frees the developer from the scaffolding setup needed to run a
request

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

URL DESCRIPTION METHOD

Get a list of all posts

Get a list of posts by


user
Search posts using a
filter
Create a new post

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

Helps convert from a response type into class objects


● JSON (Gson or Moshi)
● XML (Jackson, SimpleXML, JAXB)
● Protocol buffers
● Scalars (primitives, boxed, and Strings)

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

Launch a new coroutine in the view model:

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

● Third-party image-loading library in Android


● Focused on performance for smoother scrolling
● Supports images, video stills, and animated GIFs

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

● Apply a crop to an image


● Apply transitions
● Set options for placeholder image or error image
● Set caching policies

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

● App permissions best practices


● Retrofit
● Moshi
● Glide

Android Development with Kotlin This work is licensed under the Apache 2 license. 36
Pathway

Practice what you’ve learned by


completing the pathway:
Lesson 11: Connect to the internet

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

Operation Relative speed

Reading from LiveData FAST

Reading from Room database SLOW

Reading from network SLOWEST

Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Cache network responses

● Account for long network request times and still be responsive


to the user
● Use Room locally when retrieval from the network may be costly
or difficult
● Can cache based on the least recently used (LRU) value,
frequently accessed (FRU) values, or other algorithm

Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Repository pattern

● Can abstract away multiple data sources from the caller


● Supports fast retrieval using local database while sending
network request for data refresh (which can take longer)
● Can test data sources separately from other aspects of your app

Android Development with Kotlin This work is licensed under the Apache 2 license. 7
App architecture with repository pattern
UI Controller

ViewModel

Repository

Remote data source Room Mock backend

Android Development with Kotlin This work is licensed under the Apache 2 license. 8
Implement a repository class

● Provide a common interface to access data:


○ Expose functions to query and modify the underlying data
● Depending on your data sources, the repository can:
○ Hold a reference to the DAO, if your data is in a database
○ Make network requests if you connect to a web service

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 Jetpack architecture component


● Recommended solution to execute background work
(immediate or deferred)
● Opportunistic and guaranteed execution
● Execution can be based on certain conditions

Android Development with Kotlin This work is licensed under the Apache 2 license. 11
When to use WorkManager

Task

Yes Requires active No


user

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

● Worker - does the work on a background thread, override


doWork() method
● WorkRequest - request to do some work
● Constraint - conditions on when the work can run
● WorkManager - schedules the WorkRequest to be run

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

● Can be scheduled to run once or repeatedly


○ OneTimeWorkRequest
○ PeriodicWorkRequest
● Persisted across device reboots
● Can be chained to run sequentially or in parallel
● Can have constraints under which they will run

Android Development with Kotlin This work is licensed under the Apache 2 license. 17
Schedule a OneTimeWorkRequest

Create WorkRequest:

Add the work to the WorkManager queue:

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)

Flex period Flex period Flex period


can run work can run work can run work

...
Interval 1 Interval 2 Interval N

Specify an interval using TimeUnit (e.g., TimeUnit.HOURS, TimeUnit.DAYS)

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

Result status Result status with output

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

Practice what you’ve learned by


completing the pathway:
Lesson 12: Repository pattern and
WorkManager

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

● Used to specify the visual design of your app


● Helps you maintain a consistent look across your app
● Hierarchical (you can inherit from parent styles and override
specific attributes)

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

● Collection of named resources, useful broadly across the app


● Named resources are known as theme attributes
● Examples:
○ Use a theme to define primary & secondary colors in the app
○ Use a theme to set the default font for all text within an activity

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:

Use ?attr/themeAttributeName syntax.


Examples: ?attr/colorPrimary
?attr/colorPrimaryVariant

Android Development with Kotlin This work is licensed under the Apache 2 license. 9
Styles

● A style is a collection of view attributes, specific to a


type of view
● Use a style to create a collection of reusable styling
information, such as font size or colors
● Good for declaring small sets of common designs used
throughout your app

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

On a view in a layout file:

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

● Use view attributes to set attributes explicitly for each view


● You can use every property that can be set via styles or themes
● Use for custom or one-off designs such as margins, paddings,
or constraints

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:

Specified as hexadecimal colors in form of #AARRGGBB

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

● Declare your dimension values in res/values/dimens.xml:

● Refer to them as @dimen/<name> in layouts or R.dimen.<name> in code:


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)

● The textual equivalent to


density-independent pixels (dp)
● Specify text sizes in sp
(takes into account user preferences)
● Users can adjust Font and Display sizes
in the Settings app (after Display)

Android Development with Kotlin This work is licensed under the Apache 2 license. 20
Type scale

● A set of styles designed to work


together in a cohesive manner
for your app and content
● Contains reusable categories of
text with intended purpose for
each (for example, headline,
subtitle, caption)

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

Interactive building blocks


for creating a user interface

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

Light mode Dark mode

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

● Text fields ● App bars (top and bottom)


● Buttons ● Floating Action Button (FAB)
● Menus ● Navigation Drawer
● Cards ● Bottom navigation
● Chips ● Snackbar
...and more!
Android Development with Kotlin This work is licensed under the Apache 2 license. 37
Text field
● Composed of TextInputLayout with child view
TextInputEditText
● Shows a floating label or a text hint before the user enters text
● Two types:

Filled text field Outlined text field

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

● Allows movement between top level


destinations in your app
● Alternate design pattern to a
navigation drawer
● Limited to 5 locations max

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

● Display short messages within the app


● Messages have a duration (SHORT,
LONG, or INDEFINITE)
● May contain an optional action
● Works best in a CoordinatorLayout
● Shown at bottom of enclosing container

Android Development with Kotlin This work is licensed under the Apache 2 license. 43
Snackbar examples
Show a simple message:

Add an action to a Snackbar:

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

● Acts as top-level container in an app


● Manages interaction of its child views, such as gestures
● Recommended for use with views like a Snackbar or FAB

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

● A card holds content and actions


for a single item.
● Cards are often arranged in a list,
grid, or dashboard.
● Use MaterialCardView.

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

● Users can choose a language that uses right-to-left (RTL) scripts.


● Add android:supportsRtl="true" to app tag in manifest.
● Convert left and right to start and end, respectively, in your layout
files (change android:paddingLeft to android:paddingStart).
● Localize strings and format text in messages.
● Optionally, use -ldrtl resource qualifier to provide alternate resources.

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

Sunflower Google I/O


app app

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

Practice what you’ve learned by


completing the pathway:
Lesson 13: App UI Design

Android Development with Kotlin This work is licensed under the Apache 2 license. 59

You might also like