Creating Your First Application
Creating Your First 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.
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.
2
Creating an Android Project
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.
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.
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.)
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.
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
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
<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).