Android Programming for Beginners - Third Edition

Book description

Learn the Java and Android skills you need to start developing powerful mobile applications with the help of actionable steps

Key Features

  • Kick-start your Android programming career or just have fun publishing apps to the Google Play marketplace
  • Get a first principles introduction to using Java and Android and prepare to start building your own apps from scratch
  • Learn by example by building four real-world apps and dozens of mini apps

Book Description

Do you want to make a career in programming but don't know where to start? Do you have a great idea for an app but don't know how to make it a reality? Or are you worried that you'll have to learn Java programming to become an Android developer? Look no further! This new and expanded third edition of Android Programming for Beginners will be your guide to creating Android applications from scratch.

The book starts by introducing you to all the fundamental concepts of programming in an Android context, from the basics of Java to working with the Android API. You'll learn with the help of examples that use up-to-date API classes and are created within Android Studio, the official Android development environment that helps supercharge your mobile application development process. After a crash course on the key programming concepts, you'll explore Android programming and get to grips with creating applications with a professional-standard UI using fragments and storing user data with SQLite. This Android Java book also shows you how you can make your apps multilingual, draw on the screen with a finger, and work with graphics, sound, and animations.

By the end of this Android programming book, you'll be ready to start building your own custom applications in Android and Java.

What you will learn

  • Understand the fundamentals of coding in Java for Android
  • 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
  • Explore the design patterns used by professionals to build top-grade applications
  • Build real-world Android applications that you can deploy to the Google Play marketplace

Who this book is for

This Android book is for you if you are completely new to Java, Android, or programming and want to get started with Android app development. If you have experience of using Java on Android, this book will serve as a refresher to help you advance your knowledge and make progress through the early projects covered in the book.

Publisher resources

Download Example Code

Table of contents

  1. Android Programming for Beginners – Third Edition
  2. Contributors
  3. About the author
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Reviews
  6. Chapter 1: Beginning Android and Java
    1. Technical requirements
    2. What's new in the third edition?
    3. Why Java and Android?
    4. The beginner's first stumbling block
    5. How Java and Android work together
      1. Understanding the Android API
      2. Java is object-oriented
      3. Run that by me again – what exactly is Android?
      4. Android Studio
    6. Setting up Android Studio
    7. What makes an Android app?
      1. Android resources
    8. The structure of Android's Java code
      1. Packages
      2. Classes
      3. Methods
    9. Building our first Android app
    10. Deploying the app so far
      1. Running and debugging the app on an Android emulator
      2. Running the app on a real device
    11. Frequently asked questions
    12. Summary
  7. Chapter 2: First Contact: Java, XML, and the UI Designer
    1. Technical requirements
    2. Examining the logcat output
      1. Filtering the logcat output
    3. Exploring the project Java and the main layout XML
      1. Examining the file
      2. Examining the app layout file
    4. Adding buttons to the main layout file
      1. Adding a button via the visual designer
      2. Adding a button by editing the XML code
      3. Giving the buttons unique id attributes
      4. Positioning the two buttons in the layout
      5. Making the buttons call different methods
    5. Leaving comments in our Java code
    6. Coding messages to the user and the developer
    7. Writing our first Java code
      1. Adding message code to the onCreate method
      2. Writing our own Java methods
    8. Frequently asked questions
    9. Summary
  8. Chapter 3: Exploring Android Studio and the Project Structure
    1. Technical requirements
    2. Project Explorer and project anatomy
    3. Exploring the file and folder structure of the Empty Activity project template
      1. Exploring an Empty Activity project
    4. Exploring the file and folder structure of the Basic Activity project template
    5. Exploring a Basic Activity project
    6. Exploring the Android emulator
      1. Emulator control panel
      2. Using the emulator as a real device
    7. Summary
  9. Chapter 4: Getting Started with Layouts and Material Design
    1. Technical requirements
    2. Finding out about Material Design
    3. Exploring Android UI design
    4. Introducing layouts
      1. Creating and exploring a layout project
      2. Building a layout with LinearLayout
      3. Adding a LinearLayout layout type to a project
      4. Preparing your workspace
      5. Examining the generated XML
      6. Adding a TextView to the UI
      7. Wiring up the UI with the Java code (Part 1)
      8. Adding layouts within layouts
      9. Making the layout look pretty
      10. Wiring up the UI with Java code (Part 2)
    5. Building a precise UI with ConstraintLayout
      1. Adding a CalendarView
      2. Adding and constraining more UI elements
      3. Making the text clickable
    6. Laying out data with TableLayout
      1. Adding a TableRow element to TableLayout
      2. Organizing the table columns
    7. Linking back to the main menu
    8. Summary
  10. Chapter 5: Beautiful Layouts with CardView and ScrollView
    1. Technical requirements
    2. Attributes quick summary
      1. Sizing using dp
      2. Sizing fonts using sp
      3. Determining size with wrap or match
      4. Using padding and margin
      5. Using the layout_weight property
      6. Using gravity
    3. Building a UI with CardView and ScrollView
      1. Setting the view with Java code
      2. Adding image resources
      3. Creating the contents for the cards
      4. Defining dimensions for CardView
      5. Adding CardView to our layout
    4. Creating a tablet emulator
    5. Frequently asked question
    6. Summary
  11. Chapter 6: The Android Lifecycle
    1. Technical requirements
    2. The life and times of an Android app
    3. How Android interacts with our apps
    4. A simplified explanation of the Android lifecycle
      1. The lifecycle phases demystified
    5. How we handle the lifecycle phases
    6. Lifecycle demo app
      1. Coding the lifecycle demo app
      2. Running the lifecycle demo app
      3. Examining the lifecycle demo app output
      4. Some other overridden methods
    7. The structure of Java code – revisited
    8. Introducing fragments and the lifecycle
    9. Summary
  12. Chapter 7: Java Variables, Operators, and Expressions
    1. Technical requirements
    2. Java is everywhere
    3. Understanding Java syntax and jargon
      1. More code comments
    4. Storing and using data with variables
      1. Types of variables
    5. Using variables
      1. Variable declaration
      2. Variable initialization
    6. Changing values in variables with operators
      1. The assignment operator
      2. The addition operator
      3. The subtraction operator
      4. The division operator
      5. The multiplication operator
      6. The increment operator
      7. The decrement operator
    7. Trying out expressions
      1. Expressing Yourself demo app
    8. Summary
  13. Chapter 8: Java Decisions and Loops
    1. Technical requirements
    2. Making decisions in Java
      1. Indenting code for clarity
      2. More operators
      3. How to use all these operators to test variables
    3. Switching to make decisions
      1. The switch demo app
    4. Repeating code with loops
      1. while loops
      2. do while loops
    5. for loops
    6. Loops demo app
    7. Summary
  14. Chapter 9: Learning Java Methods
    1. Technical requirements
    2. Methods revisited
      1. What exactly are Java methods?
    3. Method structure
      1. Modifier
      2. Return type
      3. Name of the method
      4. Parameters
      5. The body
    4. Using method demo apps
      1. Real-world methods
    5. Exploring method overloading
    6. Scope and variables revisited
    7. Method recursion
    8. Questions
    9. Summary
    10. Further reading
  15. Chapter 10: Object-Oriented Programming
    1. Technical requirements
    2. Important memory management warning
    3. Object-oriented programming
      1. What is OOP exactly?
      2. Why do it like this?
      3. Class recap
    4. Looking at the code for a class
      1. Class implementation
      2. Declaring, initializing, and using an object of the class
    5. Basic classes app
      1. More things we can do with our first class
    6. Frequently asked questions
    7. Summary
  16. Chapter 11: More Object-Oriented Programming
    1. Technical requirements
    2. Remember that encapsulation thing?
      1. Controlling class use with access modifiers
      2. Controlling variable use with access modifiers
      3. Methods can have access modifiers too
      4. Accessing private variables with getters and setters
      5. Setting up our objects with constructors
    3. Static methods
    4. Encapsulation and static methods mini-app
    5. OOP and inheritance
    6. Inheritance example app
    7. Polymorphism
      1. Abstract classes
      2. Interfaces
    8. Frequently asked questions
    9. Summary
  17. Chapter 12: The Stack, the Heap, and the Garbage Collector
    1. Technical requirements
    2. All the Android UI elements are classes too
      1. Re-introducing references
      2. So how does this heap thing help me?
      3. Using buttons and TextView widgets from our layout
      4. Running the app
    3. Inner and anonymous classes
    4. Frequently asked questions
    5. Summary
  18. Chapter 13: Anonymous Classes – Bringing Android Widgets to Life
    1. Technical requirements
    2. Declaring and initializing the objects
    3. Creating UI widgets from pure Java without XML
    4. Exploring the palette – part 1
      1. The EditText widget
      2. The ImageView widget
      3. Radio buttons and groups
    5. Anonymous classes
    6. Exploring the palette and more anonymous classes – part 2
      1. Switch
      2. CheckBox
      3. TextClock
      4. Using WebView
    7. The Widget Exploration app
      1. Setting up the Widget Exploration project and UI
      2. Coding the Widget Exploration app
    8. Using WebView
    9. Running the Widget Exploration app
    10. Summary
  19. Chapter 14: Android Dialog Windows
    1. Technical requirements
    2. Dialog windows
      1. Creating the Dialog Demo project
      2. Coding a DialogFragment class
      3. Using the DialogFragment class
    3. The Note to Self app
      1. Using naming conventions and string resources
      2. How to get the code files for the Note to Self app
      3. The completed app
      4. Building the project
      5. Preparing the string resources
      6. Coding the Note class
      7. Implementing the dialog designs
      8. Coding the dialog boxes
      9. Showing our new dialogs
    4. Summary
  20. Chapter 15: Arrays, Maps, and Random Numbers
    1. Technical requirements
    2. A random diversion
    3. Handling large amounts of data with arrays
      1. Arrays are objects
    4. Simple array example mini-app
    5. Getting dynamic with arrays
      1. Dynamic array example
    6. Entering the nth dimension with arrays
      1. Multidimensional array mini-app
      2. Array-out-of-bounds exceptions
    7. ArrayList
      1. The enhanced for loop
    8. Arrays and ArrayList instances are polymorphic
    9. More Java collections – meet the Java HashMap
    10. The Note to Self app
    11. Frequently asked questions
    12. Summary
  21. Chapter 16: Adapters and Recyclers
    1. Technical requirements
    2. RecyclerView and RecyclerAdapter
      1. The problem with displaying lots of widgets
      2. The solution to the problem of displaying lots of widgets
      3. How to use RecyclerView and RecyclerAdapter
      4. What we will do to set up RecyclerView with RecyclerAdapter and an ArrayList of notes
    3. Adding RecyclerView, RecyclerAdapter, and ArrayList to the Note to Self project
      1. Removing the temporary "Show Note" button and adding RecyclerView
      2. Creating a list item for RecyclerView
      3. Coding the RecyclerAdapter class
      4. Coding MainActivity to use the RecyclerView and RecyclerAdapter classes
    4. Running the app
    5. Frequently asked questions
    6. Summary
  22. Chapter 17: Data Persistence and Sharing
    1. Technical requirements
    2. Android intents
      1. Switching Activity
      2. Passing data between activities
    3. Adding a settings page to Note to Self
      1. Creating SettingsActivity
      2. Designing the settings screen layout
      3. Enabling the user to switch to the settings screen
    4. Persisting data with SharedPreferences
    5. Reloading data with SharedPreferences
    6. Making the Note to Self settings persist
      1. Coding the SettingsActivity class
      2. Coding the MainActivity class
    7. More advanced persistence
      1. What is JSON?
      2. Java exceptions – try, catch, and finally
    8. Backing up user data in Note to Self
    9. Frequently asked questions
    10. Summary
  23. Chapter 18: Localization
    1. Technical requirements
    2. Making the Note to Self app accessible for Spanish and German speakers
      1. Adding Spanish language support
      2. Adding German language support
      3. Adding the string resources
    3. Running Note to Self in German or Spanish
      1. Making the translations work in Java code
    4. Summary
  24. Chapter 19: Animations and Interpolations
    1. Technical requirements
    2. Animations in Android
      1. Designing cool animations in XML
      2. Instantiating animations and controlling them with Java code
      3. More animation features
    3. Animation demo app – introducing SeekBar
      1. Laying out the animation demo
      2. Coding the XML animations
      3. Wiring up the animation demo app with Java
    4. Frequently asked questions
    5. Summary
  25. Chapter 20: Drawing Graphics
    1. Technical requirements
    2. Understanding the Canvas class
      1. Getting started drawing with Bitmap, Canvas, and ImageView
    3. Using the Canvas class
      1. Preparing the instances of the required classes
      2. Initializing the objects
      3. Setting the Activity content
    4. Canvas demo app
      1. Creating a new project
    5. The Android coordinate system
      1. Plotting and drawing
    6. Creating bitmaps
    7. Manipulating bitmaps
      1. What is a bitmap exactly?
      2. The Matrix class
    8. Bitmap manipulation demo app
      1. Adding the graphic to the project
    9. Frequently asked question
    10. Summary
  26. Chapter 21: Threads and Starting the Live Drawing App
    1. Technical requirements
    2. Creating the Live Drawing project
      1. Refactoring MainActivity to LiveDrawingActivity
      2. Locking the game to fullscreen and landscape orientation
      3. Looking ahead at the Live Drawing app
      4. Coding the LiveDrawingActivity class
      5. Coding the LiveDrawingView class
      6. Coding the LiveDrawingView class
    3. The game loop
    4. Threads
      1. Problems with threads
    5. Implementing the game loop with a thread
      1. Implementing Runnable and providing the run method
      2. Coding the thread
      3. Starting and stopping the thread
      4. Using the Activity lifecycle to start and stop the thread
      5. Coding the run method
    6. Running the app
    7. Summary
  27. Chapter 22: Particle Systems and Handling Screen Touches
    1. Technical requirements
    2. Adding custom buttons to the screen
    3. Implementing a particle system effect
      1. Coding the Particle class
      2. Coding the ParticleSystem class
      3. Spawning particle systems in the LiveDrawingView class
    4. Handling touches
      1. Coding the onTouchEvent method
      2. Finishing the HUD
    5. Running the app
    6. The Android Studio Profiler tool
    7. Summary
  28. Chapter 23: Supporting Different Versions of Android, Sound Effects, and Spinner Widget
    1. Handling different versions of Android
      1. Detecting the current Android version
    2. The SoundPool class
      1. Initializing SoundPool the new way
      2. Initializing SoundPool the old way
    3. Sound demo app introducing the Spinner widget
      1. Making sound effects
      2. Laying out the sound demo
      3. Coding the sound demo
    4. Summary
  29. Chapter 24: Design Patterns, Multiple Layouts, and Fragments
    1. Technical requirements
    2. Introducing the model-view-controller pattern
    3. Android design guidelines
    4. Real-world apps
    5. Device detection mini-app
      1. Coding the MainActivity class
      2. Running the app
    6. Configuration qualifiers
      1. The limitation of configuration qualifiers
    7. Fragments
      1. Fragments have a lifecycle too
      2. Managing Fragments with FragmentManager
    8. Our first Fragment app
    9. Fragment reality check
    10. Frequently asked question
    11. Summary
  30. Chapter 25: Building a Simple Image Gallery App
    1. Technical requirements
    2. Angry Birds classic swipe menu
    3. Building an image gallery/slider app
      1. Implementing the layout
      2. Coding the PagerAdapter class
      3. Coding the MainActivity class
      4. Running the gallery app
    4. Summary
  31. Chapter 26: Advanced UI with Navigation Drawer and Fragment
    1. Technical requirements
    2. Introducing NavigationView
    3. Examining the Simple Database app
      1. Insert
      2. Delete
      3. Search
      4. Results
    4. Starting the Simple Database project
    5. Exploring the autogenerated code and assets
    6. Coding the Fragment classes and their layouts
      1. Creating the empty files for the classes and layouts
      2. Coding the classes
      3. Designing the layouts
      4. Designing content_insert.xml
      5. Designing content_delete.xml
      6. Designing content_search.xml
      7. Designing content_results.xml
    7. Using the Fragment classes and their layouts
      1. Editing the Navigation Drawer menu
      2. Adding a holder to the main layout
      3. Coding the class
    8. Summary
  32. Chapter 27: Android Databases
    1. Technical requirements
    2. Databases 101
      1. What is a database?
      2. What is SQL?
      3. What is SQLite?
    3. SQL syntax primer
      1. SQLite example code
    4. Android SQLite API
      1. SQLiteOpenHelper and SQLiteDatabase
      2. Building and executing queries
      3. Database cursors
    5. Coding the database class
    6. Coding the Fragment classes to use the DataManager class
    7. Running the Age Database app
    8. Summary
  33. Chapter 28: A Quick Chat before You Go
    1. Publishing
    2. Make an app!
    3. Keep learning
      1. Keep reading
      2. GitHub
      3. Stack Overflow
      4. Android user forums
      5. Higher-level study
    4. My other channels
    5. Goodbye and thank you
    6. Why subscribe?
  34. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Leave a review - let other readers know what you think

Product information

  • Title: Android Programming for Beginners - Third Edition
  • Author(s): John Horton
  • Release date: April 2021
  • Publisher(s): Packt Publishing
  • ISBN: 9781800563438