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
Android Programming for Beginners
Android Programming for Beginners

Android Programming for Beginners: Build in-depth, full-featured Android 9 Pie apps starting from zero programming experience , Second Edition

eBook
$27.98 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Android Programming for Beginners

Chapter 2. First Contact – Java, XML, and the UI Designer

At this stage, we have a working Android development environment and we have built and deployed our first app. It is obvious, however, that code auto-generated by Android Studio is not going to make the next top-selling app on the Google Play Store. We need to explore this auto-generated code so that we can begin to understand Android and then learn how to build on this useful template. With this aim in mind, in this chapter, we will do the following:

  • See how to get technical feedback from our apps
  • Examine the Java code and UI XML code from our first app
  • Get our first taste of using the Android User Interface (UI) designer
  • Write our first Java code
  • Learn some core Java fundamentals and how they relate to Android

First, let's see how to get feedback from our apps.

Examining the log output

In the previous chapter, we mentioned that our app was running in debug mode on the emulator or real device so that we can monitor it and get feedback when things go wrong. So where is all this feedback then?

You might have noticed a whole load of scrolling text at the bottom of the Android Studio window. If not, click on the logcat tab, as shown by the highlighted area labelled as 1 in the following screenshot:

Tip

Note that the emulator must be running, or a real device must be attached in debugging mode, for you to see the following window. Furthermore, if you restarted Android Studio for some reason and have not yet executed the app, then the logcat window will be empty. Refer to the first chapter to get the app running on an emulator or a real device:

Examining the log output

You can drag the window to make it taller, just like you can in most other Windows applications, if you want to see more.

This window is called the logcat, or sometimes it is referred to as the console. It is our...

A note for early adopters of this book

At time of completing this book, Android 9 and Android Studio 3.2 had just been released. This book was written to accommodate these latest versions. One of the changes in the new releases is the way that Android supports devices running older versions of Android. It has just been significantly improved. Android uses a support library, which means that old devices (within reason) can make use of newer features.

The good news is that this book uses the new, improved version!

However, if you are a very early adopter (late 2018 and maybe into early 2019) of this book and you look very closely at the code generated by Android Studio, you will notice some slight differences with the code presented in the book. The differences occur in the import… statements at the top of the Java code files. The book presents code that looks a bit like this:

import androidx.appcompat.app.AppCompatActivity;

Whereas you might notice code in Android Studio 3.2 or earlier...

Exploring the project's Java code and the main layout's XML code

We are going to look at the resource files that have the code that defines our simple UI layout and the file that has our Java code. At this stage, we will not try to understand it all as we need to learn some more basics before it makes sense to do so. What we will see, however, is the basic content and structure of both files so we can reconcile their content with what we already know about Android resources and Java.

Examining the HelloWorldActivity.java file

Let's look at the Java code first. You can see this code by left-clicking on the HelloWorldActivity.java tab, as shown in the following screenshot:

Examining the HelloWorldActivity.java file

As we are not looking at the intricate details of the code, an annotated screenshot is more useful than reproducing the actual code in text form. Regularly refer to the following screenshot while reading on with this section:

Examining the HelloWorldActivity.java file

The first thing to note is that I have added a few empty lines amongst the code to space...

Adding buttons to the main layout file

Here, we will add a couple of buttons to the screen, and we will then see a fast way to make them actually do something. We will add a button in two different ways: first, using the visual designer, and second, by adding to and editing XML code directly.

Adding a button via the visual designer

To get started adding our first button, switch back to the design view by clicking the Design tab underneath the XML code we have just been discussing, as shown next:

Adding a button via the visual designer

Notice that to the left-hand side of the layout, we have a window that is called the Palette, and this is shown next:

Adding a button via the visual designer

The palette is divided into two parts. The left-hand list has the categories of UI elements and allows you to select a category, while the right-hand side shows you all the available UI elements from the currently selected category.

Make sure that the Common category is selected as shown in the previous screenshot. Now, left-click and hold on the Button widget and then drag it onto the...

Leaving comments in our Java code

In programming it is always a clever idea to write messages known as code comments and sprinkle them liberally amongst your code. This is to remind us of what we were thinking at the time we wrote the code. To do this, you simply append a double forward slash and then type your comment, as follows:

// This is a comment and it could be useful

In addition, we can use comments to comment out a line of code. Suppose we have a line of code that we temporarily want to disable. Then we can do so by adding two forward slashes, as follows:

// The code below used to send a message
// Log.i("info","our message here");
// But now it doesn't do anything
// And I am getting ahead of where I should be

Tip

Using comments to comment out code should only be a temporary measure. Once you have found the correct code to use, commented-out code should be cut to keep the code file clean and organized.

Let's look at two separate ways to send messages in...

Coding messages to the user and the developer

In the introduction to this chapter and in the previous chapter, we talked a bit about using other people's code, specifically via the classes and their methods of the Android API. We saw that we could do some quite complex things with insignificant amounts of code (such as talking to satellites).

To get us started coding, we are going to use two different classes from the Android API that allow us to output messages. The first class, Log, allows us to output messages to the logcat window. The second class, Toast, is not a tasty breakfast treat, but it will produce a toast-shaped pop-up message for our app's user to see.

Here is the code we need to write to send a message to the logcat:

Log.i("info","our message here");

Exactly why this works will become clearer in Chapter 10, Object-Oriented Programming, but for now we just need to know that whatever we put between the two sets of quote marks will be output to the...

Writing our first Java code

So, we now know the code that will output to logcat or the user's screen. But where do we put the code? To answer this question, we need to understand that the onCreate method in HelloWorldActivity.java executes as the app is preparing to be shown to the user. So, if we put our code at the end of this method, it will run just as the user sees it. Sounds good.

Tip

We know that to execute the code in a method, we need to call it. We have wired our buttons up to call a couple of methods, topClick and bottomClick. Soon, we will write these methods. But who or what is calling onCreate? The answer to this mystery is that Android itself calls onCreate in response to the user clicking the app icon to run the app. In Chapter 6, The Android Lifecycle, we will look deeper, and it will be clear exactly what code executes and when. You don't need to completely comprehend this now. I just wanted to give you an overview of what was going on.

Let's quickly try this...

Examining the log output


In the previous chapter, we mentioned that our app was running in debug mode on the emulator or real device so that we can monitor it and get feedback when things go wrong. So where is all this feedback then?

You might have noticed a whole load of scrolling text at the bottom of the Android Studio window. If not, click on the logcat tab, as shown by the highlighted area labelled as 1 in the following screenshot:

Note

Note that the emulator must be running, or a real device must be attached in debugging mode, for you to see the following window. Furthermore, if you restarted Android Studio for some reason and have not yet executed the app, then the logcat window will be empty. Refer to the first chapter to get the app running on an emulator or a real device:

You can drag the window to make it taller, just like you can in most other Windows applications, if you want to see more.

This window is called the logcat, or sometimes it is referred to as the console. It is our app...

A note for early adopters of this book


At time of completing this book, Android 9 and Android Studio 3.2 had just been released. This book was written to accommodate these latest versions. One of the changes in the new releases is the way that Android supports devices running older versions of Android. It has just been significantly improved. Android uses a support library, which means that old devices (within reason) can make use of newer features.

The good news is that this book uses the new, improved version!

However, if you are a very early adopter (late 2018 and maybe into early 2019) of this book and you look very closely at the code generated by Android Studio, you will notice some slight differences with the code presented in the book. The differences occur in the import… statements at the top of the Java code files. The book presents code that looks a bit like this:

import androidx.appcompat.app.AppCompatActivity;

Whereas you might notice code in Android Studio 3.2 or earlier that...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Kick-start your Android programming career, or just have fun publishing apps to the Google Play marketplace
  • A first-principles introduction to Java, via Android, which means you'll be able to start building your own applications from scratch
  • Learn by example and build four real-world apps and dozens of mini-apps throughout the book

Description

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 in order to learn Android, you must know Java. If so, then this book is for you. This new and expanded second edition of Android Programming for Beginners will be your companion to create Android Pie applications from scratch. We will introduce you to all the fundamental concepts of programming in an Android context, from the basics of Java to working with the Android API. All examples use the up-to-date API classes, and 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 and store your user's data with SQLite. In addition, you'll see how to make your apps multilingual, draw to the screen with a finger, 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.

Who is this book for?

This book is for you if you are completely new to Java, Android, or programming and want to make Android applications. This book also acts as a refresher for those who already have experience of using Java on Android to advance their knowledge and make fast progress through the early projects.

What you will learn

  • Master the fundamentals of coding Java for Android Pie
  • Install and set up your Android development environment
  • Build functional user interfaces with the Android Studio visual designer
  • Add user interaction, data captures, sound, and animation to your apps
  • Manage your apps data using the built-in Android SQLite database
  • Find out about the design patterns used by professionals to make top-grade applications
  • Build, deploy, and publish real Android applications to the Google Play marketplace

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Oct 31, 2018
Length: 766 pages
Edition : 2nd
Language : English
ISBN-13 : 9781789538502
Vendor :
Google
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Oct 31, 2018
Length: 766 pages
Edition : 2nd
Language : English
ISBN-13 : 9781789538502
Vendor :
Google
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 147.97
Mastering iOS 12 Programming
$54.99
Android Programming for Beginners
$48.99
Android 9 Development Cookbook
$43.99
Total $ 147.97 Stars icon
Banner background image

Table of Contents

32 Chapters
1. Beginning Android and Java Chevron down icon Chevron up icon
2. First Contact – Java, XML, and the UI Designer Chevron down icon Chevron up icon
3. Exploring Android Studio and the Project Structure Chevron down icon Chevron up icon
4. Getting Started with Layouts and Material Design Chevron down icon Chevron up icon
5. Beautiful Layouts with CardView and ScrollView Chevron down icon Chevron up icon
6. The Android Lifecycle Chevron down icon Chevron up icon
7. Java Variables, Operators, and Expressions Chevron down icon Chevron up icon
8. Java Decisions and Loops Chevron down icon Chevron up icon
9. Java Methods Chevron down icon Chevron up icon
10. Object-Oriented programming Chevron down icon Chevron up icon
11. More Object-Oriented Programming Chevron down icon Chevron up icon
12. The Stack, the Heap, and the Garbage Collector Chevron down icon Chevron up icon
13. Anonymous Classes – Bringing Android Widgets to Life Chevron down icon Chevron up icon
14. Android Dialog Windows Chevron down icon Chevron up icon
15. Arrays, ArrayList, Map and Random Numbers Chevron down icon Chevron up icon
16. Adapters and Recyclers Chevron down icon Chevron up icon
17. Data Persistence and Sharing Chevron down icon Chevron up icon
18. Localization Chevron down icon Chevron up icon
19. Animations and Interpolations Chevron down icon Chevron up icon
20. Drawing Graphics Chevron down icon Chevron up icon
21. Threads, and Starting the Live Drawing App Chevron down icon Chevron up icon
22. Particle Systems and Handling Screen Touches Chevron down icon Chevron up icon
23. Supporting Different Versions of Android, Sound Effects, and the Spinner Widget Chevron down icon Chevron up icon
24. Design Patterns, Multiple Layouts, and Fragments Chevron down icon Chevron up icon
25. Advanced UI with Paging and Swiping Chevron down icon Chevron up icon
26. Advanced UI with Navigation Drawer and Fragment Chevron down icon Chevron up icon
27. Android Databases Chevron down icon Chevron up icon
28. Coding a Snake Game Using Everything We Have Learned So Far Chevron down icon Chevron up icon
29. Enumerations and Finishing the Snake Game Chevron down icon Chevron up icon
30. A Quick Chat Before You Go Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.8
(13 Ratings)
5 star 53.8%
4 star 15.4%
3 star 7.7%
2 star 7.7%
1 star 15.4%
Filter icon Filter
Top Reviews

Filter reviews by




Anil Nov 03, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
good book for beginers
Amazon Verified review Amazon
Sand George Ionut Nov 05, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
good
Amazon Verified review Amazon
skoob Aug 14, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I've some rudimentary experience with BASIC programming including Visual Studio and Python but nothing of Java - except the OOP in evidence in Python. However, I'm currently up to chapter 13 and having a great time. There's heaps of stuff you just won't get at first and the author is very honest about that. You'll forget a ton of what you've read but once you start thinking about how to create or recycle code to make your own apps, a lot just starts dropping into place. The Java and Android APIs are vast so the author lays out an expectation of growing familiarity as the book progresses rather than an expectation that the reader will instantly grasp it all straight away. I never thought I could write a simple app at this point but I've made a simple maths app to test my daughter's basic math skills on her phone already and I'm more than happy with that so early in the book. There are some slight difference I noticed with the behaviour of the current version of Android Studio but they are readily resolved and troubleshooting is all part of the programming experience.An excellent text from an author who clearly empathises with the beginner.
Amazon Verified review Amazon
David W Morgan Jan 21, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Although I am only through about a quarter of the book, I am finding that things pretty much work as described. I am finding the IDE slightly different, but the examples work. I think this in itself is a great achievement. So far, no broken code due to printing errors, the code works. I have typed it all in, not used cut and paste. I did not see it mentioned in the book so far, but you really don't have to type all the code. A lot is suggested for you in the IDE, you just need to choose it.This book is easily readable, I think would be even for beginners. I am not a programmer, but have done a little code writing. I think the examples are very good. Creating the first game takes several chapters, and proceeds by small steps. The first few chapters do not produce code on an output device, but do produce debugging code. This lets you learn to use the IDE, find errors, and is very much the way I would choose to use debugging code. That and the comments may be excessive for an experienced programmer, but I think are good for a beginner.I have the kindle version, and am not impressed by that. I would like to write notes on the pages, and it looks like kindle does not let you write free form on the page. You can attach notes, but then they appear as small note boxes on the written text. You need to open them to see what is in them. I should have bought the print version.The book has a number of punctuation errors, mostly misplaced commas. Some words run together, or have a space in the middle of a word. These are obvious, and don't get in the way of learning.I am very impressed that using the book I have been able to get Android Studio set up and running on my Mint 18.3 Linux platform. Also that the examples really work. The Android Studio that I am running is 3.2.1, and the book still applies.
Amazon Verified review Amazon
Juan Apr 12, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
He usado varios manuales de programación y, en mi opinión, éste está perfectamente organizado. Los contenidos se ofrecen de forma precisa, clara, comprensible, y, lo más importante, de forma progresiva, bajo la idea de que el lector no posee conocimientos previos. También se introducen referencias para la ampliación de conocimientos, así como notas con ciertas singularidades del entorno Android, lo cual me parece realmente útil. En general estoy muy satisfecho con la compra.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.