Building Android Apps with Kotlin
Introduction to building Android apps using the Kotlin Programming Language
Updated: 03 September 2023
From this Udacity Corse
Building your First App
Create a Project
To get started you will need to first install Android Studio and an Emulator if needed
To get started create a new Android Studio Project with the following settings, select the Empty Activity
and name the application DiceRoller with the Root Package com.example.DiceRoller
. Select the API Level 19
and
Name | Dice Roller |
---|---|
Package | com.nabeelvalley.diceroller |
Save Location | C:\Repos\AndroidStudio\DiceRoller |
Language | Kotlin |
Minimum API Level | API Level 19: Android 4.4 |
Use androidx.* artifacts |
Set up an Emulator
- In the debug toolbar click
Run (Green Play Button) > Add new Virtual Device
And select thePixel 2 with Play Store
and then select the newest API Emulator after downloading the image - Once the Emulator is downloaded you can select it
- Click Okay and the app should build and open on your Emulator
To use an app on your device configure the debugging See Intro To Android Notes
What’s in an App
The application when using the Android
project view, if you look at the application there are the following folders
java
is your application coderes
are the static resources for your appgeneratedJava
contains build outputsmanifests
contains manifest files with application detailsGradle Scripts
are generated files that configure the application build and installation
The MainActivity.kt
file and the activity_main.xml
files are the stuff we see when the application runs the Main Activity, this is declared in the AndroidManifest.xml
file:
The Objects in our Layout File (activity_main.xml
) are transformed into Kotlin Objects during the inflation process that we can work with and manipulate in the `MainActivity.xml
An Activity has a set of Lifecycle
Methods that are called during the application setup, for our activity we have the following code:
The R.layout.activity_main
is the layout that is being used, in the Layout file we can have:
The above layout can be previewed on Android Studio, but is essentially a ConstraintLayout
with just a TextView
in the center. The different nodes in the Layout file are called Views
, there are many types of views
Update the Layout
For our first layout we’ll use a LinearLayout
with an Image and Button in it. For now just replace the current View COntents with:
Looking at the XML we can see the different properties of the LinearLayout
and TextView
that are on the screen
Now let’s update the text and positioning of the layout to add a button and center everything on the screen, we can use the following code to do so:
Lastly double click on the word Roll, and a lightbulb should appear in the menu, select the Extract string resource
and it will be added to the values/strings.xml
file:
We can then reference it in the layout by replacing Roll
with @string/roll
Handle the Button
In order for us to access objects in the layout file we need to have a way to refer to the different elements, we can do this by searching for the relevant layout item. To make this easy we can define an ID for a view and this is generated as a constant in the R
class and we can use findViewById
in the Kotlin code and we can access this object
Set the Id for the button to modify the text when the activity starts up
- Set the ID on the button with
android:id="@+id/roll_button"
- Get the button from the
onCreate
method with
- Update the button text with
We can then add a listener
to handle the button click using and simply show a toast message. We make use of the setOnClickListener
to do this
We can make use of this handler to change the TextView, set the ID to @+id/result_text
Next we’ll move the handler to it’s own logic. Create a function in the class called handleRollClick
which will just replace the text for now, first be sure to add import kotlin.random.Random
and use that Random
Then reference this from the onClickHandler
with:
Clicking the button now should display a random number
Use an Image
First, download all the images from here and unzip the folder
The images we’re using are all vectors. Switch to Project
view and go to app > src > main > res > Drawable
and drag the images into the Drawable
folder
You can then preview these images in Android Studio
Now replace the result_text
with the below Image View that shows the empty_dice
image until the dice has been rolled
Then in the handler get the dice_image
view and set the imageResource
to the image based on the number we have
We should minimize the calls to findViewById
as this can be expensive. We should keep this reference in a field, we should create a lateinit
variable which allows us to say that a variable will not be used before we assign it initially
The MainActivity
can be updated to use the lateinit
variable by inializing in with the onCreate
function and then using that reference in the handleDiceRoll
function
We can also set the ImageView
to set some data for the preview data, this is helpful for when we are designing and our data is based on some image or data that will maybe be fetched during some loading process, we can show the data for this with the tools
namespace in the XML. For the ImageView
we can change the image src
for the preview by adding this:
So our resulting ImageView
will be:
Gradle
Gradle is the build system that is used for android, it does a few different things:
- Specify what devices can run an app
- Compile that application
- Dependency management
- App Signing
- Automated Testing
When running an application the Gradle file compiles the application into an APK
, there are two Gradle file types in the project, one for the entire project and one for each module
The Gradle website has some detail into what each of the different parts of this file are
Looking at the Project Gradle file in repositories
we can see which remote servers libraries will be downloaded from:
Next we specify the dependencies for the Project, these are reference the plugins needed:
If we look at the app module build.gradle
we can see the different plugins needed for a project
Next we can see the configuration for our module:
If we look further down we can see the dependencies needed for a specific module:
Updating App Compatability
Note that androidx
is included as a dependency for the application, this is a collection of libraries that are used to provide compatability across different android API levels
For us to make use of this for better image support, we can do the following:
- Enable the support library in the app
build.gradle
file in thedefaultConfig
:
- Replace
app:src
withapp:srcCompat
- Add the XML namespace to the app:
- Not sure how necessary this is but I also needed to change the
ImageView
toandroidx.appcompat.widget.AppCompatImageView
because the preview no longer worked after changing to usesrcCompat
The resulting ImageView
is:
If we want to be more correct about how we’re handling the Image view we can also change the type of our diceImage
property to be AppCompatImageView
which we import from androidx
, so our declaration looks like so:
Developing Layouts
All visual elements are views and are children of the View
class and have some common properties like height, width, background, and interactivity
Units for expressing dimensions are dp
which are Density Independent Pixels which allow size to be consistent over different device sizes
Views are organised as a heirarchy and ViewGroup
s are views that are used to contain other views, some view groups are:
LinearLayout
Horizontal or Vertial layoutsScrollView
for scrollable content
Deeper view heirarchies are more complex to render and can impact performance, the ConstraintLayout
helps you to arrange more complex layouts without deep nesting of Views
Create Project
Create a new project called About Me
with an Empty Activity
Name | About Me |
---|---|
Package | com.nabeelvalley.aboutme |
Save Location | C:\Repos\AndroidStudio\DiceRoller |
Language | Kotlin |
Minimum API Level | API Level 19: Android 4.4 |
Use androidx.* artifacts |
Set up Linear Layout
Replace the existing layout for activity_main.xml
and set the Root element to be a LinearLayout
To get started let’s use the following layout:
Will be using a mixture of the layout editor and the XML view, you can drag and edit elements using the layout editore
We’ll also create a dimension
and string
resource for the text element size and content using the refactorings (light bulb) in the Android Studio XML view for the layout
The final layout should be like:
With the following in the:
values/strings.xml
values/dimens.xml
View Styling
We can modify the styles for a component, for spacing on a view we have the following:
- Padding
- Border
- Margin
We can update the text view to add some spacing (using a dimension
) and select the Roboto
font. We can then use Android studio to extract the styling to a style
with the following steps:
- Right click text view > Refactor > Extract Style
- Keep margins, colour, and font
- The style should be created and applied:
In the styles.xml
file you can see the created style:
And we can apply it to a View using the style
attribute:
Checkpoint: