1
Lesson 4:
Build your first Android app
This work is licensed under the Apache 2 license.
Android Development with Kotlin v1.0
This work is licensed under the Apache 2 license.
About this lesson
Lesson 4: Build your first Android app
2
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Android Studio
Official IDE for building Android apps
3
Android Development with Kotlin
This work is licensed under the Apache 2 license.
4
Your first app
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Open Android Studio
5
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Create new project
6
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Enter your project details
7
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Android releases and API levels
8
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Choose API levels for your app
minSdkVersion <= targetSdkVersion <= compileSdkVersion
9
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.
Tour of Android Studio
10
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Run your app
11
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Android Virtual Device (AVD) Manager
12
Android Development with Kotlin
This work is licensed under the Apache 2 license.
13
Anatomy of an Android App
project
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Anatomy of a basic app project
14
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Android app project structure
MyApplication
├── app
│ ├── libs
│ └── src
│ ├── androidTest
│ ├── main
│ │ ├── java
│ │ ├── res
│ │ └── AndroidManifest.xml
│ └── test
├── build.gradle
└── gradlew
15
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Browse files in Android Studio
16
Android Development with Kotlin
This work is licensed under the Apache 2 license.
17
Layouts and resources in Android
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Views
18
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Layout Editor
19
Android Development with Kotlin
This work is licensed under the Apache 2 license.
XML Layouts
You can also edit your layout in XML.
20
Android Development with Kotlin
This work is licensed under the Apache 2 license.
XML for a TextView
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
21
Hello World!
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Size of a View
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_width="48dp"
22
Android Development with Kotlin
This work is licensed under the Apache 2 license.
ViewGroups
A ViewGroup is a container that determines how views are displayed.
23
FrameLayout
TextView
TextView
TextView
Button
LinearLayout
TextView
Button
ConstraintLayout
TextView
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.
FrameLayout example
A FrameLayout generally holds a single child View.
24
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"/>
</FrameLayout>
TextView
Android Development with Kotlin
This work is licensed under the Apache 2 license.
LinearLayout example
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView ... />
<TextView ... />
<Button ... />
</LinearLayout>
25
TextView
TextView
Button
Android Development with Kotlin
This work is licensed under the Apache 2 license.
View hierarchy
26
ImageView
TextView
Button
Button
Button
Button
LinearLayout
ImageView
TextView
LinearLayout
Android Development with Kotlin
This work is licensed under the Apache 2 license.
App resources
Static content or additional files that your code uses
27
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Common resource directories
Add resources to your app by including them in the appropriate resource directory under the parent res folder.
28
main
├── java
└── res
├── drawable
├── layout
├── mipmap
└── values
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Resource IDs
Examples:
29
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.
Resource IDs for views
Individual views can also have resource IDs.
30
Add the android:id attribute to the View in XML. Use @+id/name syntax.
<TextView
android:id="@+id/helloTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
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.
31
Activities
Android Development with Kotlin
This work is licensed under the Apache 2 license.
What’s an Activity?
32
Android Development with Kotlin
This work is licensed under the Apache 2 license.
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
33
Android Development with Kotlin
This work is licensed under the Apache 2 license.
How an Activity runs
34
Activity launched
App is running
Activity shut down
onCreate()
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Implement the onCreate() callback
Called when the system creates your Activity
35
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Layout inflation
36
Layout files
layout1
ViewGroup
View1
ViewGroup
View2
View3
Activity
LayoutInflater
layout2
layout3
Android Development with Kotlin
This work is licensed under the Apache 2 license.
37
Make an app interactive
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Define app behavior in Activity
Modify the Activity so the app responds to user input, such as a button tap.
38
Android Development with Kotlin
This work is licensed under the Apache 2 license.
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!"
39
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Set up listeners for specific events
40
User interacts with a View
An event is fired
Did developer register a callback?
Ignore the event
Execute the callback
No
Yes
Android Development with Kotlin
This work is licensed under the Apache 2 license.
View.OnClickListener
class MainActivity : AppCompatActivity(), View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
...
val button: Button = findViewById(R.id.button)
button.setOnClickListener(this)
}
override fun onClick(v: View?) {
TODO("not implemented")
}
}
41
Android Development with Kotlin
This work is licensed under the Apache 2 license.
SAM (single abstract method)
Converts a function into an implementation of an interface
Format: InterfaceName { lambda body }
val runnable = Runnable { println("Hi there") }
42
is equivalent to
val runnable = (object: Runnable {
override fun run() {
println("Hi there")
}
})
Android Development with Kotlin
This work is licensed under the Apache 2 license.
View.OnClickListener as a SAM
A more concise way to declare a click listener
43
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
...
val button: Button = findViewById(R.id.button)
button.setOnClickListener({ view -> /* do something*/ })
}
}
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Late initialization
class Student(val id: String) {
lateinit var records: HashSet<Any>
init {
// retrieve records given an id
}
}
44
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Lateinit example in Activity
class MainActivity : AppCompatActivity() {
lateinit var result: TextView
override fun onCreate(savedInstanceState: Bundle?) {
...
result = findViewById(R.id.result_text_view)
}
}
45
Android Development with Kotlin
This work is licensed under the Apache 2 license.
46
Gradle: Building an
Android app
Android Development with Kotlin
This work is licensed under the Apache 2 license.
What is Gradle?
47
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Gradle build file
48
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Plugins
Provide libraries and infrastructure needed by your app
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
49
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Android configuration
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.example.sample"
minSdkVersion 19
targetSdkVersion 30
}
}
50
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Dependencies
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
...
}
51
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Repositories
repositories {
google()
mavenCentral()
}
52
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Common Gradle tasks
53
Android Development with Kotlin
This work is licensed under the Apache 2 license.
54
Accessibility
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Accessibility
55
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Make apps more accessible
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:
57
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Accessibility Scanner example
58
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Add content labels
<ImageView
...
android:contentDescription="@string/stop_sign" />
59
Android Development with Kotlin
This work is licensed under the Apache 2 license.
No content label needed
android:importantForAccessibility="no"
60
Android Development with Kotlin
This work is licensed under the Apache 2 license.
TalkBack
61
Android Development with Kotlin
This work is licensed under the Apache 2 license.
TalkBack example
62
Reads text aloud as user navigates the screen
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Switch access
63
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Android Accessibility Suite
64
Collection of accessibility apps that help you use your Android device eyes-free, or with a switch device. It includes:
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Accessibility Resources
65
Android Development with Kotlin
This work is licensed under the Apache 2 license.
66
Summary
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Summary
67
In Lesson 4, you learned how to:
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Learn more
68
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Pathway
Practice what you’ve learned by�completing the pathway:
69
Android Development with Kotlin
This work is licensed under the Apache 2 license.