android_learning:headfirst_android_development_notes:chapter_4
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
android_learning:headfirst_android_development_notes:chapter_4 [2016/01/09 01:07] – created mithat | android_learning:headfirst_android_development_notes:chapter_4 [2016/02/14 23:08] (current) – [p. 147: Code blocks] mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | <WRAP center round info 60%> | + | ====== Chapter 4 notes ====== |
- | Under development. | + | |
- | </WRAP> | + | You should be fairly comfortable with Android Studio by now, so you can expect fewer videos going forward. |
+ | |||
+ | ====== p. 119-120: Code block ====== | ||
+ | Here are the Views in my '' | ||
+ | |||
+ | <code java> | ||
+ | < | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | |||
+ | < | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | |||
+ | < | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | |||
+ | < | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | </ | ||
+ | |||
+ | ===== p. 122: Code blocks ===== | ||
+ | |||
+ | Extra code to add to '' | ||
+ | |||
+ | Member variables: | ||
+ | <code java> | ||
+ | // Number of seconds displayed on the stopwatch | ||
+ | private int seconds = 0; | ||
+ | // Is the stopwatch running? | ||
+ | private boolean running; | ||
+ | </ | ||
+ | |||
+ | Methods: | ||
+ | <code java> | ||
+ | //Start the stopwatch running when the Start button is clicked. | ||
+ | public void onClickStart(View view) { | ||
+ | running = true; | ||
+ | } | ||
+ | |||
+ | //Stop the stopwatch running when the Stop button is clicked. | ||
+ | public void onClickStop(View view) { | ||
+ | running = false; | ||
+ | } | ||
+ | |||
+ | //Reset the stopwatch when the Reset button is clicked. | ||
+ | public void onClickReset(View view) { | ||
+ | running = false; | ||
+ | seconds = 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== p. 124 ===== | ||
+ | Here is some boilerplate repeating '' | ||
+ | <code java> | ||
+ | final Handler handler = new Handler(); | ||
+ | handler.post(new Runnable() { // Give handler a Runnable instance to execute ASAP | ||
+ | @Override | ||
+ | public void run() { | ||
+ | /* Stuff you | ||
+ | * want to do | ||
+ | * goes here. */ | ||
+ | // Repost the Runnable (this) to run some time in the future. | ||
+ | handler.postDelayed(this, | ||
+ | } | ||
+ | }); | ||
+ | </ | ||
+ | |||
+ | ===== p. 127: Code blocks ===== | ||
+ | To '' | ||
+ | <code java> | ||
+ | runTimer(); | ||
+ | </ | ||
+ | |||
+ | and the '' | ||
+ | <code java> | ||
+ | private void runTimer() { | ||
+ | final TextView timeView = (TextView)findViewById(R.id.time_view); | ||
+ | final Handler handler = new Handler(); | ||
+ | handler.post(new Runnable() { | ||
+ | @Override | ||
+ | public void run() { | ||
+ | int hours = seconds/ | ||
+ | int minutes = (seconds%3600)/ | ||
+ | int secs = seconds%60; | ||
+ | String time = String.format(" | ||
+ | hours, minutes, secs); | ||
+ | timeView.setText(time); | ||
+ | if (running) { | ||
+ | seconds++; | ||
+ | } | ||
+ | handler.postDelayed(this, | ||
+ | } | ||
+ | }); | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | Be sure you import '' | ||
+ | |||
+ | ===== p. 130 ===== | ||
+ | To rotate a stock AVD, use the '' | ||
+ | |||
+ | ===== pp. 137-138: Code blocks ===== | ||
+ | |||
+ | '' | ||
+ | <code java> | ||
+ | @Override | ||
+ | public void onSaveInstanceState(Bundle savedInstanceState) { | ||
+ | savedInstanceState.putInt(" | ||
+ | savedInstanceState.putBoolean(" | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | Code to be added to '' | ||
+ | <code java> | ||
+ | if (savedInstanceState != null) { | ||
+ | seconds = savedInstanceState.getInt(" | ||
+ | running = savedInstanceState.getBoolean(" | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== p. 147: Code blocks ===== | ||
+ | Additional member variable: | ||
+ | <code java> | ||
+ | |||
+ | Restore additional item in '' | ||
+ | < | ||
+ | wasRunning = savedInstanceState.getBoolean(" | ||
+ | </ | ||
+ | |||
+ | Add additional item to '' | ||
+ | <code java> | ||
+ | savedInstanceState.putBoolean(" | ||
+ | </ | ||
+ | |||
+ | New methods: | ||
+ | <code java> | ||
+ | @Override | ||
+ | protected void onStop() { | ||
+ | super.onStop(); | ||
+ | wasRunning = running; | ||
+ | running = false; | ||
+ | } | ||
+ | </ | ||
+ | <code java> | ||
+ | @Override | ||
+ | protected void onStart() { | ||
+ | super.onStart(); | ||
+ | if (wasRunning) { | ||
+ | running = true; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== p. 149: Expected app behavior ===== | ||
+ | {{youtube> | ||
+ | \\ | ||
+ | |||
+ | ===== pp. 152-153 ===== | ||
+ | You may want to skip these pages until you have completed the app through p. 158. | ||
+ | |||
+ | ===== pp. 154-158 ===== | ||
+ | Note that in the final version of this app, methods '' | ||
+ | |||
+ | Coming up with a test case on an AVD that will partially obscure your app and put it into " | ||
+ | |||
+ | Don't stress out if you aren't able to test that the changed code works when the app is " | ||
+ | |||
+ | ===== p. 162 ===== | ||
+ | The diagram is a good one to copy and tape next to your computer. |
android_learning/headfirst_android_development_notes/chapter_4.1452301625.txt.gz · Last modified: 2016/01/09 01:07 by mithat