Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Android Programming for Beginners

You're reading from   Android Programming for Beginners Learn all the Java and Android skills you need to start making powerful mobile applications

Arrow left icon
Product type Paperback
Published in Dec 2015
Publisher Packt
ISBN-13 9781785883262
Length 698 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Paresh Mayani Paresh Mayani
Author Profile Icon Paresh Mayani
Paresh Mayani
John Horton John Horton
Author Profile Icon John Horton
John Horton
Arrow right icon
View More author details
Toc

Table of Contents (32) Chapters Close

Preface 1. The First App FREE CHAPTER 2. Java – First Contact 3. Exploring Android Studio 4. Designing Layouts 5. Real-World Layouts 6. The Life and Times of an Android App 7. Coding in Java Part 1 – Variables, Decisions, and Loops 8. Coding in Java Part 2 – Methods 9. Object-Oriented Programming 10. Everything's a Class 11. Widget Mania 12. Having a Dialogue with the User 13. Handling and Displaying Arrays of Data 14. Handling and Displaying Notes in Note To Self 15. Android Intent and Persistence 16. UI Animations 17. Sound FX and Supporting Different Versions of Android 18. Design Patterns, Fragments, and the Real World 19. Using Multiple Fragments 20. Paging and Swiping 21. Navigation Drawer and Where It's Snap 22. Capturing Images 23. Using SQLite Databases in Our Apps 24. Adding a Database to Where It's Snap 25. Integrating Google Maps and GPS Locations 26. Upgrading SQLite – Adding Locations and Maps 27. Going Local – Hola! 28. Threads, Touches, Drawing, and a Simple Game 29. Publishing Apps 30. Before You Go Index

Our first Android app

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.

Creating the project

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:

  1. Start Android Studio by clicking on its icon in the start menu folder that you chose in step 15 of the previous tutorial.

    Tip

    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.

  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:
    Creating the project
  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:
    Creating the project
  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.
  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:
    Creating the project
  8. Click on the Next button to continue and you will see the Target Android Devices window:
    Creating the project
  9. Here, we can see that we have the option of developing an application for a phone, tablet, TV, and wear. Wear is the range of Android-enabled smartwatches. In the preceding screenshot, in the grayed-out part at the bottom of the window, we can also see that we can develop for Glass, Google's trendy Android-enabled glasses, although we would need to install additional files to do this. In this book, we will be developing for phones and tablets, so the already selected option is just what we need. The only other thing we need to choose on this screen is the Minimum SDK. We already know that the Android SDK is a collection of packages of code that we will be using to develop our apps. Like any good SDK, the Android SDK is regularly updated, and each time it gets a significant update, the version number is increased. Simply put, the higher the version number, the newer the features you get to use; the lower the version number, the more devices our app will work on. For now, the default setting API 15: Android 4.0.3 (IceCreamSandwich) will give us lots of great features and at least 90% compatibility with the Android devices that are currently in use. If, at the time of reading this, Android Studio is suggesting a newer API, then go with it.
  10. Click on the Next button. Now we can see the Add an activity to Mobile window:
    Creating the project
  11. In the previous sections, we discussed Java classes in brief. An 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:
    Creating the project
  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.
  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:
    Creating the project
  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.

Tip

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.

A note on version controlling

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:

  • 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.

Tip

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.

Deploying and testing the app

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

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:
    Deploying to an emulator
  2. Next, you will see the Choose Device window. Click on the Launch emulator option as shown in the next screenshot:
    Deploying to an emulator
  3. Click on OK to launch the emulator. Nexus 5 comes preconfigured when you install Android Studio, but you can also choose and customize emulators yourself. Refer to the tip Creating a new emulator after this tutorial.
  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:
    Deploying to an emulator
  5. We need to simulate swiping a finger up on the screen to unlock. So click and drag up from somewhere near the bottom on the emulator screen. You can now see your first app in action, as shown in the following screenshot:
    Deploying to an emulator
  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:
    Deploying to an emulator
  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.
    Deploying to an emulator
  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.

Tip

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.

Note

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.

Deploying to a real Android 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.

Tip

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:

  1. Click on the Settings menu option or the Settings app.
  2. Click on Developer options.
  3. Tick the checkbox for USB Debugging.
  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:
    Deploying to a real Android device
  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.

You have been reading a chapter from
Android Programming for Beginners
Published in: Dec 2015
Publisher: Packt
ISBN-13: 9781785883262
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image