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

Learn all the Java and Android skills you need to start making powerful mobile applications with practical and actionabl

5,717 1,392 11MB

English Pages 766 [767] Year 2018

Report DMCA / Copyright

DOWNLOAD FILE

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

Table of contents :
Cover
Copyright
Packt Upsell
Contributors
Table of Contents
Preface
Chapter 1: Beginning Android and Java
What's new in the second edition?
Why Java and Android?
The beginner's first stumbling block
How Java and Android work together
The Android API
Java is object-oriented
Run that by me again – What exactly is Android?
Android Studio
Setting up Android Studio
Final step – for now
What makes an Android app?
Android resources
The structure of Android's Java code
Packages
Classes
Methods
Our first Android app
Extra step 1
Extra step 2
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
Chapter 2: First Contact – Java, XML, and the UI Designer
Examining the log output
Filtering the logcat output
A note for early adopters of this book
Exploring the project's Java code and the main layout's XML code
Examining the HelloWorldActivity.java file
Code folding (hiding) in Android Studio
The package declaration
Importing classes
The class
Methods inside the class
Summary of the Java code so far
Examining the main layout file
UI layout elements
UI text elements
Adding buttons to the main layout file
Adding a button via the visual designer
Editing the button's attributes
Examining the XML code for the new button
Adding a button by editing the XML code
Giving the buttons unique id attributes
Positioning the two buttons in the layout
Making the buttons call different methods
Leaving comments in our Java code
Coding messages to the user and the developer
Writing our first Java code
Adding message code to the onCreate method
Examining the output
Writing our own Java methods
Examining the output
Frequently asked questions
Summary
Chapter 3: Exploring Android Studio and the Project Structure
A quick guided tour of Android Studio
Project Explorer and project anatomy
The Empty Activity project
Exploring the Empty Activity project
The manifests folder
The java folder
The res folder
The res/drawable folder
The res/layout folder
The res/mipmap
res/values
The Basic Activity project
Exploring the Basic Activity project
The MainActivity.java file
The activity_main.xml file
The extra methods in MainActivity.java
The content_main.xml file
Exploring the Android emulator
Emulator control panel
Using the emulator as a real device
Accessing the app drawer
Viewing active apps and switching between apps
Summary
Chapter 4: Getting Started with Layouts and Material Design
Material design
Exploring Android UI design
Layouts
Creating the Exploring Layouts project
Building a menu with LinearLayout
Adding a LinearLayout to the project
Preparing your workspace
Examining the generated XML
Adding a TextView to the UI
Adding a multi-line 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 the Java code (part 2)
Building a precise UI with ConstraintLayout
Adding a CalenderView
Resizing a view in a ConstraintLayout
Using the Component Tree window
Adding constraints manually
Adding and constraining more UI elements
Making the text clickable
Laying out data with TableLayout
Adding a TableRow to TableLayout
Using the Component Tree when the visual designer won't do
Organizing the table columns
Linking back to the main menu
Summary
Chapter 5: Beautiful Layouts with CardView and ScrollView
Attributes 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 ScrollView
Setting the view with Java code
Adding image resources
Creating the content for the cards
Defining dimensions for CardViews
Adding CardViews to our layout
Including layout files inside another layout
Themes and material design
Using the Android Studio theme designer
Creating a tablet emulator
Frequently asked questions
Summary
Chapter 6: The Android Lifecycle
The life and times of an Android app
How Android interacts with our apps
A simplified explanation of the Android lifecycle
The lifecycle phases demystified
How we handle the lifecycle phases
Lifecycle demo app
Coding the lifecycle demo app
Running the lifecycle demo app
Examining the Lifecycle Demo app output
Some other overridden methods
The structure of Java code – revisited
Summary
Chapter 7: Java Variables, Operators, and Expressions
Java is everywhere
Syntax and jargon
More code comments
Storing and using data with variables
Types of variables
Primitive types
Reference types
Using variables
Variable declaration
Variable initialization
Changing values in variables with operators
The assignment operator
The addition operator
The subtraction operator
The division operator
The multiplication operator
The increment operator
The decrement operator
Expressions
Expressing yourself demo app
Summary
Chapter 8: Java Decisions and Loops
Making decisions in Java
Indenting code for clarity
More operators
The comparison operator
The logical NOT operator
The NOT equal operator
The greater than operator
The less than operator
The greater than or equal to operator
The less than or equal to operator
The logical AND operator
The logical OR operator
How to use all of these operators to test variables
Using the Java if keyword
Switching to make decisions
Switch Demo app
Repeating code with loops
While loops
Breaking out of a loop
The continue keyword
Do while loops
For loops
Loops demo app
Summary
Chapter 9: Java Methods
Methods revisited
What exactly are Java methods?
Method structure
Modifier
Return type
Name of a method
Parameters
The body
Using methods demo apps
Real world methods
Discovering variable scope
Exploring method overloading
Scope and variables revisited
Frequently asked questions
Further reading
Summary
Chapter 10: Object-Oriented programming
Important memory management warning
Object-oriented programming
What is OOP exactly
Encapsulation
Polymorphism
Inheritance
Why do it like this?
Class recap
Looking at the code for a class
Class implementation
Declaring, initializing, and using an object of the class
Basic classes app
More things that we can do with our first class
Frequently asked questions
Chapter 11: More Object-Oriented Programming
Remember that encapsulation thing?
Controlling class use with access modifiers
Class access modifiers
Class access in summary
Controlling variable use with access modifiers
Variable access modifiers
Variable access summary
Methods have access modifiers too
Method access modifiers
Method access summary
Accessing private variables with getters and setters
Setting up our objects with constructors
Static methods
Encapsulation and static methods mini-app
OOP and inheritance
Inheritance example app
Polymorphism
Abstract classes
Interfaces
Frequently asked questions
Summary
Chapter 12: The Stack, the Heap, and the Garbage Collector
All the Android UI elements are classes too
Re-introducing references
A quick break to throw out the trash
Seven facts about the Stack and the Heap
So how does this Heap thing help me?
Using Buttons and TextView widgets from our layout
The Inner and Anonymous classes
Frequently asked questions
Summary
Chapter 13: Anonymous Classes – Bringing Android Widgets to Life
Declaring and initializing the objects
Creating UI widgets from pure Java without XML
Exploring the palette – part 1
The EditText widget
The ImageView widget
Radio button and group
Anonymous classes
Exploring the palette – Part 2, and more anonymous classes
Switch
CheckBox
TextClock
Widget exploration app
Setting up the widget exploration project and UI
Coding the widget exploration app
Getting a reference to the parts of the UI
Coding the checkboxes
Coding the RadioButtons
Using an anonymous class for a regular button
Coding the Switch
Running the Widget Exploration app
Converting layouts to ConstraintLayout
Summary
Chapter 14: Android Dialog Windows
Dialog windows
Creating the Dialog Demo project
Coding a DialogFragment class
Using chaining to configure the DialogFragment
Using the DialogFragment class
The Note to self app
Using naming conventions and String resources
How to get the code files for the Note to self app
The completed app
Building the project
Preparing the String resources
Coding the Note class
Implementing the Dialog designs
Coding the dialog boxes
Coding the DialogNewNote class
Coding the DialogShowNote class
Showing our new dialogs
Coding the floating action button
Summary
Chapter 15: Arrays, ArrayList, Map and Random Numbers
A random diversion
Handling large amounts of data with arrays
Arrays are objects
Simple array example mini-app
Getting dynamic with arrays
Dynamic array example
Entering the nth dimension with Arrays
Multidimensional Array mini app
Array out of bounds exceptions
ArrayLists
The enhanced for loop
Arrays and ArrayLists are polymorphic
More Java Collections – Meet Java Hashmap
The Note to Self app
Frequently asked questions
Summary
Chapter 16: Adapters and Recyclers
RecyclerView and RecyclerAdapter
The problem with displaying lots of widgets
The solution to the problem with displaying lots of widgets
How to use RecyclerView and RecyclerAdapter
What we will do to set up RecyclerView with RecyclerAdapter and an ArrayList of notes
Adding RecyclerView, RecyclerAdapter, and ArrayList to the Note to Self project
Removing the temporary Show Note button and adding the RecyclerView
Creating a list item for the RecyclerView
Coding the RecyclerAdapter class
Coding the NoteAdapter constructor
Coding the onCreateViewHolder method
Coding the onBindViewHolder method
Coding getItemCount
Coding the ListItemHolder inner class
Coding MainActivity to use the RecyclerView and RecyclerAdapter classes
Adding code to onCreate
Modifying the addNote method
Coding the showNote method
Running the app
Frequently asked questions
Summary
Chapter 17: Data Persistence and Sharing
Android Intents
Switching Activity
Passing data between Activities
Adding a settings page to Note to Self
Creating the SettingsActivity
Designing the Settings screen layout
Enabling the user to switch to the settings screen
Persisting data with SharedPreferences
Reloading data with SharedPreferences
Making the Note to Self settings persist
Coding the SettingsActivity class
Coding the MainActivity class
More advanced persistence
What is JSON?
Java exceptions – try, catch, and finally
Backing up user data in Note to Self
Frequently asked questions
Summary
Chapter 18: Localization
Making the Note to Self app for Spanish, English, and German speakers
Adding Spanish support
Adding German support
Adding the string resources
Running Note to Self in German or Spanish
Making the translations work in Java code
Summary
Chapter 19: Animations and Interpolations
Animations in Android
Designing cool animations in XML
Fading in and out
Move it, move it
Scaling or stretching
Controlling the duration
Rotate animations
Repeating animations
Combining animation's properties with Set
Instantiating animations and controlling them with Java code
More animation features
Listeners
Animation interpolators
Animations Demo App – introducing SeekBar
Laying out the animation demo
Coding the XML Animations
Wiring up the Animation Demo app in Java
Frequently asked questions
Summary
Chapter 20: Drawing Graphics
Understanding the Canvas class
Getting started drawing with Bitmap, Canvas, and ImageView
Canvas and Bitmap
Paint
ImageView and Activity
Canvas, Bitmap, Paint, and ImageView – quick summary
Using the Canvas class
Preparing the instances of the required classes
Initializing the objects
Setting the Activity content
The Canvas Demo app
Creating a new project
Coding the Canvas demo app
Drawing on the screen
Android coordinate system
Plotting and drawing
Creating Bitmaps
Manipulating Bitmaps
What is a Bitmap exactly?
The Matrix class
Inverting a bitmap to face the opposite direction
Rotating the bitmap to face up and down
Bitmap manipulation demo app
Add the graphic to the project
Frequently asked question
Summary
Chapter 21: Threads, and Starting the Live Drawing App
Creating the Live Drawing project
Looking ahead at the Live Drawing app
Coding the LiveDrawingActivity class
Coding the LiveDrawingView class
Coding the LiveDrawingView class
Adding the member variables
Coding the LiveDrawingView constructor
Coding the draw method
Adding the printDebuggingText method
Understanding the draw method and the SurfaceView class
The game loop
Threads
Problems with threads
Implementing the game loop with a thread
Implementing Runnable and providing the run method
Coding the thread
Starting and stopping the thread
Using the Activity lifecycle to start and stop the thread
Coding the run method
Running the app
Summary
Chapter 22: Particle Systems and Handling Screen Touches
Adding custom buttons to the screen
Implementing a particle system effect
Coding the Particle class
Coding the ParticleSystem class
Spawning particle systems in the LiveDrawingView class
Handling touches
Coding the onTouchEvent method
Finishing the HUD
Running the app
Summary
Chapter 23: Supporting Different Versions of Android, Sound Effects, and the Spinner Widget
Handling different versions of Android
Detecting the current Android version
The Soundpool class
Initializing SoundPool the new way
Initializing SoundPool the old way
Loading sound files into memory
Playing a sound
Stopping a sound
Sound demo app introducing Spinner widget
Making sound FX
Laying out the sound demo
Coding the Sound demo
Summary
Chapter 24: Design Patterns, Multiple Layouts, and Fragments
Introducing the model-view-controller pattern
Model
View
Controller
Android design guidelines
Real-world apps
Device detection mini-app
Coding the MainActivity class
Unlocking the screen orientation
Running the app
Configuration qualifiers
The limitation of configuration qualifiers
Fragments
Fragments have a lifecycle too
onCreate
onCreateView
onAttach and onDetach
onStart, onPause, and onStop
Managing Fragments with FragmentManager
Our first Fragment app
Fragment reality check
Frequently asked question
Summary
Chapter 25: Advanced UI with Paging and Swiping
Angry birds classic swipe menu
Building an image gallery/slider app
Implementing the layout
Coding the PagerAdapter class
Coding the MainActivity class
Running the gallery app
Building a Fragment Pager/slider app
Coding the SimpleFragment class
The fragment_layout
Coding the MainActivity class
The activity_main layout
Running the fragment slider app
Summary
Chapter 26: Advanced UI with Navigation Drawer and Fragment
Introducing the NavigationView
Examining the Simple Database app
Insert
Delete
Search
Results
Starting the Simple Database project
Exploring the auto-generated code and assets
Coding the Fragment classes and their layouts
Creating the empty files for the classes and layouts
Coding the classes
Designing the layouts
Designing content_insert.xml
Designing content_delete.xml
Designing content_search.xml
Designing content_results.xml
Using the Fragment classes and their layouts
Editing the Navigation Drawer menu
Adding a holder to the main layout
Coding the MainActivity.java
Summary
Chapter 27: Android Databases
Database 101
What is a database
What is SQL
What is SQLite
SQL syntax primer
SQLite example code
Creating a table
Inserting data into the database
Retrieving data from the database
Updating the database structure
Android SQLite API
SQLiteOpenHelper and SQLiteDatabase
Building and executing queries
Database cursors
Coding the database class
Coding the Fragment classes to use the DataManager
Running the Age Database app
Summary
Chapter 28: Coding a Snake Game Using Everything We Have Learned So Far
How to play
Getting started with the Snake game
Make the app full screen and landscape
Adding some empty classes
Coding SnakeActivity
Adding the sound effects
Coding the game engine
Coding the members
Coding the constructor
Coding the newGame method
Coding the run method
Coding the updateRequired method
Coding the update method
Coding the draw method
Coding onTouchEvent
Coding pause and resume
Running the game
Adding the graphics
Coding the apple
The Apple constructor
Using the apple
Running the game
Summary
Chapter 29: Enumerations and Finishing the Snake Game
Enumerations
Add the sound to the project
Coding the Snake class
Coding the constructor
Coding the reset method
Coding the move method
Coding the detectDeath method
Coding the checkDinner method
Coding the draw method
Coding the switchHeading method
Using the snake class and finishing the game
Running the completed game
Summary
Chapter 30: A Quick Chat Before You Go
Publishing
Making an app!
Carrying on learning
Carrying on reading
GitHub
StackOverflow
Android user forums
Higher-level study
My other channels
Goodbye and thank you
Other Books You May Enjoy
Index

Polecaj historie