Android Quiz timer task example
Ex. application for 4 questions and answers with time period,
Each question having 30 sec time to answer, if not answer show an alert your time up, when user click OK shift to next question.
When user answer the question show the next question with reset the time period.
user can allow to increase the timer for each click 30 sec.
Please check the bellow code snippet for reference.
QuizTimerActivity.Java:
------------------------
package com.example.test;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
public class QuizTimerActivity extends Activity {
private Context mContext;
private int TIME = 30, remaining_sec = 0;
private CountDownTimer countDownTimer;
private TextView textView_timer, currentqusView;
private int totalQus = 4, currentQus = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.timer_layout);
mContext = this;
textView_timer = (TextView) findViewById(R.id.textView);
currentqusView = (TextView) findViewById(R.id.currentqus);
}
public void getStart(View view){
currentqusView.setText("Current Question: "+currentQus);
TIME = 30;
timerTask();
}
public void getStop(View view){
if(countDownTimer!= null){
countDownTimer.cancel();
countDownTimer = null;
}
}
public void getReStart(View view){
currentQus = 1;
currentqusView.setText("Current Question: "+currentQus);
TIME = 30;
timerTask();
}
public void getIncrease(View view){
TIME = remaining_sec + 30;
timerTask();
}
public void getAns1(View view){
if(countDownTimer!= null){
countDownTimer.cancel();
countDownTimer = null;
}
if(currentQus < totalQus){
currentQus = currentQus + 1;
currentqusView.setText("Current Question: "+currentQus);
TIME = 30;
timerTask();
}
}
public void getAns2(View view){
if(countDownTimer!= null){
countDownTimer.cancel();
countDownTimer = null;
}
if(currentQus < totalQus){
currentQus = currentQus + 1;
currentqusView.setText("Current Question: "+currentQus);
TIME = 30;
timerTask();
}
}
public void getAns3(View view){
if(countDownTimer!= null){
countDownTimer.cancel();
countDownTimer = null;
}
if(currentQus < totalQus){
currentQus = currentQus + 1;
currentqusView.setText("Current Question: "+currentQus);
TIME = 30;
timerTask();
}
}
public void getAns4(View view){
if(countDownTimer!= null){
countDownTimer.cancel();
countDownTimer = null;
}
if(currentQus < totalQus){
currentQus = currentQus + 1;
currentqusView.setText("Current Question: "+currentQus);
TIME = 30;
timerTask();
}
}
public void dialogAlert(String message){
AlertDialog.Builder adb = new AlertDialog.Builder(mContext);
adb.setTitle(R.string.app_name);
adb.setMessage(message);
adb.setNeutralButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
currentQus = currentQus + 1;
currentqusView.setText("Current Question: "+currentQus);
TIME = 30;
timerTask();
dialog.cancel();
}
});
AlertDialog ad = adb.create();
ad.show();
}
public void timerTask(){
if(countDownTimer!= null){
countDownTimer.cancel();
countDownTimer = null;
}
countDownTimer = new CountDownTimer(TIME * 1000, 1000) {
public void onTick(long millisUntilFinished) {
remaining_sec = (int) millisUntilFinished/1000;
textView_timer.setText(""+remaining_sec);
System.out.println("TIME---" + TIME + "remaining_sec---" + remaining_sec);
}
public void onFinish() {
TIME = 0;
dialogAlert("Time finished");
}
}.start();
}
}
timer_layout.xml:
------------------
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical" >
<TextView
android:id="@+id/currentqus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Current Question : ?"
android:textColor="#000000"
android:textSize="30sp" />
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your Time here"
android:gravity="center"
android:textColor="#000000"
android:textSize="30sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp" >
<Button
android:id="@+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="getAns1"
android:layout_weight="1"
android:text="Ans1" />
<Button
android:id="@+id/button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="getAns2"
android:layout_weight="1"
android:text="Ans2" />
<Button
android:id="@+id/button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="getAns3"
android:text="Ans3" />
<Button
android:id="@+id/button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="getAns4"
android:text="Ans4" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="getStart"
android:text="Start" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="getStop"
android:text="Stop" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="getReStart"
android:text="ReStart" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="getIncrease"
android:text="Increase" />
</LinearLayout>
</LinearLayout>
</ScrollView>