Android with Kotlin Course Notes
Lesson 01: Build your First App
Build your first app: “Dice Roller” that covers basic Android components like displaying texts and images as well as a tour of the Android tools you’ll be using throughout this course.
Android project contains
- Kotlin files for the core logic of the app
- A resources folder for static content such as images and strings
- An Android Manifest file that defines essential app details so the OS can launch your app
- Gradle scripts, for building and running your app
AndroidManifest.xml
The name in the manifest’s package
attribute should always match your project’s base package name where you keep your activities and other app code.
The package
attribute also represents your app’s universally unique application ID.
This intent-filter tells the OS where to start the app when the user clicks on the app icon.
Activity And Layout
- Main activity is an example of an activity.
- An activity is a core Android class that is responsible for drawing an app user interface and receiving input events.
- When your app launches it launches a specific activity this is the activity that was declared in the manifest with the correct intent-filter tag.
- Activities have an Associated layout file which in this case is activity underscore main.
- Layout files are XML files that express what the app actually looks like they do this by defining things like text images and buttons and where these things will appear on the screen. these text images and buttons are called views.
- The activity and the layout are connected by a process known as layout inflation this process is triggered when the activity starts.
setContentView
Set the activity content from a layout resource. The resource will be inflated, adding all top-level views to the activity.
Linear Layout
LinearLayout
is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation
attribute.
Gravity specifies how a component should be placed in its group of cells.
Best Practice
Always extract hard-coded text to strings.xml file. This will make changing strings easier and allows you to localize your app.
Calling elements
By using findViewById
to get a reference to the element.
- in xml layout file:
- in Kotlin file, set a button with:
- To change a property of the element you can call methods directly:
OnClickListener
Interface definition for a callback to be invoked when a view is clicked.
- setting a button OnClickListener
Toast
A toast is a view containing a quick little message for the user.
- Showing a Toast message
ImageView
Displays image resources, also commonly used to apply tints to an image and handle image scaling.
Best Practice
Always use lateinit to access certain element variable efficiently. Declare it at the beginning of the class.
XML namespace
-
XML namespaces are used for providing uniquely named elements and attributes in an XML document.
-
Android Studio supports a variety of XML attributes in the
tools
namespace that enable design-time features (such as which layout to show in a fragment) or compile-time behaviors (such as which shrinking mode to apply to your XML resources). When you build your app, the build tools remove these attributes so there is no effect on your APK size or runtime behavior.
Example:
Gradle
Gradle is an advanced build toolkit, to automate and manage the build process while allowing you to define flexible custom build configurations.
It controls:
- What devices run your app
- Compile to executable
- Dependency management
- App signing for Google Play
- Automated Tests
build.gradle
- Plugins repositories:
- Android app configuration:
- External Dependencies:
Vector Drawables
A VectorDrawable is a vector graphic defined in an XML file as a set of points, lines, and curves along with its associated color information. The major advantage of using a vector drawable is image scalability. It can be scaled without loss of display quality, which means the same file is resized for different screen densities without loss of image quality.
Vector drawables backward compatibility solution:
To support vector drawable and animated vector drawable on devices running platform versions lower than Android 5.0 (API level 21) Use the following code snippet to configure the vectorDrawables
element:
Then you can use the new app:srcCompat
attribute to reference vector drawables as well as any other drawable available to android:src
:
You’ll also need to add the namespace to the root of the layout
Summary
- To install Android Studio, go to Android Studio and follow the instructions to download and install it.
- To see an app’s Android hierarchy in the Project pane, click the Project tab in the vertical tab column. Then select Android in the drop-down menu at the top.
- When you need to add new dependencies to your project or change dependency versions, edit the
build.gradle(Module:app)
file. - All code and resources for an app are located within the
app
andres
folders. Thejava
folder includes activities, tests, and other components in Kotlin or Java source code (or both). Theres
folder holds resources, such as layouts, strings, and images. - To add features, components, and permissions to your Android app, edit the
AndroidManifest.xml
file. All app components, such as additional activities, must be declared in this XML file. - To create an Android virtual device (an emulator) to run your app, use the AVD Manager.
- To run your app on a physical Android device using Android Studio, enable USB debugging on the device. To do this, open Settings > About phone and tap Build number seven times. Then open Settings > Developer options and select USB debugging.
Activities
MainActivity
is a subclass ofAppCompatActivity
, which in turn is a subclass ofActivity
. AnActivity
is a core Android class that is responsible for drawing an Android app UI and receiving input events.- All activities have an associated layout file, which is an XML file in the app’s resources. The layout file is named for the activity, for example
activity_main.xml
. - The
setContentView()
method inMainActivity
associates the layout with the activity, and inflates that layout when the activity is created. - Layout inflation is a process where the views defined in the XML layout files are turned into (or “inflated” into) Kotlin view objects in memory. Once layout inflation happens, the
Activity
can draw these objects to the screen and dynamically modify them.
Views
- All UI elements in the app layout are subclasses of the
View
class and are called views.TextView
andButton
are examples of views. View
elements can be grouped inside aViewGroup
. A view group acts as a container for the views, or other view groups, within it.LinearLayout
is an example of a view group that arranges its views linearly.
View attributes
- The
android:layout_width
andandroid:layout_height
attributes indicate the width and height of a view. Thematch_parent
value stretches the view to its parent’s width or height. Thewrap_content
value shrinks the view to fit the view’s contents. - The
android:text
attribute indicates the text that a view should display (if that view displays text.) For buttons,android:text
is the button label. - The
android:orientation
attribute in aLinearLayout
view group arranges the view elements it contains. A value ofhorizontal
arranges views left to right. A value ofvertical
arranges the views top to bottom. - The
android:layout_gravity
attribute determines the placement of a view and all that view’s children. - The
android:textSize
attribute defines the size of the text in a text view. Text sizes are specified in sp units (scalable pixels). By using sp units, you can size text independently of the device’s display quality.
Strings
- Instead of hardcoding strings in the layout, it’s a best practice to use string resources.
- String resources are contained in the
res/values/string.xml
file. - To extract strings, use
Alt+Enter
(Option+Enter
on a Mac). Select Extract string resources from the popup menu.
Using views
- To connect your Kotlin code to a view that you defined in the layout, you need to get a reference to the view object after the view has been inflated. Assign an ID (
android:id
) to the view in the layout, then use thefindViewById()
method to get the associated view object. - When you create an ID for a view in the XML layout file, Android Studio creates an integer constant with that ID’s name in the generated
R
class. You can then use thatR.id
reference in thefindViewById()
method. - You can set the attributes of a view object in your Kotlin code directly by property name. For example, the text in a text view is defined by the
android:text
attribute in the XML, and it is defined by thetext
property in Kotlin. - A click handler is a method that is invoked when the user clicks or taps on a UI element. To attach a click-handler method to a view such as a button, use the
setOnClickListener()
method.
Using toasts
A toast is a view that shows the user a simple message in a small popup window.
To create a toast, call the makeText()
factory method on the Toast
class with three arguments:
- The context of the app
Activity
- The message to display, for example a string resource
- A duration, for example
Toast.LENGTH_SHORT
To display the toast, call show()
.
App resources:
- Your app’s resources can include images and icons, standard colors used in the app, strings, and XML layouts. All of those resources are stored in the
res
folder. - The
drawable
resources folder is where you should put all the image resources for your app.
Using vector drawables in image views:
- Vector drawables are images described in XML format. Vector drawables are more flexible than bitmap images (such as PNG files) because they can be scaled to any size or resolution.
- To add a drawable to your app’s layout, use an
<ImageView>
element. The source of the image is in theandroid:src
attribute. To refer to the drawable resource folder, use@drawable
, for example"@drawable/image_name"
. - Use the
ImageView
view in yourMainActivity
code for the image. You can usesetImageResource()
to change the view’s image to a different resource. UseR.drawable
to refer to specific drawables, for examplesetImageResource(R.drawable.image_name)
.
The lateinit
keyword:
- Minimize the calls to
findViewById()
in your code by declaring fields to hold those views, and initializing the fields inonCreate()
. Use thelateinit
keyword for the field to avoid needing to declare it nullable.
The tools
namespace for design-time attributes:
- Use the
tools:src
attribute in the<ImageView>
element in your layout to display an image in only Android Studio’s preview or design editor. You can then use an empty image forandroid:src
for the final app. - Use the
tools
namespace in the Android layout file to create placeholder content or hints for layout in Android Studio. Data declared bytools
attributes is not used in the final app.
API levels:
- Each Android OS has an official version number and name (for example Android 9.0, “Pie”) and an API level (API 28). Use the API levels in your app’s Gradle files to indicate the versions of Android your app supports.
- The
compileSdkVersion
parameter in thebuild.gradle
file specifies the Android API level that Gradle should use to compile your app. - The
targetSdkVersion
parameter specifies the most recent API level that you have tested your app against. In many cases this parameter has the same value ascompileSdkVersion
. - The
minSdkVersion
parameter specifies the oldest API level your app can run on.
Android Jetpack:
- Android Jetpack is a collection of libraries, developed by Google, that offers backward-compatible classes and helpful functions for supporting older versions of Android. Jetpack replaces and expands on the set of libraries formerly known as the Android Support Library.
- Classes imported from the
androidx
package refer to the Jetpack libraries. Dependencies to Jetpack in yourbuild.gradle
file also start withandroidx
.
Backward compatibility for vector drawables:
- Vector drawables are only natively supported in versions of Android higher than API 21. In older versions, Gradle generates PNG images for those drawables when your app is built.
- You can specify that the Android Support Library should be used for vector drawables in older API versions with the
vectorDrawables.useSupportLibrary = true
configuration parameter in thebuild.gradle
file. - Once you’ve enabled the support library for vector drawables, use the
app:srcCompat
attribute in the<ImageView>
element (instead ofandroid:src
) to specify the vector drawable source for that image.
The app
namespace:
- The
app
namespace in your XML layout file is for attributes that come from either your custom code or from libraries, not from the core Android framework.
Learn to help yourself
- Official Android developer documentation is at developer.android.com.
- Material Design is a conceptual design philosophy that outlines how apps should look and function on mobile devices. Material Design isn’t just for Android apps. The Material Design guidelines are at material.io.
- Android Studio provides templates for common and recommended app and activity designs. These templates offer working code for common use cases.
- When you create a project, you can choose a template for your first activity.
- While you are developing your app, you can create activities and other app components from built-in templates.
- Google Samples contains code samples that you can study, copy, and incorporate into your projects.