Android Bootcamp Week 1
Hello Android
https://github.com/ThoughtWorksAustralia/AndroidBootcampProject#week-1-hello-android
Goals:
- Develop a working Android app and explore its anatomy.
- Allow the user to enter their name.
Retrospective...
Next week on
Android Bootcamp:
Behaviour Driven Development
with Android
Next week...
- What did we do well?
- What could we do better?
- What puzzles you?
You've created a new Android App
What the heck is all this stuff?
Summary
- You've dissected an Android and seen:
- Activities
- Resources
- The Manifest
- You've run your app on an emulator
- You've created your own activity so the user can enter their name
- You've explored the Android life-cycle
Exercise 1: Explore the Lifecycle
Exercise 2: Create a Second Activity
- Use File > New > Activity to open the New Activity wizard. Choose Blank Activity on the first page. Fill in the second page like this:
- Run the newly created application on the Genymotion emulator
- Try filtering LogCat to your androidbootcamp package
- Move the HelloAndroid activity to a new controller package
- Use Code > Generate to generate overrides for onDestroy(), onStart(), onStop(), onPause(), onResume() and onRestart().
- Add a log statement to each of these as well as onCreate(): Log.d("lifecycle", "HelloAndroid <method>");
- Filter LogCat to the "lifecycle" tag, then explore when these messages are produced. What happens when you change from portrait to landscape?
The build directory holds generated files including R, a class that provides references to all resources.
The Android Activity Life-Cycle
- Explore the files that are created. Try running the app. Fix the compile errors.
- We will use the new activity to allow the player to enter their name and return it to the first activity:
- In the layout for the new activity, add a TextView, an EditText and a Button.
- Add an onClick handler to the button that returns the name entered by the player
Resources: Static files
to be packaged
with the app
These form the
View, and exist to
keep the view separate
from the model and
controller code
src/main/java holds your Java classes. Android Studio creates the first one for you. It's an Activity.
Activities are descendants of android.app.Activity. They are controllers that orchestrate the flow of control between screens of your app. Keep the code in these as brief as possible. Business logic should be delegated to model classes.
Use Intents to progress from one Activity to another.
An Intent is a description of an action to be performed, together with data relevant to the action.
Drawables: Images in various
resolutions for different screen densities.
9 patches allow you to control how images get stretched. Extension is .9.png
You can use image files from the platforms/data/res directory of the Android SDK under the Apache License 2.0, or create your own with the help of the Android Asset Studio
Exercise 4: Keep the game state safe
Exercise 3: Capture the Player's Name
Layouts describe in xml how the various activities in your app appear on screen.
Reuse parts of pages with fragments.
Menus are also described in xml.
dimens.xml allows you to name sizes of things. Best practice is to use sp (scale independent pixels) for font sizes, dp (density independent pixels) for everything else.
strings.xml allows you to name string resources
styles.xml allows you to define sets of styling to be applied to screen elements. Styles can be inherited.
Gradle is the tool used to package up your application so it can be delivered to your device.
When you make changes to your project settings (eg dependencies) through Studio, those settings are saved in the Gradle files as well as in the
Studio project files (.idea and .iml files).
Gradle files should be checked in to source control, project files should not.
Directory suffixes allow you to override view elements for different devices and locales, eg
- values-w820dp for a dimens.xml for screens more than 820dp wide
- values-fr for a French version of strings.xml
- layout-land for a landscape version of a layout
- layout-large for a large screen version of a layout
512x512 image
for the Play store
The Manifest configures the app as a whole, and each Activity within it.
All Activities must be listed in the Manifest
What happens if you switch from portrait to landscape?
To stop the game state from being lost when the activity is recreated, we have to save and restore the instance state.
- Make the Game class implement Serializable (for good performance this should be Parcelable instead, but that adds complexity)
- Use outState.putSerializable to save the game in the onSaveInstanceState method
- In onRestoreInstanceState:
- Use savedInstanceState.getSerializable to restore the game
- Restore the text of the Welcome TextView
Optional extras:
- Add debug logging to the lifecycle events of the WhoAmI activity to explore how the lifecycles interact.
- What happens if you click the Home key then re-open the application. What if you click the Back key?
- Add a TextView to the layout for HelloAndroid, where we'll display a welcome message that includes the player's name. This is NOT best practice. The TextView should be in the Fragment. We'll cover that in a later week.
- Add a new Java class called 'Game' in a new package called
com.<your package name>.androidbootcamp.model.
Add a String field 'player' with a getter and a constructor that takes the player's name as a parameter.
- Add a field to the HelloAndroid Activity of type Game. This will hold all the state for the current game, including the player's name.
- In the onCreate of HelloAndroid, open the new activity using startActivityForResult.
- Add an onActivityResult handler to HelloAndroid that retrieves the name from the Intent returned by WhoAmI, and uses it to create a new Game instance.
Why are we here?
- To learn about Android application development together
- To build a simple game incrementally over six weeks
- Treasure Hunt
- Score points by finding 'treasures' in our local area
Running the App
in the Genymotion Emulator
design by Dóri Sirály for Prezi
The Android robot is reproduced or modified from work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.