-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Android Programming for Beginners
By :

In programming, it is a tradition for the first app of a new student to use whichever language/OS they are using to say hello to the world. We will quickly build an app that does just that, and in Chapter 2, Java – First Contact, we will go beyond this and add some buttons that actually respond to the user.
We will start with the creation of this project. What follows here are the steps to create a new project. My instructions at each step might seem a little verbose, but that is just because this is the first time we are running through them. After a few chapters, when we create new projects, I will be able to describe the instructions in a sentence and you will be able to create the project in 30 seconds. As this is the first one, let's do this step by step and learn it all along the way:
If you can't find the icon, you can find the file directly in the bin
folder at the location where you installed Android Studio. Find the studio64.exe
file on a 64-bit PC or the studio.exe
file on a 32-bit PC and double-click on it in the same way that you start any other Windows app.
Hello Android
.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.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.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:Activity
class is a special Java class and every Android app must have at least one. It is the part of the code where our app will start when it is launched by the user, and this also handles any interaction with the user. The options on this screen provide different ready-made templates of the Activity
class code in order to give programmers a fast start when creating various types of apps. As we are starting from scratch, the most appropriate option for us is Blank Activity. Make sure that Blank Activity is selected by clicking on it and then clicking on Next. Now, take a look at the Customize the Activity window:MyActivity
.my_layout
.My App
.my_menu
.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.
In this book, we will be covering everything you need to know to publish professional apps. One thing we will not be discussing is working collaboratively as part of a team or any backup strategies for your code. If you only intend to build apps on your own, then you don't need to know any more. However, Android Studio works almost seamlessly with a number of different Version Control Systems (VCS) and code repositories. VCS and their related code repositories have a number of advantages, which are as follows:
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:
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.
We need to test our apps frequently throughout development to check for any errors, crashes, or anything else unintended. It is also important to make sure that the app looks good and runs correctly on every device type/size that you want to target. Clearly, we do not own each of the many thousands of Android devices. This is where emulators come in.
Emulators, however, are sometimes a bit slow and cumbersome, although they have improved a lot recently. If we want to get a genuine feel of the experience that our user will get, then nothing beats deploying an app to a real device. So we will probably want to use both real devices and emulators while developing our apps.
First, we will deploy to an emulator and then on a real device.
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:
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.
Creating a new emulator
If you want to create an emulator for a different Android device, this is really simple. From the main menu, navigate to Tools | Android | AVD Manager. On the AVD Manager window, click on Create New Virtual Device. Now click on the type of device you want to create: TV, Phone, Wear, or Tablet. Now simply click on Next and follow the instructions to create your new AVD. The next time you run your app, the new AVD will appear as an option to run the app on.
Now we can take a look at how to get our app onto a real device.
The first thing to do is to visit your device manufacturer's website and obtain and install any drivers that are required for your device and operating system.
Many of the more recent Android devices don't need any drivers at all. You could try simply plugging it in!
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:
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.