Android Programming for Beginners Third Edition (John Horton)(Z-Library)
Mobile
Build in-depth, full-featured Android apps starting from zero programming experience
2
Views
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
Page
1
(This page has no text content)
Page
2
BIRMINGHAM—MUMBAI Android Programming for Beginners – Third Edi t ion Copyright © 2021 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Associate Group Product Manager: Pavan Ramchandani Publishing Product Manager: Rohit Rajkumar Senior Editor: Keagan Carneiro Content Development Editor: Divya Vijayan Technical Editor: Shubham Sharma Copy Editor: Safis Editing Project Coordinator: Manthan Patel Proofreader: Safis Editing Indexer: Rekha Nair Production Designer: Jyoti Chauhan
Page
3
First published: December 2015 Second edition: October 2018 Third edition: April 2021 Production reference: 1080421 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-80056-343-8 www.packt.com Contr ibutors About the author John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School. About the reviewers Brandon Kitt is a 24-year-old mobile app developer and enthusiast. He has worked on all kinds of projects, ranging from Java, Kotlin, Flutter, and a list of other non-mobile-specific applications, and is passionate about modern development and set on simplifying and accelerating all development processes. Brandon believes architecture, simplicity, and innovation are the building blocks of all great apps and the core foundation they are laid on. Through self-learning and an eye for clean, readable, and maintainable code, Brandon has furthered his education, exploring new technologies and toolkits that are released exceptionally quickly in the mobile development industry, to take full advantage of those changes in the process of making modern, attractive apps.
Page
4
Brandon's always available for partnership and collaboration whenever and wherever possible. You can reach Brandon on LinkedIn, Facebook, or on hotmail at brandon.kitt. Mohammed Fathy is a creative software engineer. He does what is possible with code in order to make life more efficient for people. He has been in the software engineering business for over 10 years, building new and innovative solutions for top companies in many countries. He works with Java and Kotlin to build Android apps.
Page
5
Table of Contents Preface
Page
6
Chapter 1 : Beginning Android and Java Technical requirements What's new in the third edit ion? Why Java and Android? The beginner's f irst stumbling block How Java and Android work together Understanding the Android API Java is object-oriented Run that by me again – what exactly is Android? Android Studio Sett ing up Android Studio What makes an Android app? Android resources The structure of Android's Java code Packages Classes Methods Building our f irst Android app Deploying the app so far Running and debugging the app on an Android emulator Running the app on a real device Frequently asked questions Summary
Page
7
Chapter 2 : First Contact: Java, XML, and the UI Designer Technical requirements Examining the logcat output Fi l tering the logcat output Exploring the project Java and the main layout XML Examining the MainActivity. java f i le Examining the app layout f i le Adding buttons to the main layout f i le Adding a button via the visual designer Adding a button by edit ing the XML code Giving the buttons unique id attr ibutes Posit ioning the two buttons in the layout Making the buttons cal l different methods Leaving comments in our Java code Coding messages to the user and the developer Writ ing our f irst Java code Adding message code to the onCreate method Writ ing our own Java methods Frequently asked questions Summary
Page
8
Chapter 3 : Exploring Android Studio and the Project Structure Technical requirements Project Explorer and project anatomy Exploring the f i le and folder structure of the Empty Activi ty project template Exploring an Empty Activi ty project Exploring the f i le and folder structure of the Basic Activi ty project template Exploring a Basic Activi ty project Exploring the Android emulator Emulator control panel Using the emulator as a real device Summary
Page
9
Chapter 4 : Gett ing Started with Layouts and Material Design Technical requirements Finding out about Material Design Exploring Android UI design Introducing layouts Creating and exploring a layout project Building a layout with LinearLayout Adding a LinearLayout layout type to a project Preparing your workspace Examining the generated XML Adding a TextView to the UI Wiring up the UI with the Java code (Part 1) Adding layouts within layouts Making the layout look pretty Wiring up the UI with Java code (Part 2) Building a precise UI with ConstraintLayout Adding a CalendarView Adding and constraining more UI elements Making the text cl ickable Laying out data with TableLayout Adding a TableRow element to TableLayout Organizing the table columns Linking back to the main menu Summary
Page
10
Chapter 5 : Beauti ful Layouts with CardView and Scrol lView Technical requirements Attr ibutes quick summary Sizing using dp Sizing fonts using sp Determining size with wrap or match Using padding and margin Using the layout_weight property Using gravity Building a UI with CardView and Scrol lView Sett ing the view with Java code Adding image resources Creating the contents for the cards Defining dimensions for CardView Adding CardView to our layout Creating a tablet emulator Frequently asked question Summary
Page
11
Chapter 6 : The Android Lifecycle Technical requirements The l i fe and t imes of an Android app How Android interacts with our apps A simpli f ied explanation of the Android l i fecycle The l i fecycle phases demystif ied How we handle the l i fecycle phases Lifecycle demo app Coding the l i fecycle demo app Running the l i fecycle demo app Examining the l i fecycle demo app output Some other overridden methods The structure of Java code – revisited Introducing fragments and the l i fecycle Summary
Page
12
Chapter 7 : Java Variables, Operators, and Expressions Technical requirements Java is everywhere Understanding Java syntax and jargon More code comments Storing and using data with variables Types of variables Using variables Variable declaration Variable init ial ization Changing values in variables with operators The assignment operator The addit ion operator The subtraction operator The division operator The mult ipl ication operator The increment operator The decrement operator Trying out expressions Expressing Yourself demo app Summary
Page
13
Chapter 8 : Java Decisions and Loops Technical requirements Making decisions in Java Indenting code for clari ty More operators How to use al l these operators to test variables Switching to make decisions The switch demo app Repeating code with loops while loops do while loops for loops Loops demo app Summary
Page
14
Chapter 9 : Learning Java Methods Technical requirements Methods revisited What exactly are Java methods? Method structure Modif ier Return type Name of the method Parameters The body Using method demo apps Real-world methods Exploring method overloading Scope and variables revisited Method recursion Questions Summary Further reading
Page
15
Chapter 10 : Object-Oriented Programming Technical requirements Important memory management warning Object-oriented programming What is OOP exactly? Why do i t l ike this? Class recap Looking at the code for a class Class implementation Declaring, init ial izing, and using an object of the class Basic classes app More things we can do with our f irst class Frequently asked questions Summary
Page
16
Chapter 11 : More Object-Oriented Programming Technical requirements Remember that encapsulation thing? Control l ing class use with access modif iers Control l ing variable use with access modif iers Methods can have access modif iers too Accessing private variables with getters and setters Sett ing up our objects with constructors Static methods Encapsulation and stat ic methods mini-app OOP and inheritance Inheritance example app Polymorphism Abstract classes Interfaces Frequently asked questions Summary
Page
17
Chapter 12 : The Stack, the Heap, and the Garbage Collector Technical requirements All the Android UI elements are classes too Re-introducing references So how does this heap thing help me? Using buttons and TextView widgets from our layout Running the app Inner and anonymous classes Frequently asked questions Summary
Page
18
Chapter 13 : Anonymous Classes – Bringing Android Widgets to Life Technical requirements Declaring and init ial izing the objects Creating UI widgets from pure Java without XML Exploring the palette – part 1 The EditText widget The ImageView widget Radio buttons and groups Anonymous classes Exploring the palette and more anonymous classes – part 2 Switch CheckBox TextClock Using WebView The Widget Exploration app Sett ing up the Widget Exploration project and UI Coding the Widget Exploration app Using WebView Running the Widget Exploration app Summary
Page
19
Chapter 14 : Android Dialog Windows Technical requirements Dialog windows Creating the Dialog Demo project Coding a DialogFragment class Using the DialogFragment class The Note to Self app Using naming conventions and str ing resources How to get the code f i les for the Note to Self app The completed app Building the project Preparing the str ing resources Coding the Note class Implementing the dialog designs Coding the dialog boxes Showing our new dialogs Summary
Page
20
Chapter 15 : Arrays, Maps, and Random Numbers Technical requirements A random diversion Handling large amounts of data with arrays Arrays are objects Simple array example mini-app Gett ing dynamic with arrays Dynamic array example Entering the nth dimension with arrays Mult idimensional array mini-app Array-out-of-bounds exceptions ArrayList The enhanced for loop Arrays and ArrayList instances are polymorphic More Java col lections – meet the Java HashMap The Note to Self app Frequently asked questions Summary
The above is a preview of the first 20 pages. Register to read the complete e-book.
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
AI guide
【One-Line Pitch】
A patient, project-driven introduction to Android development that assumes no prior coding experience and walks you from installing Android Studio to building complete apps with Java, XML layouts, and Material Design. Best for absolute beginners who learn by doing rather than by reading theory.
【Book Arc】
- **Opening (~0%–10%)**: Sets up the development environment and gets a first app running, establishing what Android and Java are and how they complement each other.
- **Early (~10%–32%)**: Explores Android Studio project structure, the emulator, and builds progressively richer layouts (LinearLayout, ConstraintLayout, TableLayout, ScrollView, CardView) while introducing Material Design.
- **Middle (~32%–55%)**: Shifts to Java fundamentals—variables, operators, expressions, decisions, loops, and object-oriented programming—so the reader can write logic behind the UI.
- **Late (~55%–80%)**: Applies Java and UI skills to the Android lifecycle, animations and interpolators, and interactive components like SeekBar.
- **Ending (~80%–100%)**: Consolidates everything into full-featured apps, with FAQ sections and summaries reinforcing each chapter's concepts.
【Key Takeaways】
- **The book is project-first, not theory-first** (Opening): every concept is introduced through a working app, so you learn by building rather than by memorizing syntax.
- **Java and XML are taught in parallel** (Early): UI is defined in XML layouts while behavior lives in Java, and understanding how the two connect is the core skill the book builds.
- **Layout mastery is a major early investment** (Early): LinearLayout, ConstraintLayout, TableLayout, ScrollView, and CardView are covered in depth before serious Java work begins.
- **The Android lifecycle determines where your code goes** (Middle): knowing onCreate, onDestroy, and the flow between lifecycle phases is essential for placing logic correctly.
- **Object-oriented programming is the conceptual pivot** (Middle): classes, encapsulation, inheritance, and polymorphism are introduced with the explicit advice not to memorize but to "just-about get it" and revisit later.
- **Animations and interpolators add polish** (Late): XML-defined animations controlled via Java, plus SeekBar interaction, show how to make apps feel finished.
- **The emulator is a first-class tool** (Early): rotating, simulating back/home buttons, and accessing advanced settings like GPS and sensors are treated as core workflow skills.
- **Small logic errors matter** (Middle): the bridge-crossing example shows how an unhandled equality case can silently fall into the wrong branch—a lesson in defensive conditionals.
【Reading Tips】
- **Deep-read the layout chapters (4–5) and the Java fundamentals (7–9)**; these are the foundation everything else builds on.
- **Skim the environment setup and emulator controls** on a first pass, then return when you actually need a specific setting.
- **Do not try to memorize the OOP chapters**—the author explicitly warns against it; aim for working understanding and revisit as needed.
- **Type out every code example** rather than copying; the book's value is in the repetition of building and running.
- **Use the FAQ and summary sections at the end of each chapter** as quick self-checks before moving on.
【Coverage Limits】
The excerpts cover the book's structure, early setup, layout work, Java fundamentals, lifecycle, and animations, but do not detail the final chapters' full app projects or any advanced topics beyond animations. Specific code listings and later-chapter content are only partially represented.
Passage locations
Excerpt 1
long with understanding the composition of our projects, it will also be beneficial to make sure we get the most from the emulator. Chapter 4, Getting Starte...
View in text
Excerpt 2
"info", "Done creating the app"); Figure 3.1 – Hello world! Now that we have a brand-new Empty Activity project, let's explore the files and folders that And...
View in text
Excerpt 3
and configure them the same as the first with one exception. On the second CardView, set cardElevation to 22dp and on the third CardView, set cardElevation t...
View in text
Excerpt 4
the text property and countDown for the onClick property. 5. Place a new button just below the previous one and repeat steps 2 and 3 but this time use nested...
View in text
Recommended for You
{{#thumbnailUrl}}
{{/thumbnailUrl}}
{{^thumbnailUrl}}
{{/thumbnailUrl}}
Loading recommended books...
Failed to load, please try again later
Tip the Site
Scan the WeChat Pay or Alipay code to tip. No login required.
WeChat Pay
Alipay