Skip to content Skip to sidebar Skip to footer

How To Implement Timer On Android Quiz?

I am working on Android quiz .I wanted to add timer on ImageView In Question XML , I want to call the CountDownTimer from Welcome XML on Play button. I just set the onClick in Que

Solution 1:

I don't see anything wrong with your timer implementation

But you have this

 text = (TextView) this.findViewById(R.id.button1);

And you have this

   <ImageView
android:id="@+id/button1"

So it should be

 <TextView
android:id="@+id/textView1" 

And

text = (TextView) findViewById(R.id.textView1);

Try the below. On click of the button timer starts.

public class MainActivity extends Activity implements OnClickListener {

private Button startB;
public TextView text;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startB = (Button) this.findViewById(R.id.button1);
startB.setOnClickListener((OnClickListener) this);
text = (TextView) findViewById(R.id.textView1);
}

@Override
public void onClick(View v) {
    new CountDownTimer(30000, 1000) { // adjust the milli seconds here

        public void onTick(long millisUntilFinished) {
        text.setText(""+String.format("%d min, %d sec", 
                        TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished),
                        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - 
                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
        }

        public void onFinish() {
           text.setText("done!");
        }
     }.start();

}   
}

Check this link

For Java versions below 1.5 or for systems that do not fully support the TimeUnit class the following equations can be used:

int seconds = (int) (milliseconds / 1000) % 60 ;
int minutes = (int) ((milliseconds / (1000*60)) % 60);
int hours   = (int) ((milliseconds / (1000*60*60)) % 24);

Edit:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="35dp"
        android:text="TextView" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="68dp"
        android:text="Button" />

</RelativeLayout>

enter image description here

enter image description here


Solution 2:

You should try following code.

private void setTimer() {
    counterTimer = new CountDownTimer(finishTime * 1000, 1000) {
        public void onFinish() {
            //code to execute when time finished
        }

        public void onTick(long millisUntilFinished) {
            int seconds = (int) (millisUntilFinished / 1000);
            int minutes = seconds / 60;
            seconds = seconds % 60;

            if (seconds < 10) {
                txtTimer.setText("" + minutes + ":0" + seconds);
            } else {
                txtTimer.setText("" + minutes + ":" + seconds);
            }
        }
    };
    counterTimer.start();
}

Here examFinishTime is an long value like

private long finishTime = 5; 

Note: Argument is in Miliseconds.

To start timer, just call setTimer() method in any button's click event.


Post a Comment for "How To Implement Timer On Android Quiz?"