Reader small image

You're reading from  Modern Android 13 Development Cookbook

Product typeBook
Published inJul 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781803235578
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Madona S. Wambua
Madona S. Wambua
author image
Madona S. Wambua

Madona S. Wambua is a Google Developer Expert in the Android category, an Android Engineer programming in Kotlin, and the founder and Chief Technology Officer of Jibu Labs. She is also a Women Tech Makers Ambassador and Co-Chair of AnitaB. She has over ten years of experience in the field and has worked on consumer-facing applications and building software development kits for developers. She has also worked on the famous Google Glass during her tenure at a startup and got an opportunity to work on interactive AR videos to transform lives through machine learning and computer vision. Madona is also a Keynote Speaker and the host of Tech Talks with Madona, a podcast geared towards supporting women and allies in tech.
Read more about Madona S. Wambua

Right arrow

Getting Started with Modern Android Development Skills

The Android Operating System (OS) is one of the most favored platforms for mobile devices, with many users worldwide. The OS is used in cars and wearables such as smart watches, TVs, and phones, which makes the market quite wide for Android developers. Hence, there is a need for new developers to learn how to build Android applications utilizing new Modern Android Development (MAD) skills.

Android has come a long way since being launched in 2008 and used in the first Integrated Development Environments (IDEs), Eclipse and NetBeans. Today, Android Studio is the recommended IDE for Android development and, unlike before, when Java was the preferred language, Kotlin is now the language of choice.

Android Studio includes support for Kotlin, Java, C++, and other programming languages, making this IDE suitable for developers with different skill sets.

Hence, by the end of this chapter, following the recipes, you will have Android...

Technical requirements

Running the Android IDE and an emulator successfully can be daunting for your computer. You may have heard the joke about how machines running Android Studio can be used as heaters in winter. Well, there is some truth in that, so your computer should have the following specifications to ensure your system can cope with the IDE’s demands:

  • 64-bit Microsoft Windows, macOS, or Linux installed along with a stable internet connection. Recipes in this book have been developed within macOS. You can also use a Windows or Linux laptop, as there is no difference between using either.
  • For Windows and Linux users, you can follow this link to install Android Studio: https://developer.android.com/studio/install.
  • Minimum of 8 GB of RAM or more.
  • Minimum of 8 GB of available disk space for Android Studio, the Android Software Development Kit (SDK), and the Android Emulator.
  • A minimum screen resolution of 1280 x 800 is preferred.
  • You can download...

Writing your first program in Kotlin using variables 
and idioms

Kotlin is the recommended language for Android development; you can still use Java as your language of choice, as many legacy applications still heavily rely on Java. However, in this book, we will use Kotlin, and if this is the first time you are building Android applications using the Kotlin language, the Kotlin organization has excellent resources to help you get started with free practice exercises and self-paced assessments called Kotlin Koans (https://play.kotlinlang.org/koans/overview).

In addition, you can use the Kotlin language for multiplatform development using Kotlin Multiplatform Mobile (KMM), in which you can share standard code between iOS and Android apps and write platform-specific code only where necessary. KMM is currently in Alpha.

Getting ready

In this recipe, you can either use the online Kotlin playground (https://play.kotlinlang.org/) to run your code or run the code in your Android...

Creating a Hello, Android Community app using Android Studio

We will create our first Android application now that we have installed Android Studio. In addition, we will use Compose – just to mention in advance, in this recipe, we will not go in depth about Compose, as we have a dedicated chapter on Compose, which is Chapter 2, Creating Screens Using a Declarative UI and Exploring Compose Principles.

Getting ready

Before you begin, it’s helpful to know where your Android projects are for consistency. By default, Android Studio creates a package in your home directory, and the package name is AndroidStudioProjects; here, you will find all the projects you create.

You can also decide where the folder should be if you want to change it. In addition, ensure you are using the latest version of Android Studio to utilize all the great features. To find out what the latest Android version is, you can use the following link: https://developer.android.com/studio/releases...

Setting up your emulator in Android Studio

Android Studio is a reliable and mature IDE. As a result, Android Studio has been the favored IDE for developing Android applications since 2014. Of course, you can still use other IDEs, but the advantage of Android Studio is that you do not need to install the Android SDK separately.

Getting ready

You need to have done the previous recipe to be able to follow along with this recipe since we will be setting up our emulator in order to run the project we just created.

How to do it…

This chapter seeks to be friendly to beginners and also move you smoothly toward more advanced Android as you work through the recipes.

Let’s follow these steps to see how you can set up your emulator and run your project in the Creating a Hello, Android Community App using Android Studio recipe:

  1. Navigate to Tools | Device Manager. Once the device manager is ready, you have two options: Virtual or Physical. Virtual means you will...

Creating a button in Jetpack Compose

We must note that we cannot cover all views in just one recipe; we have a chapter dedicated to learning more about Jetpack Compose, so in the project we have created, we will just try to create two more additional views for our project.

Getting ready

Open the Android Community project, as that is the project we will be building upon in this recipe.

How to do it…

Let’s start by implementing a simple button in Compose:

  1. Let’s go ahead and organize our code and align the text to the center by adding a Column() to organize our views. This should be added to the setContent{} function:
    Column(
        modifier = Modifier
            .fillMaxSize()
            .wrapContentSize(Alignment.Center),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Greeting("Hello, Android Community...

Utilizing gradlew commands to clean and run your project in Android Studio

The gradlew command is a robust Gradle wrapper that has excellent usage. In Android Studio, however, you do not need to install it because it is a script that comes packaged within the project.

Getting ready

For now, however, we will not look into all the Gradle commands but instead use the most popular ones to clean, build, provide info, debug, and scan our project to find any issues when we run our application. You can run the commands in your laptop’s terminal as long as you are in the correct directory or use the terminal provided by Android Studio.

How to do it…

Follow these steps to check and confirm whether Gradle works as anticipated:

  1. You can check the version by simply running ./gradlew.
Figure 1.12 – gradlew version

Figure 1.12 – gradlew version

  1. To build and clean your project, you can run the ./gradlew clean and ./gradlew build commands. If anything...

Understanding the Android project structure

If this is your first time looking at the Android project folder, you might wonder where to add your code and what the packages mean. This recipe will walk through what each folder holds and what code goes where.

Getting ready

If you open your project, you will notice many folders. The main folders in your Android project are listed here:

  • The manifest folder
  • The java folder (test/androidTest)
  • The Res Resource folder
  • Gradle Scripts

How to do it…

Let’s navigate through each folder as we learn what is stored, where, and why:

  1. In Figure 1.14, you can see the Packages dropdown; click on that, and a window with Project, Packages, Project Files, and more will pop up.
  2. You can opt to view your project using the Android logo, via Project, or the Project highlighted section next to the drop-down menu. The Project view is best when you have many modules in your application and want to add specific...

Debugging and logging in Android Studio

Debugging and logging are crucial in Android development, and you can write log messages that appear in Logcat to help you find issues in your code or verify a piece of code executes when it should.

We will introduce this topic here, but it is unfair to say we will cover it all in just one recipe; for that reason, we will cover more about debugging and logging in Chapter 12, Android Studio Tips and Tricks to Help You during Development.

Getting ready

Let us use an example to understand logging. The following log methods are listed from the highest to lowest priority. They are proper when logging network errors, success calls, and other errors:

  • Log.e(): A log error
  • Log.w(): A log warning
  • Log.i(): Log information
  • Log.d(): Debugging shows critical messages to developers, the most used log
  • Log.v(): Verbose

A good practice is associating every log with a TAG to identify the error message in Logcat quickly. A...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Modern Android 13 Development Cookbook
Published in: Jul 2023Publisher: PacktISBN-13: 9781803235578
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Madona S. Wambua

Madona S. Wambua is a Google Developer Expert in the Android category, an Android Engineer programming in Kotlin, and the founder and Chief Technology Officer of Jibu Labs. She is also a Women Tech Makers Ambassador and Co-Chair of AnitaB. She has over ten years of experience in the field and has worked on consumer-facing applications and building software development kits for developers. She has also worked on the famous Google Glass during her tenure at a startup and got an opportunity to work on interactive AR videos to transform lives through machine learning and computer vision. Madona is also a Keynote Speaker and the host of Tech Talks with Madona, a podcast geared towards supporting women and allies in tech.
Read more about Madona S. Wambua