java - ProgressBar with a countdowntimer - Android -
i have 20 sec countdown timer working on trivia game. want add together progressbar
(not progressdialog
) screen. found developer guide android confusing. googled lots of examples , tried combine them code. right displays when run game empty bar no "progress" made during each question of game.
questionview.java
public class questionview extends activity { int correctanswers = 0; int wronganswers = 0; int reply = 0; int = 0; long score = 0; long starttime = 20000; long interval = 1000; long points; boolean timerhasstarted = false; string category; button answer1, answer2, answer3, answer4; textview question, pointcounter, questionnumber, timecounter; arraylist<question> queries; timer cdtimer; progressbar bar; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.questionviewmain); answer1 = (button)findviewbyid(r.id.answer1); answer2 = (button)findviewbyid(r.id.answer2); answer3 = (button)findviewbyid(r.id.answer3); answer4 = (button)findviewbyid(r.id.answer4); question = (textview)findviewbyid(r.id.question); category = getintent().getstringextra("category"); queries = getintent().getparcelablearraylistextra("queries"); pointcounter = (textview)findviewbyid(r.id.timer); questionnumber = (textview)findviewbyid(r.id.timeelapsedview); timecounter = (textview)findviewbyid(r.id.timecounter); cdtimer = new timer(starttime, interval); bar = (progressbar)findviewbyid(r.id.progressbar); bar.setindeterminate(false); bar.setmax(9); loadquestion(); } public void loadquestion() { bar.setprogress(i); if(i == 10) { endquiz(); } else { if(!timerhasstarted) { cdtimer.start(); timerhasstarted = true; } else { cdtimer.start(); timerhasstarted = false; } reply = queries.get(i).getcorrectanswer(); question.settext(queries.get(i).getquery()); answer1.settext(queries.get(i).geta1()); answer2.settext(queries.get(i).geta2()); answer3.settext(queries.get(i).geta3()); answer4.settext(queries.get(i).geta4()); answer1.setonclicklistener(new onclicklistener() { public void onclick(view arg0) { queries.get(i).setselectedanswer(0); if(answer == 0) { correctanswers++; nextquestion(); } else { wronganswers++; nextquestion(); } } }); answer2.setonclicklistener(new onclicklistener() { public void onclick(view arg0) { queries.get(i).setselectedanswer(1); if(answer == 1) { correctanswers++; nextquestion(); } else { wronganswers++; nextquestion(); } } }); answer3.setonclicklistener(new onclicklistener() { public void onclick(view arg0) { queries.get(i).setselectedanswer(2); if(answer == 2) { correctanswers++; nextquestion(); } else { wronganswers++; nextquestion(); } } }); answer4.setonclicklistener(new onclicklistener() { public void onclick(view arg0) { queries.get(i).setselectedanswer(3); if(answer == 3) { correctanswers++; nextquestion(); } else { wronganswers++; nextquestion(); } } }); } } public arraylist<question> getqueries() { homecoming queries; } public void nextquestion() { score = score + points; i++; loadquestion(); } public class timer extends countdowntimer { public void startcountdowntimer() { } public timer(long starttime, long interval) { super(starttime, interval); } @override public void onfinish() { if(i >= 9) { cdtimer.cancel(); endquiz(); } else { wronganswers++; nextquestion(); } } @override public void ontick(long millisuntilfinished) { timecounter.settext("time remaining: " + (millisuntilfinished / 100)); points = (millisuntilfinished / 100) / 2; pointcounter.settext("points remaining: " + points); if(i < 10) { questionnumber.settext("question " + (i + 1) + " of 10"); } } } public void endquiz() { intent intent = new intent(questionview.this, results.class); intent.putextra("correctanswers", correctanswers); intent.putextra("wronganswers", wronganswers); intent.putextra("score", score); intent.putparcelablearraylistextra("queries", queries); intent.putextra("category", category); startactivity(intent); } }
xml code
<progressbar android:id="@+id/progressbar" android:layout_height="wrap_content" android:layout_width="wrap_content" android:visibility="visible" style="@android:style/widget.progressbar.horizontal" />
what looking progressbar tick downwards until 20 seconds user allotted each question gone.
new answer let's add together new line ontick()
:
bar.setprogress((int) math.round(millisuntilfinished / 1000.0));
(you may need tweak info types, away compiler... don't countdowntimer class, inaccurate , skips sec lastly number. wrote alternate class here: android countdowntimer - additional milliseconds delay between ticks)
original answer have couple pointers:
have defined maximum value progressbar?
bar.setmax(9);
i suggest loading i
progress instead of constant value of 10:
bar.setprogress(i);
if still not see progress ensure not in indeterminate mode:
bar.setindeterminate(false);
(this assume using progressbar can depict progress.)
addition move code oncreate()
:
bar = (progressbar)findviewbyid(r.id.progressbar); bar.setindeterminate(false); // may not necessary bar.setmax(9);
then move line loadquestion()
:
bar.setprogress(i);
otherwise progress never updated since create 1 countdowntimer , never phone call startcountdowntimer()
.
java android progress-bar countdowntimer
No comments:
Post a Comment