android - how to get value from countdowntimer and display into toast -
android - how to get value from countdowntimer and display into toast -
i have countdown timer 1 9999 if click start button count start, if click stop button need current value countdown , display value in toast countdown not stop if click stop button please help me
private countdowntimer countdowntimer; private boolean timerhasstarted = false; private button startb; public textview ; private final long starttime = 9999 * 1; private final long interval = 1 *1 ; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); startb = (button) this.findviewbyid(r.id.button); startb.setonclicklistener(this); text = (textview) this.findviewbyid(r.id.timer); countdowntimer = new mycountdowntimer(starttime, interval); text.settext(text.gettext() + string.valueof(starttime / 1)); } public void onclick(view v) { if (!timerhasstarted) { countdowntimer.start(); timerhasstarted = true; startb.settext("stop"); } else { /*countdowntimer.cancel(); timerhasstarted = false; startb.settext("restart");*/ } } public class mycountdowntimer extends countdowntimer { public mycountdowntimer(long starttime, long interval) { super(starttime, interval); } @override public void onfinish() { //text.settext("time's up!"); countdowntimer.start(); } @override public void ontick(long millisuntilfinished) { text.settext("" + millisuntilfinished / 1); } }
thank you
here countdown timer:
questioncountdowntimer
public class questioncountdowntimer extends countdowntimer { private textview remainingtimedisplay; private context context; public questioncountdowntimer(context context,long millisinfuture, long countdowninterval,textview remainingtimedisplay) { super(millisinfuture, countdowninterval); this.context = context; this.remainingtimedisplay = remainingtimedisplay; } @override public void ontick(long millisuntilfinished) { long millis = millisuntilfinished; string hms = string.format("%02d:%02d", timeunit.milliseconds.tominutes(millis) - timeunit.hours.tominutes(timeunit.milliseconds.tohours(millis)), timeunit.milliseconds.toseconds(millis) - timeunit.minutes.toseconds(timeunit.milliseconds.tominutes(millis))); remainingtimedisplay.settext(hms); } @override public void onfinish() { toast.maketext(context,"countdown finish :)",toast.length_short).show(); } }
note: textview remainingtimedisplay
remainingtimedisplay.settext(hms); utilize display remaining time using textview
here phone call timer:
//start quiz timer questioncountdowntimer timer = new questioncountdowntimer(this,10000, 1000, remainingtimedisplay); timer.start();
-first parameter: - utilize context show toast message
-second parameter: 10000 - total time (10 sec)
-third parameter: 1000 - countdown interval (1 sec)
-last parameter: dispaly remaining time in real time
tested , working
android countdowntimer
Comments
Post a Comment