Unit 1: Kotlin basics for Android
Android Basics: Introduction to Kotlin
Write your first program in Kotlin
- https://developer.android.com/training/kotlinplayground is an interactive code editor on the web where you can practice writing Kotlin programs.
- All Kotlin programs need to have a
main()
function:fun main() {}
- Use the
println()
function to print a line of text. - Place text you want to print between double quotes. For example
"Hello"
. - Repeat the
println()
instruction to print multiple lines of text. - Errors are marked red in the program. There is an error message in the output pane to help you figure out where the error is and what might be causing it.
Create a birthday message in Kotlin
- Create a variable using the
val
keyword and a name. Once set, this value cannot be changed. Assign a value to a variable using the equal sign. Examples of values are text and numbers.
- Use
${}
to surround variables and calculations in the text of print statements. For example:${age}
whereage
is a variable.* AString
is text surrounded by quotes, such as"Hello"
.
- An
Int
is a whole positive or negative number, such as 0, 23, or -1024. - You can pass one or more arguments into a function for the function to use, for example:
- Use a
repeat() {}
statement to repeat a set of instructions several times. For example:
- A loop is an instruction to repeat instructions multiple times. A
repeat()
statement is an example of a loop. - You can nest loops, that is, put loops within loops. For example, you can create a
repeat()
statement within arepeat()
statement to print a symbol a number of times for a number of rows, like you did for the cake layers.
Summary of using function arguments: To use arguments with a function, you need to do three things:
- Add the argument and type to the function definition:
printBorder(border: String)
- Use the argument inside the function:
println(border)
- Supply the argument when you call the function:
printBorder(border)
Android Basics: Create your first Android app
Create and run your first Android app
- To create a new project, start Android Studio, click Start a new Android Studio project, name your project, choose a template (Usually Empty Activity), and fill in the details (Choose
API 19: Android 4.4 (KitKat)
as Minimum SDK). - To create an Android virtual device (an emulator) to run your app, choose Tools > AVD Manager and then use the AVD Manager to select a hardware device and system image.
- To run your app on a virtual device, make sure you have created a device, select the device from the toolbar dropdown menu, and then run your app by clicking the Run icon! choose Run > Run app or click the Run icon in the toolbar.
- To find your project files, in the Project window, select Project Source Files from the dropdown.
Android Basics: Build a basic layout
Create a Birthday Card app
- The Layout Editor helps you create the UI for your Android app.
- Almost everything you see on the screen of your app is a
View
.Views
can be interactive, like a clickable button or an editable input field. - A
TextView
is a UI element for displaying text in your app.
- A
ConstraintLayout
is a container for other UI elements.
Views
need to be constrained horizontally and vertically within aConstraintLayout
.- One way to position a
View
is with a margin. - A margin says how far a
View
is from an edge of the container it’s in. - You can set attributes on a
TextView
like the font, text size, and color.
Add images to your Android app
- TheResource Managerin Android Studio helps you add and organize your images and other resources.
- An
ImageView
is a UI element for displaying images in your app. ImageViews
should have a content description to help make your app more accessible.
- Text that is shown to the user like the birthday greeting should be extracted into a string resource to make it easier to translate your app into other languages.
Android Basics: Add a button to an app
Classes and object instances in Kotlin
- Call the
random()
function on anIntRange
to generate a random number:(1..6).random()
- Classes are like a blueprint of an object. They can have properties and behaviors, implemented as variables and functions.
- An instance of a class represents an object, often a physical object, such as a dice. You can call the actions on the object and change its attributes.
- You can supply values to a class when you create an instance. For example:
class Dice(val numSides: Int)
and then create an instance withDice(6)
.
- Functions can return something. Specify the data type to be returned in the function definition, and use a
return
statement in the function body to return something. For example:fun example(): Int { return 5 }
Create an interactive Dice Roller app
- The top-level or first activity is often called the
MainActivity
and is provided by the project template. For example, when the user scrolls through the list of apps on their device and taps on the “Dice Roller” app icon, the Android System will start up theMainActivity
of the app.
- Add a
Button
in an Android app using the Layout Editor.
- Modify the
MainActivity.kt
class to add interactive behavior to the app.
- Pop up a
Toast
message as a temporary solution to verify you’re on the right track.
- Set an on-click listener for a
Button
usingsetOnClickListener()
to add behavior for when aButton
is clicked.
- When the app is running, you can update the screen by calling methods on the
TextView
,Button
, or other UI elements in the layout. - Comment your code to help other people who are reading your code understand what your approach was.
- Reformat your code and clean up your code.
Add conditional behavior in Kotlin
- Use an
if
statement to set a condition for executing some instructions. For example, if the user rolls the lucky number, print a winning message.
- The
Boolean
data type has values oftrue
andfalse
and can be used for decision making. - Compare values using operators such as greater than (
>
), less than (<
), and equal to (-). - Use a chain of
else if
statements to set multiple conditions. For example, print a different message for each possible dice roll. - Use an
else
statement at the end of a chain of conditions to catch any cases that may not be covered explicitly. If you cover the cases for 6-sided dice, anelse
statement would catch the 7 and 8 numbers rolled with an 8-sided dice.
- Use a
when
statement as a compact form of executing code based on comparing a value.
General form of if-else
if (condition-is-true) {
execute-this-code
} else if (condition-is-true) {
execute-this-code
} else {
execute-this-code
}
When statement
when (variable) {
matches-value -> execute-this-code
matches-value -> execute-this-code
}
Add images to the Dice Roller app
- Use
setImageResource()
to change the image that’s displayed in anImageView
- Use control flow statements like
if / else
expressions orwhen
expressions to handle different cases in your app, for example, showing different images under different circumstances.