Book Image

Android Programming for Beginners

By : John Horton, Paresh Mayani
Book Image

Android Programming for Beginners

By: John Horton, Paresh Mayani

Overview of this book

Android is the most popular OS in the world. There are millions of devices accessing tens of thousands of applications. It is many people's entry point into the world of technology; it is an operating system for everyone. Despite this, the entry-fee to actually make Android applications is usually a computer science degree, or five years’ worth of Java experience. Android Programming for Beginners will be your companion to create Android applications from scratch—whether you’re looking to start your programming career, make an application for work, be reintroduced to mobile development, or are just looking to program for fun. We will introduce you to all the fundamental concepts of programming in an Android context, from the Java basics to working with the Android API. All examples are created from within Android Studio, the official Android development environment that helps supercharge your application development process. After this crash-course, we’ll dive deeper into Android programming and you’ll learn how to create applications with a professional-standard UI through fragments, make location-aware apps with Google Maps integration, and store your user’s data with SQLite. In addition, you’ll see how to make your apps multilingual, capture images from a device’s camera, and work with graphics, sound, and animations too. By the end of this book, you’ll be ready to start building your own custom applications in Android and Java.
Table of Contents (37 chapters)
Android Programming for Beginners
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Do you have a great idea for an app, but don't know how to make it a reality? Are you trying to start a career in programming, but haven't found the right way in? Or maybe you're just frustrated that all the so-called "beginner's guides" to learn Android require you to know Java. If so, Android Programming for Beginners is for you. But why choose Android?

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 corporation—nobody 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, 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.

This is no longer considered geeky, nerdy, or reclusive; developing Android apps is considered highly skillful and really successful developers are hugely admired, even revered.

If all this fluffy kind of spiritual stuff doesn't mean anything to you, then that's fine too; developing for Android can make you a good living or even make you wealthy. With the continued growth of device ownership, the ongoing increase in CPU and GPU power and the non-stop evolution of the Android operating system (OS) itself, the need for professional app developers is only going to grow.

In short, the best Android developers—and perhaps more importantly, Android developers with the best ideas—are in greater demand than ever. Nobody knows who these future Android coding heroes are and they might not even have written their first line of Java yet.

But is this book for me?

So why isn't everybody an Android developer? Obviously, not everybody will share my enthusiasm for the thrill of creating software that can help people make their lives better, but I'm guessing that because you are reading this, you might.

Unfortunately, for those who do, there is a kind of glass wall on the path of progress that frustrates many aspiring Android developers.

Android uses Java to make its apps respond, think, and communicate with users. Every Android book, even those aimed at so-called beginners, assumes at least an intermediate level of Java and most (if not all), a fairly advanced level. So, good to excellent Java knowledge is a prerequisite for learning Android.

Unfortunately, learning Java in a completely different context to Android can sometimes be a little dull, and some of what you learn is not directly transferable into the world of Android either.

I think it makes more sense, is vastly more enjoyable, and is significantly quicker and more rewarding, to teach Java in a purely Android environment—to teach Java with the single overriding goal of learning to develop professional standard Android apps. And that's what this book is about.

What this book covers

Chapter 1, The First App, sets up your Android development environment, and then it helps you build and deploy your first simple app to an emulator and a real Android device.

Chapter 2, Java – First Contact, teaches some absolute beginner Java coding as well as shows you how to implement your first user interface design.

Chapter 3, Exploring Android Studio, gets to grips with the tools of the trade of professional Android developers with a fast tour around Android Studio.

Chapter 4, Designing Layouts, focuses on building user interfaces by exploring the layouts and widgets that make up an Android app as well as builds some apps that demo our new skills.

Chapter 5, Real-World Layouts, takes user interfaces a step further by designing and implementing some apps with layouts more like what you would see on professional Android apps.

Chapter 6, The Life and Times of an Android App, explores how the Android operating system works under the surface by building apps that demonstrate the Android lifecycle and how we make it work for us.

Chapter 7, Coding in Java Part 1 – Variables, Decisions, and Loops, gives you the first major glimpse of Java, the native language of Android, and the way we will be bringing our apps to life. Zero previous coding experience is assumed.

Chapter 8, Coding in Java Part 2 – Methods, discusses that methods are one of the key building blocks of all the apps we will make in this book. In this chapter, we will build a few working mini apps to explore how to use methods.

Chapter 9, Object-Oriented Programming, states that OOP is the way that all modern programming is done, especially Java. This chapter holds the key to using all the advanced and exciting features contained in Android.

Chapter 10, Everything's a Class, will bring together everything we have seen in all the other chapters so far. By the end of this chapter, our design and Java skills will be as one and we will be well placed to start building apps with more advanced features.

Chapter 11, Widget Mania, covers the use of our new OOP, Java, and UI knowledge that we will be able to use and interact with many of the user interface features available in Android.

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 most advanced part 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.

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.

What you need for this book

Android Studio running on any of the major operating systems can use the code in this book. Android Studio is a free software and full setup instructions for Windows are in the opening chapter.

Android Studio is the recommended development tool, and at the time of publication, the minimum system requirements were as follows:

Windows

  • Microsoft® Windows® 8/7/Vista/2003 (32 or 64 bit)

  • 2 GB RAM minimum; however, 4 GB RAM is recommended

  • 400 MB hard disk space

  • At least 1 GB for Android SDK, emulator system images, and caches

  • 1280 x 800 minimum screen resolution

  • Java Development Kit (JDK) 7

  • Optional for accelerated emulator: Intel® processor with support for Intel® VT-x, Intel® EM64T (Intel® 64), and Execute Disable (XD) Bit functionality

Mac OS X

  • Mac® OS X® 10.8.5 or a higher version, up to 10.9 (Mavericks)

  • 2 GB RAM minimum; however, 4 GB RAM is recommended

  • 400 MB hard disk space

  • At least 1 GB for the Android SDK, emulator system images, and caches

  • 1280 x 800 minimum screen resolution

  • Java Runtime Environment (JRE) 6

  • Java Development Kit (JDK) 7

  • Optional for accelerated emulator: Intel® processor with support for Intel® VT-x, Intel® EM64T (Intel® 64), and Execute Disable (XD) Bit functionality

On a Mac OS, run Android Studio with Java Runtime Environment (JRE) 6 for optimized font rendering. You can then configure your project to use Java Development Kit (JDK) 6 or JDK 7.

Linux

  • A GNOME or KDE desktop

  • GNU C Library (glibc) 2.15 or later

  • 2 GB RAM minimum; however, 4 GB RAM is recommended

  • 400 MB hard disk space

  • At least 1 GB for the Android SDK, emulator system images, and caches

  • 1280 x 800 minimum screen resolution

  • Oracle® Java Development Kit (JDK) 7

Tested on Ubuntu® 14.04, Trusty Tahr (64-bit distribution capable of running 32-bit applications).

Who this book is for

Are you trying to start a career in programming, but haven't found the right way in? Do you have a great idea for an app, but don't know how to make it a reality? Or maybe you're just frustrated that "to learn Android, you must know Java." If so, this book is for you. You don't need any programming experience to follow along with this book, just a computer and a sense of adventure.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We will see this, perhaps most distinctly, when we look at RelativeLayout later in the chapter."

A block of code is set as follows:

android:layout_below="@+id/textView"

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

<RelativeLayout xmlns:
  android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" 
      android:layout_width="match_parent"
    android:layout_height="match_parent" 
      android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" 
      tools:context=".LayoutExperiments">

</RelativeLayout>

Any command-line input or output is written as follows:

Rambo's health =﹕ 150
Vassily's health =﹕ 50
Wellington's health =﹕ 100
Green Beret﹕ is shooting
Sniper﹕ is shooting
Sailor﹕ is shooting

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "As we have already seen, widgets have properties that we can either set in XML or through the Properties window."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

All the finished code and resources for every one of the projects in this book can be found in the download bundle in a folder named after the chapter number for example Chapter 1, Chapter 2, and so on. Furthermore inside each of these folders you will find a sub-folder with the name of the app. Then as you would expect, within this folder will be the code files and the resources for that app. And finally, the resources and code files are further sub divided into folder names that correspond to their correct place within a project. So for example, to explore the completed files for the project in Chapter 2, Java – First Contact you would look in Chapter 2/Hello Android. In there you will find code files and resources in folders named java and layout. As we learn about and use more resource types, so the number of subfolders within the application folder in the download bundle will grow. Our Android Studio projects actually contain dozens of files and folders. The download bundle will only contain the files and folders we need to edit, or that are being discussed. We will take a closer look at the structure of an Android project in the second chapter.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/AndroidProgrammingforBeginners_ColoredImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.