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.
publicclassMainActivityextendsActivityimplementsOnClickListener {
privateButton startB;
publicTextView text;
@OverridepublicvoidonCreate(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);
}
@OverridepublicvoidonClick(View v) {
newCountDownTimer(30000, 1000) { // adjust the milli seconds herepublicvoidonTick(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))));
}
publicvoidonFinish() {
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:
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent" ><TextViewandroid: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" /><Buttonandroid: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>
Solution 2:
You should try following code.
privatevoidsetTimer() {
counterTimer = new CountDownTimer(finishTime * 1000, 1000) {
publicvoidonFinish() {
//code to execute when time finished
}
publicvoidonTick(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
privatelong 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?"