Reputation: 169
I mean is like stopwach, when button clicked the Timer is on until the stop button pressed
startbutton= (Button)findViewById(R.id.black1);
startbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
//Start the timer
}
});
stopbutton= (Button)findViewById(R.id.black1);
stopbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
//Stop the timer
}
});
and second question,
if timer show 90 second how to make it show imageview or button in screen? like some if statement to make button visible each timer count to 90 second (90, 180, 270, etc..) he will set button visibility to visible.
Thanks before.
Upvotes: 1
Views: 2216
Reputation: 3873
This works like stop watch timer with the interval of 1ms.
Handler handler= new Handler();
1. click to start (code)
Runnable runnable = new Runnable() {
@Override
public void run() {
//put your code to be executed on within every interval
handler.postDelayed(this, 1);
}
};
handler.postDelayed(runnable, 5); //start after 5 seconds
2. click to stop (code)
handler.removeCallbacksAndMessages(null);
It should work. Thanks
Upvotes: 0
Reputation: 4375
Use chronometer in your xml
<Chronometer
android:id="@+id/chronometer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chronometer" />
in your 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 you want to turn VISIBILITY on/off of some button/ImageView set up a handler
//Declare these variable
private Handler handler;
private Runnable updateView;
private void setVisibilityTimerOn(){
timeHandler = new Handler(); //it's better if you declare this line in onCreate (becuase if user press stopButton first before pressing startButton error will occur as handler was never initialized and you try calling removeCallback function)
updateView = new Runnable() {
public void run() {
someImageView.setVisibility(View.VISIBLE);
}
};
handler.postDelayed(updateView ,90000);//this will be on after 90 second
}
private void setVisibilityTimerOff(){
handler.removeCallbacks(updateView);
}
Upvotes: 5