1
Your First Android Application
This first chapter is full of new concepts and moving parts required to build an Android application. It
is OK if you do not understand everything by the end of this chapter. You will be revisiting these ideas
in greater detail as you proceed through the book.
The application you are going to create is called GeoQuiz. GeoQuiz tests the user’s knowledge of
geography. The user presses TRUE or FALSE to answer the question on screen, and GeoQuiz provides
instant feedback.
Figure 1.1 shows the result of a user pressing the TRUE button.
Figure 1.1 Do you come from a land down under?
1
Chapter 1 Your First Android Application
App Basics
Your GeoQuiz application will consist of an activity and a layout:
• An activity is an instance of Activity, a class in the Android SDK. An activity is responsible for
managing user interaction with a screen of information.
You write subclasses of Activity to implement the functionality that your app requires. A simple
application may need only one subclass; a complex application can have many.
GeoQuiz is a simple app, so it will have a single Activity subclass named QuizActivity.
QuizActivity will manage the user interface, or UI, shown in Figure 1.1.
• A layout defines a set of UI objects and their positions on the screen. A layout is made up of
definitions written in XML. Each definition is used to create an object that appears on screen, like
a button or some text.
GeoQuiz will include a layout file named activity_quiz.xml. The XML in this file will define
the UI shown in Figure 1.1.
The relationship between QuizActivity and activity_quiz.xml is diagrammed in Figure 1.2.
Figure 1.2 QuizActivity manages what activity_quiz.xml defines
With those ideas in mind, let’s build an app.
2
Creating an Android Project
Creating an Android Project
The first step is to create an Android project. An Android project contains the files that make up an
application. To create a new project, first open Android Studio.
If this is your first time running Android Studio, you will see the Welcome dialog, as in Figure 1.3.
Figure 1.3 Welcome to Android Studio
From the dialog, choose Start a new Android Studio project. If you do not see the dialog, you may have
created projects before. In this case, choose File → New → New Project....
3
Chapter 1 Your First Android Application
You should see the New Project wizard (Figure 1.4). In the first screen of the wizard, enter GeoQuiz
as the application name. For the company domain, enter android.bignerdranch.com. As you do this,
you will see the generated package name change to com.bignerdranch.android.geoquiz. For the
project location, you can use any location on your filesystem that you want.
Figure 1.4 Creating a new application
Notice that the package name uses a “reverse DNS” convention: The domain name of your
organization is reversed and suffixed with further identifiers. This convention keeps package names
unique and distinguishes applications from each other on a device and on Google Play.
4
Creating an Android Project
Click Next. The next screen allows you to specify details about which devices you want to support.
GeoQuiz will only support phones, so just check Phone and Tablet. Select a minimum SDK version
of API 19: Android 4.4 (KitKat) (Figure 1.5). You will learn about the different versions of Android in
Chapter 6.
Figure 1.5 Specifying device support
5
Chapter 1 Your First Android Application
Click Next.
In the next screen, you are prompted to choose a template for the first screen of GeoQuiz (Figure 1.6).
You want the most basic template available. Choose Empty Activity and click Next.
(Android Studio updates regularly, so your wizard may look slightly different from what we are
showing you. This is usually not a problem; the choices should be similar. If your wizard looks very
different, then the tools have changed more drastically. Do not panic. Head to this book’s forum at
forums.bignerdranch.com and we will help you navigate the latest version.)
Figure 1.6 Choosing a type of activity
6
Creating an Android Project
In the final dialog of this wizard, name the activity subclass QuizActivity (Figure 1.7). Notice the
Activity suffix on the class name. This is not required, but it is an excellent convention to follow.
Figure 1.7 Configuring the new activity
Leave Generate Layout File checked. The layout name will automatically update to activity_quiz
to reflect the activity’s new name. The layout name reverses the order of the activity name, is all
lowercase, and has underscores between words. This naming style is recommended for layouts as well
as other resources that you will learn about later.
If your version of Android Studio has other options on this screen, leave them as is. Click Finish.
Android Studio will create and open your new project.
7
Chapter 1 Your First Android Application
Navigating in Android Studio
Android Studio opens your project in a window, as shown in Figure 1.8.
The different panes of the project window are called tool windows.
The lefthand view is the project tool window. From here, you can view and manage the files associated
with your project.
The main view is the editor. To get you started, Android Studio has opened QuizActivity.java in the
editor.
Figure 1.8 A fresh project window
You can toggle the visibility of the various tool windows by clicking on their names in the strips of tool
buttons on the left, right, and bottom of the screen. There are keyboard shortcuts for many of these as
well. If you do not see the tool button strips, click the gray square button in the lower-left corner of the
main window or choose View → Tool Buttons.
8
Laying Out the UI
Laying Out the UI
Open app/res/layout/activity_quiz.xml. If you see a graphical preview of the file, select the Text
tab at the bottom to see the backing XML.
Currently, activity_quiz.xml defines the default activity layout. The defaults change frequently, but
the XML will look something like Listing 1.1.
Listing 1.1 Default activity layout (activity_quiz.xml)
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_quiz"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context="com.bignerdranch.android.geoquiz.QuizActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</RelativeLayout>
The default activity layout defines two widgets: a RelativeLayout and a TextView.
Widgets are the building blocks you use to compose a UI. A widget can show text or graphics, interact
with the user, or arrange other widgets on the screen. Buttons, text input controls, and checkboxes are
all types of widgets.
The Android SDK includes many widgets that you can configure to get the appearance and behavior
you want. Every widget is an instance of the View class or one of its subclasses (such as TextView or
Button).