java - how to trigger timer from other class and its TextView and implementation in another xml and class respectively? -
i working on android quiz , want timer on each question-answer page. have menu page in quiz , play button start game. , want timer triggered when click on play button. have create textview in question xml represent menu page. , implementation in questionactivity class represent first question page. posting welcomeactivity class although not play role in question.
play button layout
<button android:text="play" android:id="@+id/playbtn" android:layout_width="80dip" android:layout_alignparentright="true" android:layout_height="wrap_content" android:paddingtop="5dip" android:paddingbottom="5dip" android:textcolor="#ffffff" android:background="@drawable/start_button" /> question xml representing textview timer
<textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/question" android:layout_centerhorizontal="true" android:background="@drawable/timer_bttn" android:onclick="onclick"/> questionactivity implemented timer code
public class questionactivity extends activity implements onclicklistener{ private question currentq; private gameplay currentgame; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.question); /** * configure current game , question */ currentgame = ((cykapplication)getapplication()).getcurrentgame(); currentq = currentgame.getnextquestion(); button nextbtn1 = (button) findviewbyid(r.id.answer1); nextbtn1.setonclicklistener(this); button nextbtn2 = (button) findviewbyid(r.id.answer2); nextbtn2.setonclicklistener(this); button nextbtn3 = (button) findviewbyid(r.id.answer3); nextbtn3.setonclicklistener(this); button nextbtn4 = (button) findviewbyid(r.id.answer4); nextbtn4.setonclicklistener(this); /** * update question , answer options.. */ setquestions(); } /** * method set text question , answers current games * current question */ 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))); int score = currentgame.getscore(); string scr = string.valueof(score); textview score1=(textview) findviewbyid(r.id.score); score1.settext(scr); } @override public void onclick(view arg0) { //log.d("questions", "moving next question"); if(!checkanswer(arg0)) return; /** * check if end of game */ if (currentgame.isgameover()) { //log.d("questions", "end of game! lets add scores.."); //log.d("questions", "questions correct: " + currentgame.getright()); //log.d("questions", "questions wrong: " + currentgame.getwrong()); 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); } /** * check if checkbox has been selected, , if * has check if correct , update gamescore */ private boolean checkanswer(view v) { button b=(button) v; string answer = b.gettext().tostring(); //log.d("questions", "valid checkbox selection made - check if correct"); if (currentq.getanswer().equalsignorecase(answer)) { //log.d("questions", "correct answer!"); currentgame.incrementscore(); } else { //log.d("questions", "incorrect answer!"); currentgame.decrementscore(); } return true; } public void settimer() { long finishtime = 5; countdowntimer countertimer = new countdowntimer(finishtime * 1000, 1000) { public void onfinish() { //code 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(); } } welcomeactivity
public class welcomeactivity extends activity implements onclicklistener{ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.welcome); ////////////////////////////////////////////////////////////////////// //////// game menu ///////////////////////////////////////////////// button playbtn = (button) findviewbyid(r.id.playbtn); playbtn.setonclicklistener(this); button settingsbtn = (button) findviewbyid(r.id.settingsbtn); settingsbtn.setonclicklistener(this); button rulesbtn = (button) findviewbyid(r.id.rulesbtn); rulesbtn.setonclicklistener(this); button exitbtn = (button) findviewbyid(r.id.exitbtn); exitbtn.setonclicklistener(this); } /** * listener game menu */ @override public void onclick(view v) { intent i; switch (v.getid()){ case r.id.playbtn : //once logged in, load main page //log.d("login", "user has started game"); //get question set // list<question> questions = getquestionsetfromdb(); //initialise game retrieved question set /// gameplay c = new gameplay(); c.setquestions(questions); c.setnumrounds(getnumquestions()); ((cykapplication)getapplication()).setcurrentgame(c); //start game now.. // = new intent(this, questionactivity.class); startactivityforresult(i, constants.playbutton); break; case r.id.rulesbtn : = new intent(this, rulesactivity.class); startactivityforresult(i, constants.rulesbutton); break; case r.id.settingsbtn : = new intent(this, settingsactivity.class); startactivityforresult(i, constants.settingsbutton); break; case r.id.exitbtn : finish(); break; } }
as mentioned piyush gupta, should call settimer() method onresume in questionactivity.
from android developer documentation:
(onresume is) called activity start interacting user. place begin animations, open exclusive-access devices (such camera), etc.
in code should make countertimer use in settimer() class member, not local variable; if don't go out of scope once settimer() call completes , access lost.
so need add following questionactivity:
public class questionactivity extends activity implements onclicklistener{ // new: add countertimer member private countdowntimer countertimer; // new: implement onresume @override public void onresume() { settimer(); super.onresume(); } // change: settimer should changed follows public void settimer() { long finishtime = 5; // note: use member, instead of local countertimer = new countdowntimer(finishtime * 1000, 1000) { public void onfinish() { //code 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(); } } this above example uses code now, advise create timer in oncreate using class member store (ie. in settimer(), except countertimer.start(); call. use countertimer.start(); in onresume. , maybe add countertimer.cancel() call onpause timer ends when activity loses focus.
Comments
Post a Comment