Book description
What will you learn from this book?
If you have an idea for a killer Android app, this book will help you build your first working application in a jiffy. You’ll learn hands-on how to structure your app, design interfaces, create a database, make your app work on various smartphones and tablets, and much more. It’s like having an experienced Android developer sitting right next to you! All you need is some Java know-how to get started.
Why does this book look so different?
Based on the latest research in cognitive science and learning theory, Head First Android Development uses a visually rich format to engage your mind, rather than a text-heavy approach that puts you to sleep. Why waste your time struggling with new concepts? This multi-sensory learning experience is designed for the way your brain really works.
Publisher resources
Table of contents
- Dedication
- Authors of Head First Android Development
- How to Use This Book: Intro
-
1. Getting Started: Diving In
- Welcome to Androidville
- The Android platform dissected
- Here’s what we’re going to do
- Your development environment
- Install Java
- Build a basic app
- Let’s build the basic app
- Activities and layouts from 50,000 feet
- Building a basic app (continued)
- Building a basic app (continued)
- You’ve just created your first Android app
- Android Studio creates a complete folder structure for you
- Useful files in your project
- Edit code with the Android Studio editors
- Run the app in the Android emulator
- Creating an Android Virtual Device
- Run the app in the emulator
- You can watch progress in the console
- Test drive
- What just happened?
- Refining the app
- What’s in the layout?
- activity_main.xml has two elements
- The layout file contains a reference to a string, not the string itself
- Let’s look in the strings.xml file
- Take the app for a test drive
- Your Android Toolbox
-
2. Building Interactive Apps: Apps That Do Something
- You’re going to build a Beer Adviser app
- Here’s what you need to do
- Create the project
- We’ve created a default activity and layout
- Adding components with the design editor
- activity_find_beer.xml has a new button
- A closer look at the layout code
- Changes to the XML...
- ...are reflected in the design editor
- Use string resources rather than hardcoding the text
- Change the layout to use the string resources
- Let’s take the app for a test drive
- Add values to the spinner
- Get the spinner to reference a string-array
- Test drive the spinner
- We need to make the button do something
- Make the button call a method
- What activity code looks like
- Add an onClickFindBeer() method to the activity
- onClickFindBeer() needs to do something
- Once you have a View, you can access its methods
- Update the activity code
- The first version of the activity
- What the code does
- Test drive the changes
- Building the custom Java class
- Enhance the activity to call the custom Java class so that we can get REAL advice
- Activity code version 2
- What happens when you run the code
- Test drive your app
- Your Android Toolbox
-
3. Multiple Activities and Intents: State Your Intent
- Apps can contain more than one activity
- Here’s the app structure
- Create the project
- Update the layout
- Update strings.xml...
- Create the second activity and layout
- What just happened?
- Welcome to the Android manifest file
- Every activity needs to be declared
- An intent is a type of message
- Use an intent to start the second activity
- What happens when you run the app
- The story continues...
- Test drive the app
- Pass text to a second activity
- Update the text view properties
- putExtra() puts extra information in an intent
- Update the CreateMessageActivity code
- Get ReceiveMessageActivity to use the information in the intent
- What happens when the user clicks the Send Message button
- Test drive the app
- We can change the app to send messages to other people
- How Android apps work
- But we don’t know what apps are on the device
- Create an intent that specifies an action
- Change the intent to use an action
- What happens when the code runs
- The story continues...
- The intent filter tells Android which activities can handle which actions
- How Android uses the intent filter
- You need to run your app on a REAL device
- Test drive the app
- What if you ALWAYS want your users to choose an activity?
- What happens when you call createChooser()
- The story continues...
- Change the code to create a chooser
- Test drive the app
- If you have NO matching activities
- Your Android Toolbox
-
4. The Activity Lifecycle: Being an Activity
- How do activities really work?
- The Stopwatch app
- The stopwatch layout code
- How the activity code will work
- Add code for the buttons
- The runTimer() method
- Handlers allow you to schedule code
- The full runTimer() code
- The full StopwatchActivity code
- What happens when you run the app
- The story continues
- Test drive the app
- What just happened?
- Rotating the screen changes the device configuration
- From birth to death: the states of an activity
- The activity lifecycle: from create to destroy
- Your activity inherits the lifecycle methods
- How do we deal with configuration changes?
- Or save the current state...
- ...then restore the state in onCreate()
- What happens when you run the app
- The story continues
- Test drive the app
- There’s more to an activity’s life than create and destroy
- The activity lifecycle: the visible lifetime
- We need to implement two more lifecycle methods
- The updated StopwatchActivity code
- What happens when you run the app
- Test drive the app
- But what if an app is only partially visible?
- The activity lifecycle: the foreground lifetime
- Stop the stopwatch if the activity’s paused
- What happens when you run the app
- Test drive the app
- The complete activity code
- Your handy guide to the lifecycle methods
- Your Android Toolbox
-
5. The User Interface: Enjoy the View
- Your user interface is made up of layouts and GUI components
- Three key layouts: relative, linear, and grid
- RelativeLayout displays views in relative positions
- Adding padding
- Positioning views relative to the parent layout
- Attributes for positioning views relative to the parent layout
- Positioning views relative to other views
- Attributes for positioning views relative to other views
- Use margins to add distance between views
- RelativeLayout: a summary
- LinearLayout displays views in a single row or column
- A linear layout displays views in the order they appear in the layout XML
- Let’s change up a basic linear layout
- Here’s the starting point for the linear layout
- Make a view streeeeetch by adding weight
- Adding weight to one view
- Adding weight to multiple views
- Use gravity to specify where text appears in a view
- Test drive
- Using the android:gravity attribute: a list of values
- Move the button to the right with layout-gravity
- More values you can use with the android:layout-gravity attribute
- The full linear layout code
- LinearLayout: a summary
- GridLayout displays views in a grid
- Adding views to the grid layout
- Let’s create a new grid layout
- We’ll start with a sketch
- Row 0: add views to specific rows and columns
- Row 1: make a view span multiple columns
- Row 2: make a view span multiple columns
- The full code for the grid layout
- GridLayout: a summary
- Layouts and GUI components have a lot in common
- GUI components are a type of View
- What being a view buys you
- A layout is really a hierarchy of Views
- Playing with views
- Text view
- Edit Text
- Button
- Toggle button
- Switch
- Check boxes
- Radio buttons
- Spinner
- Image views
- Images: the layout XML
- Adding images to buttons
- Image Button
- Scroll views
- Toasts
- Your Android Toolbox
-
6. List Views and Adapters: Getting Organized
- Every app starts with ideas
- Categorize your ideas: top-level, category, and detail/edit activities
- Navigating through the activities
- Use ListViews to navigate to data
- We’re going to build part of the Starbuzz app
- The drink detail activity
- The Starbuzz app structure
- Here are the steps
- The Drink class
- The image files
- The top-level layout contains an image and a list
- Use a list view to display the list of options
- The full top-level layout code
- Test drive
- Get ListViews to respond to clicks with a Listener
- Set the listener to the list view
- The full TopLevelActivity code
- Where we’ve got to
- A category activity displays the data for a single category
- A ListActivity is an activity that contains only a list
- How to create a list activity
- android:entries works for static array data held in strings.xml
- Connect list views to arrays with an array adapter
- Add the array adapter to DrinkCategoryActivity
- What happens when you run the code
- Test drive the app
- App review: where we’ve got to
- How we handled clicks in TopLevelActivity
- Pass data to an activity using the ListActivity onListItemClick() method
- The full DrinkCategoryActivity code
- A detail activity displays data for a single record
- Retrieve data from the intent
- Update the views with the data
- The DrinkActivity code
- What happens when you run the app
- The story continues
- Test drive the app
- Your Android Toolbox
-
7. Fragments: Make it Modular
- Your app needs to look great on all devices
- Your app may need to behave differently too
- Fragments allow you to reuse code
- The Workout app structure
- Here are the steps
- The Workout class
- How to add a fragment to your project
- Fragment layout code looks just like activity layout code
- What fragment code looks like
- Adding a fragment to an activity’s layout
- Passing the workout ID to the fragment
- Get the activity to set the workout ID
- Activity states revisited
- The fragment lifecycle
- Your fragment inherits the lifecycle methods
- Set the view’s values in the fragment’s onStart() method
- Test drive the app
- Where we’ve got to
- We need to create a fragment with a list
- A ListFragment is a fragment that contains only a list
- How to create a list fragment
- We’ll use an ArrayAdapter to set the values in the ListView
- The updated WorkoutListFragment code
- Display WorkoutListFragment in the MainActivity layout
- Test drive the app
- Wiring up the list to the detail
- We need to decouple the fragment with an interface
- First, add the interface to the list fragment
- Then make the activity implement the interface
- You want fragments to work with the back button
- Don’t update—instead, replace
- Using fragment transactions
- The updated MainActivity code
- Test drive the app
- Rotating the device breaks the app
- The WorkoutDetailFragment code
- Phone versus tablet
- The phone and tablet app structures
- Put screen-specific resources in screen-specific folders
- The different folder options
- Tablets use layouts in the layout-large folder
- The MainActivity phone layout
- Phones will use DetailActivity to display details of the workout
- The full DetailActivity code
- Use layout differences to tell which layout the device is using
- The revised MainActivity code
- Test drive the app
- Your Android Toolbox
-
8. Nested Fragments: Dealing with Children
- Creating nested fragments
- Fragments and activities have similar lifecycles...
- The StopwatchFragment code
- The StopwatchFragment layout
- Adding the stopwatch fragment to WorkoutDetailFragment
- Add a FrameLayout where the fragment should appear
- Then display the fragment in Java code
- getFragmentManager() creates transactions at the activity lavel
- Nested fragments need nested transactions
- Display the fragment in its parent’s onCreate() method
- The full WorkoutDetailFragment code
- Test drive the app
- Why does the app crash if you press a button?
- Let’s look at the StopwatchFragment layout code
- The onClick attribute calls methods in the activity, not the fragment
- First, remove the onClick attributes from the fragment’s layout
- Make the fragment implement OnClickListener
- Attach the OnClickListener to the buttons
- The StopwatchFragment code
- Test drive the app
- Rotating the device re-creates the activity
- onCreate() REPLACES the fragment
- The WorkoutDetailFragment code
- Test drive the app
- Your Android Toolbox
-
9. Action Bars: Taking Shortcuts
- Great apps have a clear structure
- Different types of navigation
- Let’s start with the action bar
- The Android support libraries
- Your project may include support libraries
- We’ll get the app to use up to date themes
- Apply a theme in AndroidManifest.xml
- Define styles in style resource files
- Set the default theme in styles.xml
- What happens when you run the app
- Test drive the app
- Adding action items to the action bar
- The menu resource file
- The menu showAsAction attribute
- Add a new action item
- Inflate the menu in the activity with the onCreateOptionsMenu() method
- React to action item clicks with the onOptionsItemSelected() method
- Create OrderActivity
- Start OrderActivity with the Create Order action item
- The full MainActivity.java code
- Test drive the app
- Sharing content on the action bar
- Add a share action provider to menu_main.xml
- Specify the content with an intent
- The full MainActivity.java code
- Test drive the app
- Enabling Up navigation
- Setting an activity’s parent
- Adding the Up button
- Test drive the app
- Your Android Toolbox
-
10. Navigation Drawers: Going Places
- The Pizza app revisited
- Navigation drawers deconstructed
- The Pizza app structure
- Create TopFragment
- Create PizzaFragment
- Create PastaFragment
- Create StoresFragment
- Add the DrawerLayout
- The full code for activity_main.xml
- Initialize the drawer’s list
- Use an OnItemClickListener to respond to clicks in the list view
- The selectItem() method so far
- Changing the action bar title
- Closing the navigation drawer
- The updated MainActivity.java code
- Get the drawer to open and close
- Using an ActionBarDrawerToggle
- Modifying action bar items at runtime
- The updated MainActivity.java code
- Enable the drawer to open and close
- Syncing the ActionBarDrawerToggle state
- The updated MainActivity.java code
- Test drive the app
- The title and fragment are getting out of sync
- Dealing with configuration changes
- Reacting to changes on the back stack
- Adding tags to fragments
- Find the fragment using its tag
- The full MainActivity.java code
- Test drive the app
- Your Android Toolbox
-
11. SQLite Databases: Fire Up the Database
- Back to Starbuzz
- Android uses SQLite databases to persist data
- Android comes with SQLite classes
- The current Starbuzz app structure
- We’ll change the app to use a database
- The SQLite helper manages your database
- Create the SQLite helper
- 1. Specify the database
- Inside a SQLite database
- You create tables using Structured Query Language (SQL)
- Insert data using the insert() method
- Update records with the update() method
- Multiple conditions
- The StarbuzzDatabaseHelper code
- What the SQLite helper code does
- What if you need to change the database?
- SQLite databases have a version number
- Upgrading the database: an overview
- The story continues....
- How the SQLite helper makes decisions
- Upgrade your database with onUpgrade()
- Downgrade your database with onDowngrade()
- Let’s upgrade the database
- Upgrading an existing database
- Renaming tables
- The full SQLite helper code
- The SQLite helper code (continued)
- What happens when the code runs
- Your Android Toolbox
-
12. Cursors and Asynctasks: Connecting to Databases
- The story so far...
- We’ll change the app to use the database
- The current DrinkActivity code
- Get data from the database with a cursor
- A query lets you say what records you want from the database
- The SQLiteDatabase query() method lets you build SQL using a query builder
- Specifying table and columns
- Applying multiple conditions to your query
- Order data in your query
- Using SQL functions in queries
- SQL GROUP BY and HAVING clauses
- Get a reference to the database
- getReadableDatabase() versus getWritableDatabase()
- getReadableDatabase()
- getWritableDatabase()
- The code for getting a cursor
- To read a record from a cursor, you first need to navigate to it
- Navigating cursors
- Getting cursor values
- The DrinkActivity code
- What we’ve done so far
- The current DrinkCategoryActivity code
- How do we replace the array data in the ListView?
- A CursorAdapter reads just enough data
- The story continues
- A SimpleCursorAdapter maps data to views
- Creating the SimpleCursorAdapter
- Closing the cursor and database
- The revised code for DrinkCategoryActivity
- Test drive the app
- Put important information in the top-level activity
- Add favorites to DrinkActivity
- Add a new column to the cursor
- Respond to clicks to update the database
- The DrinkActivity code
- Display favorites in TopLevelActivity
- Display the favorite drinks in activity_top_level.xml
- What changes are needed for TopLevelActivity.java
- The new top-level activity code
- Test drive the app
- Cursors don’t automatically refresh
- Change the cursor with changeCursor()
- The revised TopLevelActivity.java code
- Test drive the app
- Databases can make your app go in sloooo-moooo....
- What code goes on which thread?
- AsyncTask performs asynchronous tasks
- The onPreExecute() method
- The doInBackground() method
- The onProgressUpdate() method
- The onPostExecute() method
- The AsyncTask class
- Execute the AsyncTask
- The DrinkActivity.java code
- A summary of the AsyncTask steps
- Your Android Toolbox
-
13. Services: At Your Service
- Services work behind the scenes
- The started service app
- We’re going to create an IntentService
- The IntentService from 50,000 feet
- How to log messages
- The full DelayedMessageService code
- You declare services in AndroidManifest.xml
- Add a button to activity_main.xml
- You start a service using startService()
- Test drive the app
- We want to send a message to the screen
- onStartCommand() runs on the main thread
- The full DelayedMessageService.java code
- The application context
- Test drive the app
- Can we improve on using Toasts?
- How you use the notification service
- You create notifications using a notification builder
- Getting your notification to start an activity
- Send the notification using the notification service
- The full code for DelayedMessageService.java
- What happens when you run the code
- The story continues
- Test drive the app
- Bound services are more interactive
- The steps needed to create the OdometerService
- Create a new Odometer project
- How binding works
- Define the Binder
- Get the service to do something
- The Service class has four key methods
- Location, location, location...
- Add the LocationListener to the service
- Registering the LocationListener
- Stop location updates when the service is destroyed
- Tell the activity the distance traveled
- The full OdometerService.java code
- Update AndroidManifest.xml
- Where we’ve got to
- Update MainActivity’s layout
- Create a ServiceConnection
- Bind to the service when the activity starts
- Display the distance traveled
- The full MainActivity.java code
- What happens when you run the code
- The story continues
- Test drive the app
- Your Android Toolbox
-
14. Material Design: Living in a Material World
- Welcome to Material Design
- CardViews and RecyclerViews
- The Pizza app structure
- Add the pizza data
- Add the support libraries
- Create the CardView
- The full card_captioned_image.xml code
- RecyclerViews use RecyclerView.Adapters
- Create the basic adapter
- Define the adapter’s ViewHolder
- Create the ViewHolders
- Each card view displays an image and a caption
- Add the data to the card views
- The full code for CaptionedImagesAdapter.java
- Create the recycler view
- Add the RecyclerView to the layout
- The PizzaMaterialFragment.java code
- A RecyclerView uses a layout manager to arrange its views
- Specifying the layout manager
- The full PizzaMaterialFragment.java code
- Get MainActivity to use the new PizzaMaterialFragment
- What happens when the code runs
- The story continues
- Test drive the app
- Where we’ve got to
- Create PizzaDetailActivity
- What PizzaDetailActivity.java needs to do
- The code for PizzaDetailActivity.java
- Getting a RecyclerView to respond to clicks
- You can listen to views from the adapter
- Keep your adapters reusable
- Add the interface to the adapter
- Implement the listener in PizzaMaterialFragment.java
- Test drive the app
- Bring the content forward
- The full code for fragment_top.xml
- The full code for TopFragment.java
- Test drive the app
- Your Android Toolbox
- I. Leaving town...
- E. O’reilly®: Android Development
- Index
- About the Authors
- Copyright
Product information
- Title: Head First Android Development
- Author(s):
- Release date: June 2015
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9781449362188
You might also like
book
Head First Android Development, 2nd Edition
What will you learn from this book? If you have an idea for a killer Android …
book
Head First Android Development, 3rd Edition
What will you learn from this book? If you have an idea for a killer Android …
book
Android Programming with Kotlin for Beginners
Build smart looking Kotlin apps with UI and functionality for the Android platform Key Features Start …
book
Android Programming for Beginners - Third Edition
Learn the Java and Android skills you need to start developing powerful mobile applications with the …