User Tools

Site Tools


android_learning:headfirst_android_development_notes:chapter_4

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
android_learning:headfirst_android_development_notes:chapter_4 [2016/01/09 01:07] – created mithatandroid_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 ''content_stopwatch.xml'': 
 + 
 +<code java> 
 +    <TextView 
 +        android:layout_width="wrap_content" 
 +        android:layout_height="wrap_content" 
 +        android:textAppearance="?android:attr/textAppearanceLarge" 
 +        android:textSize="92sp" 
 +        android:text="" 
 +        android:id="@+id/time_view" 
 +        android:layout_alignParentTop="true" 
 +        android:layout_centerHorizontal="true" /> 
 + 
 +    <Button 
 +        android:layout_width="wrap_content" 
 +        android:layout_height="wrap_content" 
 +        android:text="@string/start" 
 +        android:id="@+id/start_button" 
 +        android:layout_below="@+id/time_view" 
 +        android:layout_centerHorizontal="true" 
 +        android:layout_marginTop="20dp" 
 +        android:onClick="onClickStart" /> 
 + 
 +    <Button 
 +        android:layout_width="wrap_content" 
 +        android:layout_height="wrap_content" 
 +        android:text="@string/stop" 
 +        android:id="@+id/stop_button" 
 +        android:layout_below="@+id/start_button" 
 +        android:layout_centerHorizontal="true" 
 +        android:layout_marginTop="10dp" 
 +        android:onClick="onClickStop" /> 
 + 
 +    <Button 
 +        android:layout_width="wrap_content" 
 +        android:layout_height="wrap_content" 
 +        android:text="@string/reset" 
 +        android:id="@+id/reset_button" 
 +        android:layout_below="@+id/stop_button" 
 +        android:layout_centerHorizontal="true" 
 +        android:layout_marginTop="10dp" 
 +        android:onClick="onClickReset" /> 
 +</code> 
 + 
 +===== p. 122: Code blocks ===== 
 + 
 +Extra code to add to ''StopWatchActivity.java'' (i.e., don't replace the entire file). 
 + 
 +Member variables: 
 +<code java> 
 +// Number of seconds displayed on the stopwatch 
 +private int seconds = 0; 
 +// Is the stopwatch running? 
 +private boolean running; 
 +</code> 
 + 
 +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; 
 +
 +</code> 
 + 
 +===== p. 124 ===== 
 +Here is some boilerplate repeating ''Handler'' code: 
 +<code java> 
 +final Handler handler = new Handler();  // Make a new Handler 
 +handler.post(new Runnable() {   // Give handler a Runnable instance to execute ASAP 
 +    @Override                   // Redefine the Runnables's run method 
 +    public void run() { 
 +        /* Stuff you 
 +         * want to do 
 +         * goes here. */ 
 +        // Repost the Runnable (this) to run some time in the future. 
 +        handler.postDelayed(this, MILLISECONDS_UNTIL_handler.post_IS_CALLED_AGAIN); 
 +    } 
 +}); 
 +</code> 
 + 
 +===== p. 127: Code blocks ===== 
 +To ''StopwatchActivity.java'' add the following to the end of ''onCreate()'' 
 +<code java> 
 +runTimer(); 
 +</code> 
 + 
 +and the ''runTimer()'' method: 
 +<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/3600; 
 +            int minutes = (seconds%3600)/60
 +            int secs = seconds%60; 
 +            String time = String.format("%d:%02d:%02d", 
 +                    hours, minutes, secs); 
 +            timeView.setText(time); 
 +            if (running) { 
 +                seconds++; 
 +            } 
 +            handler.postDelayed(this, 1000); 
 +        } 
 +    }); 
 +
 +</code
 + 
 +Be sure you import ''android.os.Handler'' and ''android.widget.TextView''. 
 + 
 +===== p. 130 ===== 
 +To rotate a stock AVD, use the ''Ctrl+F11'' shortcut to switch to the previous layout orientation and ''Ctrl+F12'' to switch to next layout orientation. 
 + 
 +===== pp. 137-138: Code blocks ===== 
 + 
 +''onSaveInstanceState'' method to be added to ''StopwatchActivity'': 
 +<code java> 
 +@Override 
 +public void onSaveInstanceState(Bundle savedInstanceState) { 
 +    savedInstanceState.putInt("seconds", seconds); 
 +    savedInstanceState.putBoolean("running", running); 
 +} 
 +</code> 
 + 
 +Code to be added to ''onCreate()'' just before ''runTimer();'': 
 +<code java> 
 +if (savedInstanceState != null) { 
 +    seconds = savedInstanceState.getInt("seconds"); 
 +    running = savedInstanceState.getBoolean("running"); 
 +
 +</code> 
 + 
 +===== p. 147: Code blocks ===== 
 +Additional member variable: 
 +<code java>private boolean wasRunning;</code> 
 + 
 +Restore additional item in ''onCreate'''s ''if (savedInstanceState != null)'' block: 
 +<code> 
 +wasRunning = savedInstanceState.getBoolean("wasRunning"); 
 +</code> 
 + 
 +Add additional item to ''onSaveInstanceState'': 
 +<code java> 
 +savedInstanceState.putBoolean("wasRunning", wasRunning); 
 +</code> 
 + 
 +New methods: 
 +<code java> 
 +@Override 
 +protected void onStop() { 
 +    super.onStop(); 
 +    wasRunning = running; 
 +    running = false; 
 +
 +</code> 
 +<code java> 
 +@Override 
 +protected void onStart() { 
 +    super.onStart(); 
 +    if (wasRunning) { 
 +        running = true; 
 +    } 
 +
 +</code> 
 + 
 +===== p. 149: Expected app behavior ===== 
 +{{youtube>eJI5Hv7hiR0??600x360&rel=0}} 
 +\\ 
 + 
 +===== 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 ''onStart'' and ''onStop'' are no longer needed. This is because ''onResume'' will be called after ''onStart'' and ''onPause'' will normally be called before ''onStop''
 + 
 +Coming up with a test case on an AVD that will partially obscure your app and put it into "Pause" is not trivial. However, if you start and restart the app, it should behave the same as before (even though you have now placed the run/don't run code in ''onResume'' and ''onPause''
 + 
 +Don't stress out if you aren't able to test that the changed code works when the app is "Paused" (i.e., not having focus), but do be sure it still works when "Stopped" and "Restarted". And be sure to understand what Android is doing and the pause/resume process. (P.S., don't forget to read pp. 152-153.) 
 + 
 +===== 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

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki