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
€20.98 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

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 : 9781789531039
Vendor :
Google
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.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
€189.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
€264.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 111.97
Mastering iOS 12 Programming
€41.99
Android Programming for Beginners
€36.99
Android 9 Development Cookbook
€32.99
Total 111.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

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.