Issue
I am implementing a timer in android with one text view and one button for start/stop.
How do I set register different events on clicklistener of the same button, such that when it is clicked the first time it will start a timer and when clicked a second time it will stop the timer and report the time between events? I am implementing a timer in android with one text view and one button for start/stop.
How do I set register different events on clicklistener of the same button, such that when it is clicked the first time it will start a timer and when clicked a second time it will stop the timer and report the time between events?
Edit1
what i did is,
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_depth);
findViewById(R.id.btn).setOnClickListener(this);
}
boolean showingFirst = true;
public void generate(View view){
if(showingFirst){
long startTime = System.currentTimeMillis();
showingFirst = false;
}else{
long difference = System.currentTimeMillis() - startTime;
showingFirst = true;
TextView myText = (TextView)findViewById(R.id.tv);
myText.setText(String.valueOf(difference));
}
}
but since long starttime is started in if when the control enters else loop it shows cannot resolve symbol 'startTime'
please help and special thanks to eliamyro
Solution
You can do it using a global boolean isStart
and start or stop the timer depending on the value of the isStart
.
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isStart) {
// Stop timer
isStart = false;
} else {
// Start timer
isStart = true;
}
}
});
Answered By - eliamyro
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.