Reader small image

You're reading from  Android Programming with Kotlin for Beginners

Product typeBook
Published inApr 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789615401
Edition1st Edition
Languages
Right arrow
Author (1)
John Horton
John Horton
author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton

Right arrow

Chapter 3. Exploring Android Studio and the Project Structure

In this chapter, we will create and run two more Android projects. The purpose of these exercises is to explore Android Studio and the structure of Android projects more deeply.

When we build our apps ready for deployment, the code and the resource files need to be packed away as they are in the APK file. Therefore, all the layout files and other resources, which we will be looking at soon, need to be in the correct structures.

Fortunately, Android Studio handles this for us when we create a project from a template. However, we still need to know how to find and amend these files, how to add our own and sometimes remove the files created by Android Studio, and how the resource files are interlinked – sometimes with each other, and sometimes with the Kotlin code (that is, the autogenerated Kotlin code, as well as our own).

Along with understanding the composition of our projects, it will also be beneficial to make sure that we get...

A quick guided tour of Android Studio


To get started, take a look at this annotated diagram of Android Studio. We will reacquaint ourselves with the parts that we have already seen, and learn about the parts that we have not yet discussed:

It will be useful to formally point out and name the various parts of the Android Studio User Interface (UI), so that I can refer to them by name, rather than describing their location and showing screenshots all the time. So, let's run through them from number 1:

  1. This is the Project window and will be the main focus of this chapter. It enables us to explore the folders, code, and resources of the project and is also referred to as the Project Explorer window. Double-click on a file here to open the file and add a new tab to area 3 on the diagram. The structure of the files and folders here closely resembles the structure that will eventually end up in the finished APK file.

    Note

    As we will see, while the structure of folders for an Android project remains...

Project Explorer and project anatomy


When we create a new Android project, we most often do so with a project template, just as we did in Chapter 1, Getting Started with Android and Kotlin. The template that we use determines the exact selection and contents of the files that Android Studio will generate. While there are big similarities across all projects that are worth noting, seeing the differences can also help. Let's build two different template projects and examine the files, their contents, and how they are all linked together through the code (XML and Kotlin).

The Empty Activity project

The simplest project type with an autogenerated UI is the Empty Activity project. Here, the UI is empty, but it is ready to be added to. It is also possible to generate a project without a UI at all. When we create a project, even with an empty UI, Android Studio autogenerates the Kotlin code to display the UI. Therefore, when we add to it, it is ready to be displayed.

Let's create an Empty Activity...

The Basic Activity project


The next simplest project type with an autogenerated UI is the Basic Activity project. This is the same type of project that we created in Chapter 1, Getting Started with Android and Kotlin. Feel free to open that project up now, but it is recommended to generate a new one so that we can examine it without any of our alterations and additions clouding the discussions.

Let's create a Basic Activity project, as follows:

  1. In Android Studio, select File | New | New Project….

  2. On the Choose your project screen, select the Basic Activity template and click on Next.

  3. Change the Name field to Basic Activity App.

  4. Choose the same package name and save the location as in the previous project.

  5. Be sure to select Kotlin as the language.

  6. Check the Use AndroidX artifacts checkbox as we did previously.

  7. The rest of the settings can be left at their defaults, so just click on Next.

Now we can dig into the files. We won't look at everything in the same detail that we did for the Empty Activity...

Exploring the Android emulator


As we progress, it helps to be familiar with exactly how to use the Android emulator. If you haven't used the latest version of Android, some of the ways to achieve even simple tasks (such as viewing all the apps) can be different to how your current device works. In addition, we want to know how to use the extra controls that come with all emulators.

The emulator control panel

You probably noticed the mini control panel that appears beside the emulator when you run it. Let's go through some of the most useful controls. Take a look at this screenshot of the emulator control panel. I have annotated it to aid the discussion:

I will just mention the more obvious controls and go into a bit more depth when necessary:

  1. These are the window controls. They minimize or close the emulator window.

  2. From top to bottom, the first button is used to power-off the emulator, to simulate powering off the actual device. The next two icons raise and lower the volume.

  3. These two buttons...

Summary


Remember that the goal of this chapter was to familiarize ourselves with the system and structure of Android and an Android project. Android projects are an elaborate interweaving of Kotlin and a multitude of resource files. Resource files can contain XML to describe our layouts, textual content, styles, and colors, as well as images. Resources can be produced to target different languages and regions of the world. Other resource types that we will see and use throughout the book include themes and sound effects.

It is not important to remember all the different ways in which the different resource files and Kotlin files are interconnected. It is only important to realize that they are interconnected, and also be able to examine files of various types and realize when they are dependent on code in another file. Whenever we create connections from our Kotlin code to the XML code, I will always point out the details of the connection again.

We do not need to learn XML in addition to...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android Programming with Kotlin for Beginners
Published in: Apr 2019Publisher: PacktISBN-13: 9781789615401
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 AU $19.99/month. Cancel anytime

Author (1)

author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton