Android Programming For Beginners - Sample Chapter
Android Programming For Beginners - Sample Chapter
Android Programming For Beginners - Sample Chapter
$ 49.99 US
31.99 UK
P U B L I S H I N G
John Horton
Android Programming
for Beginners
ee
pl
C o m m u n i t y
E x p e r i e n c e
D i s t i l l e d
Android Programming
for Beginners
Learn all the Java and Android skills you need to start making
powerful mobile applications
Sa
m
John Horton
Preface
Learn by example and build three real-world apps and over 40 mini apps throughout
the book!
Why Android?
When Android first arrived in 2008, it was almost seen as a poor relation to the
much more stylish iOS on Apple iPhone. But, quite quickly, through diverse handset
offers that struck a chord with both the practical price-conscious as well as the
fashion-conscious and tech-hungry consumers, Android user numbers exploded.
Now, after seven major releases, the annual sales of Android devices is increasing
almost every year.
For many, myself included, developing Android apps is the most rewarding thing
(apart from our friends and family) in the world.
Quickly putting together a prototype of an idea, refining it, and then deciding to
run with it as well wiring it up into a fully-fledged app is an exciting and rewarding
process. Any programming can be fun, and I have been programming all my life, but
creating for Android is somehow extraordinarily rewarding.
Defining exactly why this is so is quite difficult. Perhaps it is the fact that the platform
is free and open. You can distribute your apps without requiring the permission of a
big controlling corporationnobody can stop you. And at the same time, you have
the well-established, corporate-controlled mass markets such as Amazon App Store,
Google Play, Samsung Galaxy Apps, as well as other smaller marketplaces.
More likely, the reason developing for Android gives such a buzz is the nature of the
devices. They are deeply personal. You can create apps that actually interact with
people's lives. You can educate, entertain, organize them, and so on. But it is there
in their pocket ready to serve them in the home, workplace, or on holiday. Everyone
uses them, from infants to seniors.
Preface
Preface
Preface
Chapter 12, Having a Dialogue with the User, starts ramping up what we can do now. In
this chapter, we will build apps that use pop-up windows to get input from the user
as well as start the first major app of the book, Note To Self.
Chapter 13, Handling and Displaying Arrays of Data, will discuss Java arrays and then
show you how to use them in some apps, as almost any app that we build will need
to be able to handle significant amounts of data.
Chapter 14, Handling and Displaying Notes in Note To Self, will use the skills you
learned and practiced in the previous chapter and will enhance our Note To Self app
in order for the user to be able to view and access all their data.
Chapter 15, Android Intent and Persistence, covers how to build multiscreen apps, how
to let the user seamlessly navigate between them, and how to implement a settings
screen that remembers user's preferences.
Chapter 16, UI Animations, teaches how to use the really useful Android SeekBar
widget and how to animate our UI to make it spin, bounce, flash, and more.
Chapter 17, Sound FX and Supporting Different Versions of Android, finds out how
to bring our apps to life with sound effects. In this chapter, you will explore the
SoundPool class to add beeps, buzzes, clicks, or even explosions.
Chapter 18, Design Patterns, Fragments, and the Real World, sets the scene for the
final and most advanced third of the book. In this chapter, you will learn how
professionals organize their code with patterns and see exactly how to use this
knowledge to build Android apps.
Chapter 19, Using Multiple Fragments, shows you how to use the Android Fragment
to make our apps modular so they can run on multiple different device types, such
as phones and tablets, and appear differently on each but run from exactly the same
code that we write.
Chapter 20, Paging and Swiping, builds some apps that use the really cool swiping
effect. In this chapter, you will learn how to swipe left and right between pages,
menus, and images in an app.
Chapter 21, Navigation Drawer and Where It's Snap, explains how to use the navigation
drawer UI in your apps, how to swipe from the edge of the screen to reveal the user's
navigation choices, and how to get started on the most advanced app we will build
using navigation drawer.
Chapter 22, Capturing Images, builds a mini app to use the device camera in order to
capture and store images and then enhance our main app as well.
Preface
Chapter 23, Using SQLite Databases in Our Apps, teaches you how databases work with
a focus on the Android database of choice, SQLite. In this chapter, we will build a
database mini app.
Chapter 24, Adding a Database to Where It's Snap, shows you how to code an advanced
database to store user data, photos, and even GPS coordinates.
Chapter 25, Integrating Google Maps and GPS Locations, shows you how to connect live
to the Google Maps service to add real global mapping data to your apps.
Chapter 26, Upgrading SQLite Adding Locations and Maps, teaches you how to
upgrade the database from Chapter 24, Adding a Database to the Where It's Snap App
and add the ability to capture and show a map and GPS locations along with the
user's photos in the Where it's snap app.
Chapter 27, Going Local Hola!, will help you understand how to easily add multiple
different languages to make apps with a truly global reach.
Chapter 28, Threads, Touches, Drawing, and a Simple Game, discusses how to build a
simple but working retro Pong game to explore the important topics of drawing,
screen touches, and multithreaded computing.
Chapter 29, Publishing Apps, shows you how to build deployable apps and publish
them on the Google Play global marketplace.
Chapter 30, Before You Go, discusses briefly how you might like to take your Android
and Java skills further.
Set up the Java Development Kit (JDK), part of the required Android
development environment
[1]
Android is a fairly complex system, but you do not need to understand it in depth
to be able to make amazing apps. The part of the Android system that executes
(runs) our compiled DEX code is called the Dalvik Virtual Machine (DVM). The
DVM itself is a piece of software written in another language that runs on a specially
adapted version of the Linux operating system. So what the user sees of Android, is
itself just an app running on another operating system.
The purpose of the DVM is to hide the complexity and diversity of the hardware
and software that Android runs on but, at the same time, its purpose is to expose
all of its useful features. This exposing of features generally works in two ways. The
DVM itself must have access to the hardware, which it does, but this access must be
programmer friendly and easy to use. The way the DVM allows us access is indeed
easy to use because of the Android Application Programming Interface (API).
However, once you learn that this single line of code searches for the available
satellites and then communicates with them in orbit around the Earth while
retrieving your precise latitude and longitude on the planet, it is easy to begin to
glimpse the power and depth of the Android API in conjunction with the DVM.
Even if that code does look a little challenging at the moment, imagine talking to a
satellite in some other way!
The Android API is mainly a whole bunch of Java code. So, how do we use all this
code to do cool stuff without getting swamped by its complexity? How do we find
and manipulate the pedals, steering wheel, and sunroof of the Android API?
There are many different estimates to the number of lines of
code that have gone into Android. Some estimates are as low
as 1 million, some as high as 20 million. What might seem
surprising is that, despite this vast amount of code, Android is
known in programming circles for being "lightweight".
[2]
Chapter 1
Java is object-oriented
Java is a programming language that has been around a lot longer than Android.
It is an object-oriented language. This means that it uses the concept of reusable
programming objects. If this sounds like technical jargon, another analogy will help.
Java enables us and others (such as the Android development team) to write Java
code that can be structured based on real-world "things" and, here is the important
part, it can be reused.
So, using the car analogy, we could ask the question: if a manufacturer makes more
than one car in a day, do they redesign each and every part for each and every car?
The answer, of course, is no. They get highly skilled engineers to develop exactly
the right components that are honed, refined, and improved over years. Then, that
same component is reused again and again, as well as occasionally improved. Now,
if you are going to be picky about my analogy, then you can argue that each of the
car's components still have to be built from raw materials using real-life engineers, or
robots, and so on.
This is true. What the software engineers actually do when they write their code is
build a blueprint for an object. We then create an object from their blueprint using
Java code and, once we have that object, we can configure it, use it, combine it with
other objects, and more. Furthermore, we can design blueprints and make objects
from them as well. The compiler then translates (manufactures) our custom-built
creation into DEX code.
In Java, a blueprint is called a class. When a class is transformed into a real working
thing, we call it an object.
Objects in a nutshell
We could go on making analogies all day long. As far as we care at
this point:
Java is a language that allows us to write code once that can
be used over and over again.
This is very useful because it saves us time and allows us to
use other people's code to perform tasks we might otherwise
not have the time or knowledge to write for ourselves.
Most of the time, we do not even need to see this code or
even know how it does its work!
One last analogy. We just need to know how to use that code, just as
we only need to learn to drive the car.
[3]
[4]
Chapter 1
In summary, all we need to do is learn how to read and code Java, so we can begin to
learn and take advantage of the Android API.
All these tools are free, so let's take a look at the development environment we
will be using.
We talked a fair bit about compiling our Java code, as well as other people's
Java code, into DEX code that will run on the DVM, on people's Android
devices. In order to use Java code, we need a free software called the JDK.
The JDK also includes other people's code, which is separate from the
Android API.
There is a whole range of tools that are required to develop for Android, and
we also need the Android API, of course. This whole suite of requirements
is collectively known as the Android software development kit (SDK).
Fortunately, downloading and installing a single application will give us these
things all bundled together. This single application is called Android Studio.
[5]
[6]
Chapter 1
Now that Android Studio has exceeded version 1, there is virtually no reason to use
anything else, especially if you are just getting started with Android. If you already
have Eclipse set up for Android development and really don't want to change, that's
fine, the code in this book will work. However, there will be significant differences in
the instructions, especially regarding the user interface (UI) designer. Also, the code in
this book has been thoroughly tested by others and me in Android Studio.
The sections that follow will set up a development environment with the JDK and
Android Studio as our IDE of choice.
The JDK
This can be as simple as downloading, double-clicking on the downloaded file, and
following the installation instructions. However, sometimes it isn't. So, it is probably
worth running through the installation process step by step and pointing out along
the way a few options that can make things easier.
Now we are ready to install the JDK. This fairly simple set of steps will set up the
JDK quickly. The only slight delay is the download itself, which could take a while
on slower Internet connections. The actual installation process should be fast and
trouble free:
1. Visit the Java JDK downloads page at http://www.oracle.com/
technetwork/java/javase/downloads/jdk8-downloads-2133151.html.
2. If the link has changed, conduct a web search for Java JDK download. The
only potential stumbling block at this stage is that you click on a link for
the JRE instead. JDK is what we need. The following is a screenshot of the
important part of this page:
[8]
Chapter 1
3. Your download page will most likely be slightly different. There will be a
newer version of the JDK by the time you read these words, so the 8u51 will
be different. That's OK, we just need whichever is the newest version. Simply
click on the Accept License Agreement radio button that is highlighted in
the previous screenshot and then click on the download link, which is on
the right-hand side column corresponding to your operating system in the
Product/File Description column. If you are not sure whether you have 32bit or 64-bit Windows, refer to the tip before this section.
4. Wait while the JDK is downloaded to your hard drive.
5. In the folder where you've downloaded the JDK, right-click on the jdk-8u51windows-x64.exe file and select Run as administrator. The precise name
of the file you have will vary based on whether you have 32-bit or 64-bit
Windows and what the current version of the JDK happens to be at the time.
6. There will be a series of windows that will guide us through the installation
process. The most we have to do is just click on Next to proceed. As
promised, I will guide you through them one at a time and point out when
you might like to make changes or make a note of things. The following is a
screenshot of the first window that you will see during the installation:
[9]
8. On the window pictured in the previous screenshot, we can leave all the
options at their default values. They are just what we need. The Install
to setting is worth considering, however. By the time we install the JDK,
Android Studio, and all the extra tools and files that come with it, we will
have around 6 gigabytes of files and folders. Now, consider that we will
also be making lots of projects throughout the course of this book. Ideally,
we want all of these files, folders, and projects to be on the same hard drive.
They don't have to be, but we might avoid some problems later on if they are.
So, do you have at least 6 gigabytes of space on the hard drive that you've
chosen by default? If not, you might like to browse to another folder. In
addition to this, as we will see later on in this tutorial, it will be handy (but
not essential) to simplify the folder names used to install the JDK. So, click on
the Change button and you will see this window:
[ 10 ]
Chapter 1
9. Browse to the hard drive where you will be installing all of your
development tools. Then simplify the names of the folders in which you will
install the JDK to just Java\JDK\. As you can see in the next screenshot, I
have also switched to my D:\ drive, as I have more space there:
10. It actually doesn't matter what you call these folders as long as you
remember where they are and what they are called, including whether they
are uppercase or lowercase letters. Perhaps you can jot down a note or copy
and paste them to a file on your desktop. When you are happy with the
chosen installation location and folder names, go ahead and click on Next.
[ 11 ]
11. The next window will not appear on every installation. If you don't see the
window pictured as follows, its fine, you can skip to step 13. If you do see
the window, proceed to step 12.
12. Understandably, this window could cause confusion if you have never done
any Java development before. You might be thinking that we had already
chosen the destination folder for Java. We chose the location for the JDK. This
window refers to the bog-standard Javathe JRE. This is what runs Java
programs on your PC and, since we are developing for Android, we don't need
it. However, we still need to choose a location in order to proceed. Accept the
default and just click on Next. If you are a tidiness obsessive like me, you can
change to the same Java folder from step 8 and then click on Next.
13. Next, you will see the window that says 3 Billion Devices Run Java.
Android has over 1.1 billion devices alone at the time of writing this despite
a slow start in 2015. You might also like to know that there are more than
18,000 different distinct devices. Seriously, Google it if you think it sounds
crazy. You can do this while you wait for the installation to complete.
[ 12 ]
Chapter 1
14. Now, you will see the final screen. Click on Next Steps if you are curious,
but there is no need because we will be covering Android-specific next steps
without delay.
[ 13 ]
15. Click on Close, and we are almost done installing the JDK, with just a few
more precautionary steps left.
16. Now, we will make sure that Windows (and all its applications) know where
to find the JDK. Right-click on your My Computer (This PC on Windows 8)
icon and Properties | Advanced system settings | Environment Variables |
New (under System variables and not under User variables). Now you can
see the New System Variable dialog as follows:
[ 14 ]
Chapter 1
Now that we know what to expect, we can get on with the installation of Android
Studio. Follow the given steps to do so. There is nothing especially tricky about this,
and the few aspects that might need some consideration will be discussed as they arise:
1. Visit https://developer.android.com/sdk/index.html and click on the
Download Android Studio for Windows button. If at the time of reading
this the link has changed, simply Google Download Android Studio.
2. Next, you will see the Terms and Conditions page as shown in the
following screenshot:
3. Click on the I have read and agree with the above terms and conditions
checkbox as highlighted in the previous screenshot.
[ 15 ]
[ 16 ]
Chapter 1
8. Click on Next. Now we can see a few options, as in this next screenshot:
9. Make sure that all the options have a tick next to them, and then click on Next.
10. The next window is the license agreement. Click on I Agree and you will
see some settings that warrant a short discussion. Take a look at the next
screenshot that shows you the Install Locations window:
[ 17 ]
11. In this step, we want to install the Android Studio IDE and Android SDK
to the same hard drive where we installed the JDK. So you might just be able
to click on Next at this point. However, if you've installed the JDK to another
drive, then we need to change the drive and the folders we use at this step
too. This isn't strictly essential, but it can avoid problems for some users.
12. For Android Studio Installation Location, choose the root of the drive where
you've installed the JDK followed by \Android Studio. So in my case,
this will be D:\Android Studio. For Android SDK Installation Location,
choose the same hard drive and simply add Android\sdk as the location. So
if, like me, you've installed the JDK on D:, then choose D:\Android\sdk. The
next screenshot makes this clear:
13. Click on Next when you have selected your installation locations.
[ 18 ]
Chapter 1
14. Next, you might see the Emulator Setup window as pictured in the next
figure. If you do, then accept the default settings and click on Next; otherwise,
you can skip to step 15. Don't worry if you don't see this screen, it is a minor
issue to do with running the Android emulators a bit more smoothly. Most of
the time, you will probably want to use a real device anyway.
15. We are nearly there now. The next window asks you to choose a start menu
folder, just as when we install any new Windows app. You might want to
make a note of this location. Click on Install to accept the default settings,
and Android Studio will begin to install itself and extract the SDK to the
appropriate folder that we selected earlier. This might take some time.
[ 19 ]
16. When you see the Installation Complete window, click on Next. Now, you
will see the following window:
17. Click on Finish to bring up the second to last window of the installation
process. Assuming that this is your first time using Android Studio, click on
the I do not have a previous version of Android Studio or I do not want to
import my settings radio button and click on OK.
[ 20 ]
Chapter 1
18. Now, in the next figure, you get to choose the theme that Android Studio will
use. If you like a conventional black text on white background appearance,
then choose IntelliJ, and if you want a cool dark style, choose Darcula. You
can alter any of these schemes from within Android Studio if you change
your mind later.
[ 21 ]
22. Finally, we are presented with the Welcome to Android Studio screen.
This screen, among other things, allows us to start a new project or open an
existing project. Take a look at the next screenshot:
23. You can close this window and take a break or leave it open and read on
because we will be back here really soon.
This was a fairly lengthy tutorial but, hopefully, it wasn't too tough. Android Studio
and the supporting tools that we need are installed and ready to go. We are really
close now to building our first app.
First, let's look at the composition of an Android app a little.
Chapter 1
Android resources
Our app will include resources such as images, sounds, and user interface layouts
that are kept in separate files from the Java code. We will slowly introduce ourselves
to them over the course of this book.
They will also include files that contain the textual content of our app. It is a
convention to refer to the text in our app through separate files because it makes
them easy to change, and this makes it easy to create apps that work for multiple
different languages.
Furthermore, the actual UI layouts of our apps, despite the option to implement
them with a visual designer, are actually read from text-based files by Android.
Android (or any computer), of course, cannot read and recognize text in the same
way that a human can. Therefore, we must present our resources in a highly
organized and predefined manner. To do so, we will use Extensible Markup
Language (XML). XML is a huge topic but, fortunately, its whole purpose is to be
both human and machine readable. We do not need to learn this language, we just
need to observe (and then conform to) a few rules. Furthermore, most of the time
when we interact with XML, we will do so through a neat visual editor provided by
Android Studio. We can tell when we are dealing with an XML resource because the
filename will end with the .xml extension.
You do not need to memorize this, as we will constantly be returning to this concept
throughout the book.
Android packages
Whenever we create a new Android app, we will choose a unique name known as
a package. We will see how to do this in the Our first Android app section. Packages
are often separated into subpackages, so they can be grouped together with other
similar packages. We can simply think of these as folders and subfolders.
[ 23 ]
We can also think of all the packages that the Android API makes available to us as
books that contain code, from a library. Some common Android packages we will
use include the following:
android.graphics
android.database
android.view.animation
As you can see, they are arranged and named to make what is contained in them as
obvious as possible.
If you want to get an idea for the sheer depth and breadth
of the Android API, then take a look at the Android
package index at http://developer.android.com/
reference/packages.html.
Earlier, we learned that reusable code blueprints that we can transform into objects are
called classes. Classes are contained in these packages. We will see in our very first app
how to easily import other people's packages along with specific classes from those
packages for use in our projects. A class will almost always be contained in its own file,
with the same name as the class, and have the .java file extension.
In Java, we further break up our classes into sections that perform the different
actions for our class. We call these sections methods. These are, most often, the
methods of the class that we will use to access the functionality provided within
all those millions of lines of code. We do not need to read the code. We just need to
know which class does what we need, which package it is in, and which methods
from within the class give us precisely the results we are after.
The next diagram shows a representation of the Android API. We can think about
the structure of the code that we will write in exactly the same way, although we will
most likely have just one package per app. Of course, because of the object-oriented
nature of Java, we will only be using selective parts from this API. Also note that
each class has its own distinct data. Typically, if you want access to the data in a
class, you need to have an object of that class.
[ 24 ]
Chapter 1
You do not need to memorize this, as we will constantly be returning to this concept
throughout the book.
By the end of this chapter, we will have imported multiple packages and some classes
from them and we will have used other people's methods. By the end of Chapter 2,
Java First Contact, we will have even written our very own methods as well.
[ 25 ]
2. If you get the Windows Firewall has blocked some features of this program
message, as pictured in the next screenshot, click on Allow access:
[ 26 ]
Chapter 1
3. Now, you will see the Welcome to Android Studio start menu that we
saw at the end of the previous tutorial. Go ahead and click on Start a new
Android Studio project. You will see the New Project screen pictured next.
Starting a new project is very easy and mainly involves accepting the default
settings on a few different screens. Let's take a closer look at some of the
options, however, because they will increase our understanding of Android
and Android Studio:
4. First, note that we can name our application in the Application name field.
You can choose any name you like for your first application, but just be
aware that Android Studio will soon generate a whole bunch of code and
files for us and that the name you choose will be reflected in them. If you
want your code and files to be identical to those that we will be examining
shortly, call your application Hello Android.
[ 27 ]
5. Next, note the Company Domain field. This is where you will enter the
details of your company. It is a convention and very practical (because it
is unique) to use the domain name of your company website. If you don't
have a company or domain name, enter the domain name I am using:
gamecodeschool.com. Unlike the application name, using a different
company domain will have almost zero effect on the code and files that
we will examine later. Although if you are using your own domain name,
do refer to the tip Using your own domain name or application name, after this
tutorial. Choose and enter a company domain name.
6. Now, look at the Package name field. It has been automatically derived from
the previous two fields. If you used the same two values as suggested in
the previous two steps, your package name will be com.gamecodeschool.
helloandroid. Remember that a package is a collection of the Java classes
that are our code files, and our apps can comprise one or more packages if we
wish them to, but they must comprise at least one package. We can edit the
package name by clicking on the edit link, but we have no need to do so here.
7. Finally, in the Project location field, you can accept the default settings
or browse to the location where you would like to store all your Android
projects. You have the option to change this for each project that you create.
I've put personal projects in my Dropbox folder so, if my PC breaks down
mid project, I know my files are safe. Android Studio can also work from an
online code repository, such as GitHub, but we are not covering that in this
book. Here is a screenshot of the New project window after steps 5, 6, and 7:
[ 28 ]
Chapter 1
8. Click on the Next button to continue and you will see the Target Android
Devices window:
[ 29 ]
10. Click on the Next button. Now we can see the Add an activity to
Mobile window:
[ 30 ]
Chapter 1
12. The Customize the Activity window gives us four things that we can play
with. It is perfectly possible to leave them all at their default settings, but
let's change them to make them more meaningful and discuss what each one
is. Activity Name is the name of the class that will contain our code. If this
sounds odd, to be more specific, we will have a class that is an activity called
whatever we name it here. Name the activity MyActivity.
13. The next field is Layout Name. We discussed earlier in this chapter that
Android UI layouts are usually defined in a separate XML text file, not with
our Java code. The layout name is what this file will be called. Name the
layout my_layout.
14. The next field is Title. This is different than the Activity Name field and will
be used by Android on the device's screen as the name of the app. Name the
title My App.
[ 31 ]
15. Finally for this screen, we have Menu Resource Name. Menus are the
pop-up options that you get on Android when (perhaps unsurprisingly) you
click on the menu button. They might also be shown on the topmost bar of the
app known as the action bar. This varies depending on the version of Android
a device is running. These are considered to be part of the UI as well and are
usually defined in this separate XML file. Name our menu file my_menu.
16. It is important that the exact case and spacing is used for the information we
enter in steps 12, 13, 14, and 15. Check out the next close-up figure to see that
you have entered the text just right:
17. Click on the Finish button, and Android Studio will now create a project for
us based on our choices from the previous 16 steps.
We now have a project ready for us to deploy or modify.
Using your own domain name and application name
Using your own domain and application name is absolutely fine.
The only thing to be aware of is that the first line of code that we
will see when we look at our Java code (in Chapter 2, Java First
Contact), will be, and needs to be, different. This is the package
name. In the context of this book, we won't ever need to change the
package name or do anything with it, so it doesn't matter what you
call your apps or whether you use your own domain name.
The only time this could cause a problem is when you create a
project (with a different name or package name) and then copy and
paste the code from the download bundle. The reason for this is
that this code will have a different package name. All you need to
do to overcome this problem is either create your project with the
same package name as the code files you are copying and pasting
from or change the very first line of code, after you have copied and
pasted it, to reflect the package name of the project you've created.
[ 32 ]
Chapter 1
You can allow multiple programmers to contribute to the same project and
even the same file
You can click on a button to roll back your work to a specified position if you
realize you have gone wrong
You can store your code seamlessly in a repository on the cloud/Web and
work on your project from any development machine without any concern
about backing up the files
Achieving all these benefits is not complicated and, in some cases, it is even free.
The problem with introducing VCS in a book like this is that there are an awful lot
of options and there is no simple, universally best option that will suit everybody. In
addition, it introduces yet another hurdle before we can get down to the business of
making apps.
If the topic of version control sounds interesting, try
exploring these two providers:
http:bitbucket.org
http:.github.com
Now, you might be surprised to know that we can actually deploy our app to an
emulator or a real Android device. Throughout this book, all the code for each
project is supplied in the download bundle in a folder that reflects the chapter
number. There is no code, however, in the Chapter 1 folder because all the code in
this project has been automatically generated by Android Studio and we don't need
to type anything for this project. We will, however, examine, modify, and add to this
autogenerated code in the next chapter.
[ 33 ]
Deploying to an emulator
Deploying to an emulator is a matter of a few mouse clicks. There is a ready-made
emulator that is included with Android Studio. At the time of writing this, it is
Nexus 5. This may well be updated to a newer device by the time you read this. The
basic functionality described here will likely be identical, but the device name and
screenshots might vary slightly from your experience:
1. Just below the menu bar is a row of neat-looking buttons that we will
explore as the book progresses. The button we want right now is the green
triangle that represents play. Identify the button using the next screenshot
and click on it:
[ 34 ]
Chapter 1
2. Next, you will see the Choose Device window. Click on the Launch
emulator option as shown in the next screenshot:
[ 35 ]
4. The emulator might take some time to start! Sometimes, it might even take
5 minutes or more. This is partly dependent on whether Android Studio
has detected whether your PC can run HAXM (Hardware Acceleration
Execution Manager) during the setup phase or not. Either way, all the
tutorials in this book will work just fine. Wait for the emulator to start. When
your emulator has loaded, you will see something like the next screenshot:
[ 36 ]
Chapter 1
6. Let's play a little bit with the emulator. Click on the back button to quit the
app, as shown in the next screenshot:
[ 37 ]
7. Note that you can see the launcher icon for your app with the name you
gave it when we created the project. Obviously, we will need to replace the
default Android icon before we release our app into the world. This will do
for now though.
8. You can click on the launcher icon to run the app again or try running any of
the other apps. Play with the emulator for a bit.
Congratulations, you have now run and played with your first app. In addition to
this, we have seen that we can do many things on an emulator that we previously
could only do on a real device.
Emulators take time to start up
If you are planning to use the emulator again soon, then leave
it running in order to avoid having to wait for it to start again.
If you want to try out your app on a tablet, TV, or even a watch, you're going to need
a different emulator.
[ 38 ]
Chapter 1
Now we can take a look at how to get our app onto a real device.
The next few steps will set up the Android device for debugging. Note that different
manufacturers structure the menu options slightly differently to others. However,
the following sequence is probably very close, if not exact, to enable debugging on
most devices:
1. Click on the Settings menu option or the Settings app.
2. Click on Developer options.
3. Tick the checkbox for USB Debugging.
[ 39 ]
4. Connect your Android device to the USB port of your development system.
You can see in the following figure that on the Devices | Logcat subtab of
the Android tab, at the bottom of the Android Studio window, my Samsung
GT I9100 has been detected:
5. Click on the Play icon from the Android Studio toolbar, just as we did when
running the app on the emulator.
6. When the Choose Device window appears, click on Choose running device
and then on your device; in my case, this would be Samsung GT I9100.
7. Finally, click on OK to debug the app on your chosen device.
You can now see your app in all its glory on your device and show it to your friends
and admirers alike.
Note that although the app is running on a real device, this is still running in the
debug mode so we can get feedback about what is happening to our app. In Chapter
29, Publishing Apps, we will see how to generate a signed, distributable app that can
be uploaded to Google Play.
FAQ
1. So is Android not really an operating system, but just a virtual machine? And
are all phones and tablets really Linux machines?
No, all the different subsystems of an Android device, which includes Linux,
the DVM, and the libraries and drivers together, are what make up the
Android operating system.
[ 40 ]
Chapter 1
Android Studio, with the help of the JDK, will turn this code and
these resources into real Android apps
Now, you are good to go and can move on to Chapter 2, Java First Contact.
Summary
So far, we have set up an Android development environment and created and
deployed an app on both an emulator and a real device. If you still have unanswered
questions (and you probably have more than at the start of this chapter), don't worry
because as we dig deeper into the world of Android and Java, things will become
clearer. As the chapters progress, you will build a very rounded understanding of
how everything fits together, and then success will just be a matter of practice and
digging deeper into the Android API.
In the next chapter, we will edit the UI using the visual designer and raw XML code,
write our first Java methods, and get to use some methods provided to us by the
Android API.
[ 41 ]
www.PacktPub.com
Stay Connected: