Android Studio CountDown Timer (ASCT)


ASCT:   SOLD       




This tutorial was created in order to share after completing #Assignment3 in the Mobile Programming course, supervised by Mr. Untung Subagyo, S.Kom. This application was created using Android Studio.


Figure 1. First Interface and CountDown Running

Completion

Step 1:

Open Android Studio

Step 2:

Select New Project >> create a project name of your choice, for example "Task3" >> Next.

Step 3:

Select Target Device >> Check Phone and Tablet >> API14: Android 4.0 (IceCreamSandwich) >> Next.

Step 4:

Select Empty Activity, as the main layout >> Customize the Activity & Layout name / (default) >> Finish.

Step 5:

Select the activity_main.xml tab >> Design mode.

Step 6:

Drag & Drop TextView to Layout, then adjust its position.

Step 7:

Drag & Drop 4 buttons, then adjust their positions.

Step 8:

Set the size of all buttons by activating text mode, then change:

  • android:layout_width="300px"
  • android:layout_height="300px"

Step 9:

Change the top TextView Properties, ID to "question" and text to "200 + 21"

Step 10:

Change the background color of the four buttons as you like, for example "red, yellow, green, blue" >> via properties.

Step 11:

Change the ID of the four Buttons as you wish, for example "b1, b2, b3, b4" >> via properties.

Step 12:

Change the text of the four Buttons as you wish, but still provide the answer to the question. For example, mine are "212" (correct), "71", "36", "50".

Step 13:

Change the default TextView at the bottom to "Result" and its ID "result".

Step 14:

Drag & Drop TextView and place it in the top left corner, change ID(cd), change size(50x30dp), text(s), background(black), text color(white) via properties.

Step 15:

Declare some variables for button, textView and countdown into MainActivity class. Which have been prepared previously in the layout.

Step 16:

Define those variables inside the onCreate method, where "txt" is the widget with id "result", btn1 = widget with id "b1", btn2 = widget with id "b2", btn3 = widget with id "b3", btn4 = widget with id "b4" and timer = widget with id "lets".

Step 17:

Add implements View.OnClickListener to the MainActivity class.

Step 18:

Define the onClick() method as follows:

@Override
    public void onClick (View v) {
        if (v.equals(btn4)) {
            /*memangil aset color yang telah didefinisikan di app\res\values\color.xml*/
            txt.setTextColor(getResources().getColor(R.color.colorRight));
            txt.setText("Excellentt (y)(y)");
            timer.cancel();
        }
        else{
            txt.setTextColor(getResources().getColor(R.color.colorWrong));
            txt.setText("Wrong Answer!");
        }
    }

Step 19:

Go back to the activity_main.xml tab in Design mode.

Step 20:

Activate the onClick feature for all buttons via properties >> and select the onClick() method.

Step 21:

Run Project via AVD.

Here are the results / captures


Figure 2. Interface When CountDown is Up


Figure 3. Interface When Pressing the Wrong Button


Figure 4. Interface When Pressing the Correct Button

The Complete Package

Because the project file is quite large, I chose Dropbox as a means of sharing, here is the link:

Here is the demo

https://youtu.be/ltVolLay5W0

Source Code

Maybe you just want to adopt the source code. I provide it below.

The activity - MainActivity.java

package com.gatewan.tugas3;
/*Author Profile
Wawan Chahyo Nugroho
NIM: 12131294
 */
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
   /* Deklarasi variabel atau object */
    Button btn4;
    Button btn3;
    Button btn2;
    Button btn1;
    TextView txt;
    CountDownTimer timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt = (TextView) findViewById(R.id.result); //variabel txt dialokasikan untuk TextView dengan id "result"
        final TextView lets = (TextView) findViewById(R.id.cd);
        btn4 = (Button) findViewById(R.id.b4);
        btn4.setOnClickListener(this);
        btn3 = (Button) findViewById(R.id.b3);
        btn3.setOnClickListener(this);
        btn2 = (Button) findViewById(R.id.b2);
        btn2.setOnClickListener(this);
        btn1 = (Button) findViewById(R.id.b1);
        btn1.setOnClickListener(this);

        timer = new CountDownTimer(5000, 1000) { //mendefinisikan hitung mundur dengan tempo 5 detik

            public void onTick(long millisUntilFinished) {
                lets.setText(millisUntilFinished / 1000+"s");
            }

            public void onFinish() {
                txt.setText("Do Something!"); //untuk respon saat tempo 5 detik telah habis

            }
        }.start();

    }

    @Override
    public void onClick (View v) {
        if (v.equals(btn4)) {
            /*memangil aset color yang telah didefinisikan di app\res\values\color.xml*/
            txt.setTextColor(getResources().getColor(R.color.colorRight));
            txt.setText("Excellentt (y)(y)");
            timer.cancel();
        }
        else{
            txt.setTextColor(getResources().getColor(R.color.colorWrong));
            txt.setText("Wrong Answer!");
        }
    }

}

Layout - activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.gatewan.tugas3.MainActivity">

    <TextView
        android:text="Result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/result"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:textSize="30sp"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="16dp"
        app:layout_constraintVertical_bias="0.89"
        android:layout_marginStart="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="16dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginRight="16dp" />

    <Button
        android:text="212"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:id="@+id/b4"
        android:background="@android:color/holo_green_light"
        android:textSize="36sp"
        android:layout_above="@+id/result"
        android:layout_alignLeft="@+id/b2"
        android:layout_alignStart="@+id/b2"
        android:onClick="onClick (MainActivity)"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginRight="16dp"
        app:layout_constraintLeft_toLeftOf="@+id/guideline"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHorizontal_bias="0.81"
        app:layout_constraintVertical_bias="0.19" />

    <Button
        android:text="36"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:id="@+id/b3"
        android:background="@android:color/holo_blue_dark"
        android:textSize="36sp"
        android:layout_alignBaseline="@+id/b4"
        android:layout_alignBottom="@+id/b4"
        android:layout_toLeftOf="@+id/b4"
        android:layout_toStartOf="@+id/b4"
        android:onClick="onClick (MainActivity)"
        android:layout_marginStart="48dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="48dp"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="16dp"
        app:layout_constraintVertical_bias="0.52" />

    <Button
        android:text="50"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:id="@+id/b2"
        android:background="@android:color/holo_orange_dark"
        android:textSize="36sp"
        android:layout_above="@+id/b4"
        android:layout_toRightOf="@+id/soal"
        android:layout_toEndOf="@+id/soal"
        android:layout_marginTop="72dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginStart="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="16dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginRight="16dp"
        app:layout_constraintHorizontal_bias="0.8"
        android:onClick="onClick (MainActivity)"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="16dp"
        app:layout_constraintVertical_bias="0.41" />

    <Button
        android:text="71"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:id="@+id/b1"
        android:textSize="36sp"
        android:background="@android:color/holo_red_dark"
        android:layout_alignBaseline="@+id/b2"
        android:layout_alignBottom="@+id/b2"
        android:layout_toLeftOf="@+id/b2"
        android:layout_toStartOf="@+id/b2"
        android:onClick="onClick (MainActivity)"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="16dp"
        app:layout_constraintVertical_bias="0.19"
        android:layout_marginStart="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="16dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginRight="16dp"
        app:layout_constraintHorizontal_bias="0.19" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="200 + 12 = ?"
        android:id="@+id/soal"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textSize="24sp"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginStart="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="16dp"
        app:layout_constraintVertical_bias="0.0"
        android:layout_marginEnd="16dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginRight="16dp" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline"
        app:layout_constraintGuide_begin="10dp"
        android:orientation="vertical"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="10dp" />

    <TextView
        android:text="s"
        android:layout_width="50dp"
        android:id="@+id/cd"
        android:background="@android:color/background_dark"
        android:layout_height="35dp"
        android:textColor="@android:color/background_light"
        android:textSize="25sp"
        android:textAlignment="center"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginStart="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="16dp" />

</android.support.constraint.ConstraintLayout>

My Friend's Version a/n  Krisna Pandu Wibowo

You can download the project package  HERE , or below if you only need the source code.

The activity - MainActivity.java

package com.worker.ecoframe.aplikasisoal;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements android.view.View.OnClickListener {
    public CountDownTimer timer;
    public TextView hasil;

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

        final TextView text = (TextView) findViewById(R.id.tv);
        final Button b1 = (Button) findViewById(R.id.button);
        b1.setOnClickListener(this);
        final Button b2 = (Button) findViewById(R.id.button2);
        b2.setOnClickListener(this);
        final Button b3 = (Button) findViewById(R.id.button3);
        b3.setOnClickListener(this);
        final Button b4 = (Button) findViewById(R.id.button4);
        b4.setOnClickListener(this);

        timer = new CountDownTimer(5000, 1000) {

            public void onTick(long millisUntilFinished) {
                text.setText(millisUntilFinished / 1000 + "S");
            }
            public void onFinish() {
                Intent intent = new Intent(MainActivity.this, ResultActivity.class);
                MainActivity.this.startActivity(intent);
            }
        }.start();
    }

    @Override
    public void onClick(View v){
        hasil = (TextView) findViewById(R.id.textView3);
        switch(v.getId()) {
            case R.id.button:
                hasil.setText("Wrong Answer!!!");
                break;
            case R.id.button2:
                hasil.setText("Wrong Answer!!!");
                break;
            case R.id.button3:
                hasil.setText("Wrong Answer!!!");
                break;
            case R.id.button4:
                hasil.setText("Correct!!!");
                timer.cancel();
                break;
        }
    }

}

The activity - ResultActivity.java

package com.worker.ecoframe.aplikasisoal;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ResultActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);

        final TextView message = (TextView) findViewById(R.id.tMessage);
        final Button btn = (Button) findViewById(R.id.button5);

        message.setText("WAKTU HABIS");

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ResultActivity.this, MainActivity.class);
                ResultActivity.this.startActivity(intent);

            }
        });

    }
}

Layoutnya - activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.worker.ecoframe.aplikasisoal.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="16+19"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.123"
        android:id="@+id/textView2" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:background="@color/colorYellow"
        android:text="TextView"
        android:textAlignment="center"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.106"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.085" />

    <Button
        android:id="@+id/button"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:background="@color/colorPrimary"
        android:text="24"
        android:textSize="36sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.201"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        app:layout_constraintVertical_bias="0.174" />

    <Button
        android:id="@+id/button2"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:layout_marginBottom="0dp"
        android:layout_marginRight="8dp"
        android:background="@android:color/holo_green_light"
        android:text="37"
        android:textSize="36sp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintHorizontal_bias="0.716"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/button4"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        app:layout_constraintVertical_bias="1.0" />

    <Button
        android:id="@+id/button3"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="118dp"
        android:background="@color/colorAccent"
        android:text="4"
        android:textSize="36sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.201"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        app:layout_constraintVertical_bias="0.368" />

    <Button
        android:id="@+id/button4"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:layout_marginRight="53dp"
        android:background="@android:color/holo_blue_bright"
        android:text="35"
        android:textSize="36sp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="52dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="0.878"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        app:layout_constraintVertical_bias="0.729" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:text="TextView"
        android:textSize="36sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        app:layout_constraintVertical_bias="0.863" />

</android.support.constraint.ConstraintLayout>

Layout - activity_result.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.worker.ecoframe.aplikasisoal.ResultActivity">

    <TextView
        android:id="@+id/tMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:text="TextView"
        android:textSize="36sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.378" />

    <Button
        android:id="@+id/button5"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:background="@android:color/holo_blue_dark"
        android:text="TRY AGAIN"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tMessage" />
</android.support.constraint.ConstraintLayout>

Mission Completed!!!


Post a Comment

Previous Next

نموذج الاتصال