Home Page Icon
Home Page
Table of Contents for
Cover
Close
Cover
by Dawn Griffiths, David Griffiths
Head First Android Development, 3rd Edition
1. Getting Started: Diving In
Welcome to Androidville
Layouts define what each screen looks like
Activities define what the app does
Extra resources may be needed too
Activities and layouts form the backbone of your app
Here’s what we’re going to do
Android Studio: your development environment
You need to install Android Studio
Install Android Studio
Build a basic app
How to build the app
1. Create a new project
2. Choose your project
3. Configure your project
You’ve created your first Android project
Dissecting your new project
The folder structure includes different types of files
Introducing the key files in your project
Edit code with the Android Studio editors
The code editor
The design editor
The story so far...
How to run the app on a physical device
1. Enable USB debugging on your device
2. Set up your computer to detect the device
3. Use a USB cable to plug your device into your computer
4. Run your app
How to run the app on a virtual device
Create an Android Virtual Device (AVD)
Open the Android Virtual Device Manager
Select the hardware
Select a system image
Verify the AVD configuration
The virtual device gets created
Run the app on the AVD
Compile, package, deploy, run
Test drive
What just happened?
Refine the app
The app has one activity and one layout
The activity controls what the app does
The layout controls the app’s appearance
What’s in the layout?
The design editor
The code editor
activity_main.xml has two elements
Update the text displayed in the layout
What the code does
Test drive
Your Android Toolbox
2. Building Interactive Apps: Apps That Do Something
Let’s build a Beer Adviser app
Here’s what we’re going to do
Create the project
We’ve created a default activity and layout
A closer look at the design editor
Add a button using the design editor
Changes in the design editor are reflected in the XML
activity_main.xml has a new button
Buttons and text views are subclasses of the same Android View class
android:id
android:layout_width, android:layout_height
android:text
A closer look at the layout code
The <LinearLayout> element
The Button element
The TextView element
Let’s update the layout XML
The XML changes are reflected in the design editor
Test drive
There are warnings in the layout...
...because there’s hardcoded text
Put text in a String resource file
Android Studio helps you extract String resources
Extract the String resource
A String resource has been added to strings.xml
activity_main.xml uses the String resource
You also can extract String resources manually
Add and use a new String resource
Test drive
Add values to the spinner
Adding an array resource is similar to adding a String
Add the string-array to strings.xml
Get the spinner to display the array’s values
The full code for activity_main.xml
Test drive
We need to make the app interactive
What our MainActivity code looks like
A button can listen for on-click events...
...using an OnClickListener
Get a reference to the button...
...and call its setOnClickListener method
Pass a lambda to the setOnClickListener method
How to edit a text view’s text
How to get the spinner’s selected value
The updated code for MainActivity.kt
What happens when you run the code
Test drive
Add the findBeers function
The full code for MainActivity.kt
What happens when you run the code
Test drive
Your Android Toolbox
3. Views and View Groups: Enjoy the View
It all starts with a layout
All UI components are a type of view...
...and all layouts are a type of view group
What we’re going to do
Create a new project
Let’s build a linear layout
How to define a linear layout
Orientation can be vertical or horizontal
Padding adds space to the layout’s edges
Add a dimension resource file for consistent padding across layouts
Our layout code so far
Add UI components to the linear layout
A linear layout displays views in the order they appear in the layout XML
Make a view streeeeetch by adding weight
How to add weight to one view
How to add weight to multiple views
The gravity attribute controls the position of a view’s contents
Values you can use with the android:gravity attribute
Layout Magnets
Layout Magnets Solution
The story so far
layout-gravity controls the position of a view within a layout
More values you can use with the android:layout-gravity attribute
Use margins to add space between views
The full linear layout code
Test drive
BE the Layout
BE the Layout Solution
A frame layout stacks its views
Create a new project
How to define a frame layout
Add an image to your project
A frame layout stacks views in the order they appear in the layout XML
Test drive
A scroll view inserts a vertical scrollbar
How to add a scroll view
Test drive
Layouts are a type of View called a ViewGroup
Android inflates views into objects
Let’s visit the View Zoo
An edit text lets you enter text
How to use an edit text in your activity code
Toggle buttons, switches and checkboxes are buttons that have two states
How to use a compound button in your activity code
Radio buttons let you select a single option
An image view displays an image
Adding an image to your project
Image view: the layout XML
Using image views in your activity code
Toasts
Using it in your activity code
Your Android Toolbox
4. Constraint Layouts Put Things in Their Place
Nested layouts revisited
Nested layouts can be inefficient
Introducing the constraint layout
You design constraint layouts VISUALLY
Create a new project
Add the String resources to strings.xml
Make activity_main.xml use a constraint layout
Use the blueprint tool
Position views using constraints
We’ll add a horizontal constraint to the button
Add a vertical constraint
Changing the view’s margins
Changes to the blueprint are reflected in the XML
How to center views
Adjust a view’s position by updating its bias
How to change a view’s size
1. Make the view a fixed size
2. Make it just big enough
3. Match the view’s constraints
4. Specify the width:height ratio
BE the Constraint
BE the Constraint Solution
How to align views
Let’s build a real layout
First, add the top line of views
Get the design editor to infer the constraints
The Infer Constraints feature guesses which constraints to add
Add the next line to the blueprint...
...and then add the button
Finally, add a view for the message
Test drive
Your Android Toolbox
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Next
Next Chapter
Head First Android Development
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset