java - How to set Timer when button clicked? -
i mean stopwach, when button clicked timer on until stop button pressed
startbutton= (button)findviewbyid(r.id.black1); startbutton.setonclicklistener(new view.onclicklistener() { public void onclick(view v){ //start timer } }); stopbutton= (button)findviewbyid(r.id.black1); stopbutton.setonclicklistener(new view.onclicklistener() { public void onclick(view v){ //stop timer } });
and second question,
if timer show 90 second how make show imageview or button in screen? if statement make button visible each timer count 90 second (90, 180, 270, etc..) set button visibility visible.
thanks before.
use chronometer in xml
<chronometer android:id="@+id/chronometer1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="chronometer" />
in java
chronometer focus = (chronometer) findviewbyid(r.id.chronometer1); startbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { focus.start(); setvisibilitytimeron(); //second question solution } }); stopbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { focus.stop(); setvisibilitytimeroff(); } });
second question if want turn visibility on/off of button/imageview set handler
//declare these variable private handler handler; private runnable updateview; private void setvisibilitytimeron(){ timehandler = new handler(); //it's better if declare line in oncreate (becuase if user press stopbutton first before pressing startbutton error occur handler never initialized , try calling removecallback function) updateview = new runnable() { public void run() { someimageview.setvisibility(view.visible); } }; handler.postdelayed(updateview ,90000);//this on after 90 second } private void setvisibilitytimeroff(){ handler.removecallbacks(updateview); }
Comments
Post a Comment