TABLE OF CONTENTS (HIDE)

Android: Miscellaneous

A Count-Down Timer Example

Reference: "CountDownTimer" @ https://developer.android.com/reference/android/os/CountDownTimer.html.

Start a Android Studio project called "Count Down Timer" with "Empty Views Activity".

Define the Layout in "res\Layout\activity_main.xml"

We use the TextView (from the Hello-world template) to display our timer.

<?xml ......?>
<androidx.constraintlayout.widget.ConstraintLayout ......>

    <TextView
        android:id="@+id/txtTimer"
        android:text="hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Program the "MainActivity.java"

Android SDK provides an android.os.CountDownTimer class, which implements a countdown timer.

package ......;
import ......;
import android.os.CountDownTimer;
import android.widget.TextView;

public class MainActivity extends ...... {
    private TextView txtTimer;

    // Replace the onCreate()
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtTimer = (TextView)  findViewById(R.id.txtTimer);
        // Count down from 30 sec. onTick() every second. Values in milliseconds
        new CountDownTimer(30000, 1000) {
            public void onTick(long millisRemaining) {
                txtTimer.setText("Seconds remaining: " + millisRemaining / 1000);
            }
            public void onFinish() {
                txtTimer.setText("Time Up!");
            }
        }.start();
    }
}

Toast and Logging (in Logcat)

Create a new project called "Test Messages" with "Empty View Activity".

Define the Layout in "res\Layout\activity_main.xml"

We use a Button to trigger toast, log and messages.

<?xml ......?>
<androidx.constraintlayout.widget.ConstraintLayout ......>

   <Button
      android:id="@+id/btnID"
      android:onClick="btnOnClickHandler"
      android:text="Click Me!"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Program the "MainActivity.java"

Android SDK provides an android.os.CountDownTimer class, which implements a countdown timer.

package ......;
import ......;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends ...... {
    private static final String TAG = MyActivity.class.getSimpleName();

    // no change
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       ......
    }

    // Add this method
    // On-click handler for the button
    public void btnOnClickHandler(View v) {
        // Toast a message (a floating view of a short duration)
        Toast.makeText(this, "hello", Toast.LENGTH_LONG).show();
        // Check the Logcat console for these messages
        Log.i(TAG, "write an info message");
        Log.e(TAG,"write an error message");
        System.out.println("hello, world");   // converted to Log.i()
    }
}
  1. Click the button and look out for the toast message. A toast is a short-duration floating view.
  2. Show the Logcat console (from right-bottom menu bar). Look out for the log information and error messages. The system.out.println() message is shown as Log.i() message in Logcat.
  3. You can write to logcat via static methods:
    • Log.e() - error
    • Log.w() - warning
    • Log.i() - information
    • Log.d() - debug
    • Log.v() - verbose
    • Log.wtf() - what a terrible failure
  4. Toast and logging can help you in debugging your program.
  5. The error stack trace is also shown in the logcat console. Hence, check the logcat console if somethings go wrong.

Debugging

Read "Debug your app" @ https://developer.android.com/studio/debug. Make sure that you read through the entire article if you need to write production app.

In brief, the steps are:

  1. Set a breakpoint by clicking on the line number at the left margin. A RED dot appears indicating a breakpoint is set.
  2. Click "Debug app" button on the top menu bar.
  3. At the bottom "Debug" pane, you can find the execution toolbar with "Step Over", "Step Into", "Step Out", "Resume", "Stop" buttons.
  4. You can read the value of a variable by placing the cursor over that variable, or at the "Variable" pane.