All of my code works fine no errors, button click increases textview by 1 every click and starts the chronometer.
public class MainActivity extends Activity{
TextView txtCount;
Button btnCount;
int count=0;
Chronometer chrono;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chrono=(Chronometer) findViewById(R.id.chronometer);
txtCount=(TextView) findViewById(R.id.textView);
btnCount=(Button)findViewById(R.id.button);
btnCount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//onclick increase textView by 1
count++;
txtCount.setText(String.valueOf(count));
//on button click start the chronometer at 00:00
btnCount.setEnabled(true);
chrono.setBase(SystemClock.elapsedRealtime());
chrono.start();
}
});
}}
...but, as my code reads the chronometer is reset and started every button click. is there a way to start the chronometer on the 1st button click and then continue to use the same button to increase textview but have no interaction with the chronometer widget?
Just use a boolean to check if your chronometer is started already ?
boolean mIsStarted = false;
...
btnCount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//onclick increase textView by 1
count++;
txtCount.setText(String.valueOf(count));
//on button click start the chronometer at 00:00
btnCount.setEnabled(true);
if (!mIsStarted) {
chrono.setBase(SystemClock.elapsedRealtime());
chrono.start();
mIsStarted = true;
}
}