countdowntimer - Android Quiz Timer Issues -
implementation 1 issue
doubt 1: tried timer using progress bar. 5 seconds per question. however, progress bar display countdown values. issue after time gets over(value reaches 0 ) , not go next question default ..
implementation 2 issue
doubt 2: ,how can set countdown timer using progress bar starts @ first question , decreasing values continuously till specified limit.like want 20 questions in quiz answered in 120 mins. progress bar should 120,119 ....1 .and after reaching 0,i should automatically redirected results page.
questionactivity.java
package works.really.good; import java.sql.date; import java.util.list; import android.app.activity; import android.content.intent; import android.graphics.color; import android.os.bundle; import android.os.countdowntimer; import android.text.format.dateformat; import android.util.log; import android.view.keyevent; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.progressbar; import android.widget.radiobutton; import android.widget.textview; import works.really.good.quiz.gameplay; import works.really.good.quiz.question; import works.really.good.util.utility; public class questionactivity extends activity implements onclicklistener{ private question currentq; private gameplay currentgame; progressbar mprogressbar; countdowntimer mcountdowntimer; textview tv; int i=0; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.question); currentgame = ((myapplication)getapplication()).getcurrentgame(); currentq = currentgame.getnextquestion(); button nextbtn = (button) findviewbyid(r.id.nextbtn); nextbtn.setonclicklistener(this); setquestions(); tv = (textview) findviewbyid(r.id.tv_counter); mprogressbar=(progressbar)findviewbyid(r.id.progressbar); mprogressbar.setprogress(i); mcountdowntimer=new countdowntimer(5000,1000) { @override public void ontick(long millisuntilfinished) { log.v("log_tag", "tick of progress"+ i+ millisuntilfinished); i++; string time = (string) dateformat.format("ss", new date(millisuntilfinished)); tv.settext(time); mprogressbar.setprogress(i); } @override public void onfinish() { //do want i++; mprogressbar.setprogress(i); } }; mcountdowntimer.start(); } private void setquestions() { //set question text current question string question = utility.capitalise(currentq.getquestion()) + "?"; textview qtext = (textview) findviewbyid(r.id.question); qtext.settext(question); //set available options list<string> answers = currentq.getquestionoptions(); textview option1 = (textview) findviewbyid(r.id.answer1); option1.settext(utility.capitalise(answers.get(0))); textview option2 = (textview) findviewbyid(r.id.answer2); option2.settext(utility.capitalise(answers.get(1))); textview option3 = (textview) findviewbyid(r.id.answer3); option3.settext(utility.capitalise(answers.get(2))); textview option4 = (textview) findviewbyid(r.id.answer4); option4.settext(utility.capitalise(answers.get(3))); } @override public void onclick(view arg0) { /** * validate checkbox has been selected */ if (!checkanswer()) return; /** * check if end of game */ if (currentgame.isgameover()){ intent = new intent(this, endgameactivity.class); startactivity(i); finish(); } else{ intent = new intent(this, questionactivity.class); startactivity(i); finish(); } } @override public boolean onkeydown(int keycode, keyevent event) { switch (keycode) { case keyevent.keycode_back : return true; } return super.onkeydown(keycode, event); } private boolean checkanswer() { string answer = getselectedanswer(); if (answer==null){ return false; } else { if (currentq.getanswer().equalsignorecase(answer)) { currentgame.incrementrightanswers(); } else{ currentgame.incrementwronganswers(); } return true; } } private string getselectedanswer() { radiobutton c1 = (radiobutton)findviewbyid(r.id.answer1); radiobutton c2 = (radiobutton)findviewbyid(r.id.answer2); radiobutton c3 = (radiobutton)findviewbyid(r.id.answer3); radiobutton c4 = (radiobutton)findviewbyid(r.id.answer4); if (c1.ischecked()) { if (currentq.getanswer().equalsignorecase(c1.gettext().tostring())) { c1.settextcolor(color.green); } else { c1.settextcolor(color.red); if (currentq.getanswer().equalsignorecase(c2.gettext().tostring())) { c2.settextcolor(color.green); } else if (currentq.getanswer().equalsignorecase(c3.gettext().tostring())) { c3.settextcolor(color.green); } else if (currentq.getanswer().equalsignorecase(c4.gettext().tostring())) { c4.settextcolor(color.green); } } return c1.gettext().tostring(); } if (c2.ischecked()) { if(currentq.getanswer().equalsignorecase(c2.gettext().tostring())) { c2.settextcolor(color.green); } else { c2.settextcolor(color.red); if (currentq.getanswer().equalsignorecase(c1.gettext().tostring())) { c1.settextcolor(color.green); } else if (currentq.getanswer().equalsignorecase(c3.gettext().tostring())) { c3.settextcolor(color.green); } else if (currentq.getanswer().equalsignorecase(c4.gettext().tostring())) { c4.settextcolor(color.green); } } return c2.gettext().tostring(); } if (c3.ischecked()) { if(currentq.getanswer().equalsignorecase(c3.gettext().tostring())) { c3.settextcolor(color.green); } else { c3.settextcolor(color.red); if (currentq.getanswer().equalsignorecase(c1.gettext().tostring())) { c1.settextcolor(color.green); } else if (currentq.getanswer().equalsignorecase(c2.gettext().tostring())) { c2.settextcolor(color.green); } else if (currentq.getanswer().equalsignorecase(c4.gettext().tostring())) { c4.settextcolor(color.green); } } return c3.gettext().tostring(); } if (c4.ischecked()) { if(currentq.getanswer().equalsignorecase(c4.gettext().tostring())) { c4.settextcolor(color.green); } else { c4.settextcolor(color.red); if (currentq.getanswer().equalsignorecase(c1.gettext().tostring())) { c1.settextcolor(color.green); } else if (currentq.getanswer().equalsignorecase(c2.gettext().tostring())) { c2.settextcolor(color.green); } else if (currentq.getanswer().equalsignorecase(c3.gettext().tostring())) { c3.settextcolor(color.green); } } return c4.gettext().tostring(); } return null; } } question.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal" android:background="@drawable/background"> <linearlayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingtop="15dip" android:paddingbottom="15dip" android:gravity="center_horizontal"> <imageview android:id="@+id/logo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/logo2" android:contentdescription="quiz_logo" /> </linearlayout> <linearlayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingtop="5dip" android:paddingbottom="5dip" android:gravity="center_horizontal" > <radiogroup android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/group1"> <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:textcolor="#ffffff" android:textstyle="bold" android:id="@+id/question"/> <radiobutton android:checked="false" android:id="@+id/answer1" /> <radiobutton android:checked="false" android:id="@+id/answer2" /> <radiobutton android:checked="false" android:id="@+id/answer3" /> <radiobutton android:checked="false" android:id="@+id/answer4" /> </radiogroup> </linearlayout> <linearlayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingtop="5dip" android:paddingbottom="5dip" android:gravity="center_horizontal"> <button android:text="next" android:id="@+id/nextbtn" android:layout_width="80dip" android:layout_height="wrap_content" android:background="@drawable/button_selector" android:paddingtop="5dip" android:paddingbottom="5dip" android:textcolor="#ffffff" /> </linearlayout> <linearlayout android:orientation="horizontal" android:layout_width="290.0dip" android:layout_height="wrap_content" android:paddingtop="5dip" android:paddingbottom="5dip" android:layout_margintop="50dp" android:gravity="center_horizontal" > <progressbar android:id="@+id/progressbar" android:max="5" android:progress="0" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1.0" style="@android:style/widget.progressbar.horizontal" /> <textview android:id="@+id/tv_counter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="5"/> </linearlayout> </linearlayout> 
in picture above question activity wherein want display , start countdown progress timer.in there 20 questions in quiz.
i want start timer question activity started , time interval 2 mins(120 sec) , want reduce value 1 sec continuously till reaches 0.
also want progress bar decrease 1 percent every second elapsed , value(text) displayed alongside progress bar should decrease simultaneously.
like 120 sec,progress bar 100% . should 99% 119 sec , on.
after reaching 0 , user should redirected results page(endgameactivity.java).
i willing mail code willing me in case of doubts in code.
when countdown stops need call set question function , start timer again. there onfinish() function countdowntimer. need call next question function.
2 issue. same final countdown timer. need intent result page countdown finish
Comments
Post a Comment