Random Posts

24/1/16

Part 1: Creating an Android Project

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 again
and 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 False button:
Figure 1.1 (It’s Istanbul, not Constantinople)


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 shown in Figure 1.1.
A layout defines a set of user interface objects and their position 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 XMLin this file will define the
user interface 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.


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 Project....
Yo
u should see the new project wizard. In the first screen of the wizard, enter GeoQuiz as the application
na
me
(Figure 1.4). 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,
yo
u 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 in which the domain name of your
organization is reversed and suffixed with further identifiers. This convention keeps package names
uni
que and distinguishes applications from each other on a device and on Google Play.
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 16:
Android 4.1 (Jelly Bean) (Figure 1.5). You will learn about the different versions of Android in Chapter 6.
Figure 1.5 Specifying device support


(Android Studio updates regularly, so your wizard may look slightly different from what we are showing
yo
u. This is usually not a problem; the choices should be similar. If your wizard looks very different, then
th
e 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.)
Click Next.
In the next screen, you are prompted to choose a template for the first screen of GeoQuiz (Figure 1.6).
Choose Blank Activity and click Next.
Figure 1.6 Choosing a type of Activity


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


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.
For the Title, enter GeoQuiz to match the name of the app. Leave the Menu Resource Name as is and click Finish.
Android Studio will create and open your new project.

0 nhận xét:

Đăng nhận xét

Facebook