This is an old revision of the document!
Table of Contents
Under development.
Chapter 4 notes
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
:
<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" />
p. 122
Extra code to add to StopWatchActivity.java
(i.e., don't replace the entire file).
Member variables:
// Number of seconds displayed on the stopwatch private int seconds = 0; // Is the stopwatch running? private boolean running;
Methods:
//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 Handler
code:
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); } });
p. 127
To StopwatchActivity.java
add the following to the end of onCreate()
runTimer();
and the runTimer()
method:
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); } }); }
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 locks
onSaveInstanceState
method to be added to StopwatchActivity
:
@Override public void onSaveInstanceState(Bundle savedInstanceState) { savedInstanceState.putInt("seconds", seconds); savedInstanceState.putBoolean("running", running); }
Code to be added to onCreate()
just before runTimer();
:
if (savedInstanceState != null) { seconds = savedInstanceState.getInt("seconds"); running = savedInstanceState.getBoolean("running"); }
p. 147: Code blocks
Additional member variable:
private boolean wasRunning;
Restore additional item in onCreate
's if (savedInstanceState != null)
block:
wasRunning = savedInstanceState.getBoolean("wasRunning");
Add additional item to onSaveInstanceState
:
savedInstanceState.putBoolean("wasRunning", wasRunning);
New methods:
@Override protected void onStop() { super.onStop(); wasRunning = running; running = false; }
<code java> @Override protected void onStart() {
super.onStart(); if (wasRunning) { running = true; }
} <code>