WhoisAbel / AndroidBasicsInKotlin

In this project, you'll learn the basics of building Android apps with the Kotlin programming language. Along the way, you'll develop a collection of apps to start your journey as an Android developer.
0 stars 0 forks source link

Unit 1: Add a button to an app #4

Closed WhoisAbel closed 3 years ago

WhoisAbel commented 3 years ago

Learn more concepts in Kotlin—including classes, objects, and conditionals—to create an interactive app for your users.

WhoisAbel commented 3 years ago

IntRange

Previously, you learned that there are types of data like Int for integer numbers and String for text. IntRange is another data type, and it represents a range of integer numbers from a starting point to an endpoint.

val diceRange = 1..6

You can call functions directly on a range, for example: (1..6).random().

WhoisAbel commented 3 years ago

Note

  1. Similar to using the fun keyword in Kotlin to create a new function, use the class keyword to create a new class.
  2. You can choose any name for a class, but it is helpful if the name indicates what the class represents. By convention, the class name is written in Upper Camel Case (also called Pascal Casing). For example: Car, ParkingMeter, and CustomerRecord are all valid class names, and you can guess at what they represent.
WhoisAbel commented 3 years ago

With this class, you have a blueprint of object in real world .

Classes are like a blueprint of an object. They can have properties and behaviors, implemented as variables and functions.

image

WhoisAbel commented 3 years ago

When there are Views within a ViewGroup, the Views are considered children of the parent ViewGroup. In the case of your app, the TextView and Button would be considered children of the parent ConstraintLayout.

image image

WhoisAbel commented 3 years ago

Introduction to Activities

An Activity provides the window in which your app draws its UI. Typically, an Activity takes up the whole screen of your running app. Every app has one or more activities. The top-level or first activity is often called the MainActivity and is provided by the project template.

WhoisAbel commented 3 years ago

Enable auto imports

On macOS, open the settings by going to File > Settings > Editor > General > Auto Import. for New Projects... Expand Other Settings > Auto Import. In the Java and Kotlin sections, make sure Add unambiguous imports on the fly and Optimize imports on the fly (for current project) are checked. Note that there are two checkboxes in each section. Save the changes and close settings by pressing OK.

image

The unambiguous imports settings tell Android Studio to automatically add an import statement, as long as it can determine which one to use. The optimize imports settings tell Android Studio to remove any imports that aren't being used by your code.

WhoisAbel commented 3 years ago

Note:

Android automatically assigns ID numbers to the resources in your app. For example, the Roll button has a resource ID, and the string for the button text also has a resource ID. Resource IDs are of the form R..; for example, R.string.roll. For View IDs, the is id, for example, R.id.button

WhoisAbel commented 3 years ago

Use the rollButton object and set a click listener on it by calling the setOnClickListener() method. Instead of the parentheses following the method name, you will actually be using curly braces following the method name. This is a special syntax for declaring a Lambda,

WhoisAbel commented 3 years ago

Screen Shot 2021-10-21 at 7 08 45 PM

WhoisAbel commented 3 years ago

Use setImageResource() to change the image that's displayed in an ImageView