Create a Flashcard App With Jetpack Compose ● Basic computer literacy ● Basic math skills ● Computer ● Internet connection ● (Optional) Android device & USB cable Prerequisites Here are some the prerequisites that will be helpful. Having basic computer literacy and basic math skills is recommended. You’ll also need a computer and access to the internet to take the online course. [Mention WiFi instructions if necessary.] ● Build your first Android apps ● Learn the basics of the Kotlin programming language ● Learn Jetpack Compose ● Discover resources to continue learning Learning Objectives Kotlin Programming Language Use Kotlin to start writing Android apps. Kotlin helps developers be more productive. In this workshop, you’ll learn how to create Android app UI using Kotlin and Jetpack Compose. We’ll talk about Jetpack Compose in a minute but first, Kotlin. Kotlin is a programming language recommended by Google for creating new Android apps. It’s a modern and popular programming language, known for helping developers be more productive and more concise when writing code. As a result of many great language features, Kotlin has quickly gained momentum in industry and is used by over 50% of professional Android developers. Let’s check out the basics of Kotlin. [Read about Android’s Kotlin-first approach] Kotlin Playground Write and run Kotlin code in the browser. https://bit.ly/kotlin-pg To make it easier for you to learn, you’ll be writing your code in the Kotlin Playground which you can access via the web browser. The site looks something like this. You can write your code in this window and run it by hitting the green Run button. The result of your code (known as the output) will show up at the bottom of the window (where it says “Hello, world!”). To illustrate a few important concepts that you’ll learn in this workshop, we will go through a short code demo to create a program in Kotlin. main Function The main function is the entry point, or starting point, of the program. Start here fun main() { println("Hello, world!") } Output: Hello, world! A Kotlin program is required to have a main function , which is the entry point, or starting point, of the program. How many people here don’t know what a function is? Functions A function is a segment of a program that performs a specific task. You can have many functions in your program or only a single one. A function is a segment of a program that performs a specific task. You can have many functions in your program or only a single one. Creating separate functions for specific tasks has a number of benefits. ● Reusable code: Rather than copying and pasting code that you need to use more than once, you can simply call a function wherever needed. ● Readability: Ensuring functions do one and only one specific task helps other developers and teammates, as well as your future self to know exactly what a piece of code does. Defining a function Functions begin with the fun keyword. fun displayIntroduction() { } We will demonstrate how to define a function with a function called displayIntroduction() that we will use to print our name and age. A function definition in Kotlin starts with the fun keyword. A keyword is a reserved word that has a special meaning in Kotlin, in this case the fun keyword tells Kotlin that you are going to make a function. Defining a function Functions have a name so that they can be called. fun displayIntroduction() { } Functions need to have a descriptive name so that they can be called from other parts of the program. Defining a function Functions need a set of parentheses after the function name in order to surround the function inputs. fun displayIntroduction() { } Functions need a set of parentheses which you can use to optionally pass information into the function. displayIntroduction() won’t need information passed in. You will learn more about passing in inputs when we talk about Jetpack Compose. Defining a function The curly braces make up the function body and contain the instructions needed to execute a task. fun displayIntroduction() { } Functions need curly braces that contain the instructions needed to execute a task. Putting it together fun displayIntroduction() { // We will fill this out! } Output: Hi I’m Megha and I am 28 years old The task of the displayIntroduction() function, is to print your name and age. In order to do that you will save both your name and age into variables. Basic data types Kotlin Data type What kind of data it can contain Example literal values String Text “Add contact” “Search” Int Whole integer number 32 -59873 Double Decimal number 2.0 -37123.9999 Float Decimal number (less precise than a Double). Has an f or F at the end of the number. 5.0f -1630.209f Boolean true or false. Use this data type when there are only two possible values. true false When you decide what aspects of your app can be variables, it's important to specify what type of data can be stored in those variables. In Kotlin, there are some common basic data types. This table shows a different data type in each row. For each data type, there's a description of what kind of data it can hold and example values. A String holds text so you will use it to store your name, and an Int holds an integer number so you will use it to store your age. val keyword Use when you expect the variable value will not change. Example: name var keyword Use when you expect the variable value can change. Example: age Defining a variable Now, let’s jump into how you define a variable. You can declare a variable using either val or var With val , the variable is read-only , which means you can only read, or access, the value of the variable. Once the value is set, you cannot edit or modify its value. With var , the variable is mutable , which means the value can be changed or modified. The value can be mutated. In Kotlin, it's preferred to use val over var when possible. We will store your name as a val because that will not change. We will store your age as a var because it changes every year. Defining a variable Variables start with a var or val keyword. fun displayIntroduction() { val name: String = "Megha" var age: Int = 28 } To demonstrate how to define a variable we will define both name and age variables. Before you use a variable, you must declare it. To declare a variable, start with the val or var keyword. Defining a variable All variables must have a name. fun displayIntroduction() { val name: String = "Megha" var age: Int = 28 } All variables must have a name that they can be referenced by. Defining a variable Data type is the type of data that the variable holds. fun displayIntroduction() { val name: String = "Megha" var age: Int = 28 } The data type specifies the type of data that the variable holds. Note that a colon separates the name and data type. Defining a variable The initial value is the value that is stored in the variable. fun displayIntroduction() { val name: String = "Meghan" var age: Int = 28 } In the variable declaration, the equal sign symbol (=) follows the data type. The equal sign symbol is called the assignment operator . The assignment operator assigns a value to the variable. The variable’s initial value is the data stored in the variable. Putting it together fun displayIntroduction() { val name = "Megha" val age = 28 println("Hi I'm $name and I am $age years old") } Let’s finish putting the displayIntroduction() function together. We have our variables but they don’t do anything yet. Let’s add a print statement to print out your introduction using println to print to the output in Kotlin Playground. In order to print your variables, you will use String templates which allow you to include variable references in a string by using the $ sign before the variable name. [You can learn more about String Templates here] Putting it together fun main() { displayIntroduction() } fun displayIntroduction() { val name = "Megha" val age = 28 println("Hi I'm $name and I am $age years old") } Output: Hi I’m Megha and I am 28 years old Finally, we will replace the contents of the main() function with a call to the displayIntroduction() function when we run it, “Hi I’m Megha and I am 28 years old” will print to the output.