Search code examples
javaandroidbuttoncounterrestart

How do I add a button that will restart both of my counters to 0?


I want to add a button that when I press it, it will retart both TextViews back to 0. How do i do that? Thank you in advance. I would greatly appreciate your help.

final TextView text = (TextView) findViewById(R.id.textView2);
text.setText("");
final ImageButton button1 = (ImageButton)findViewById(R.id.imageButton2);
button1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        clicked1++;
        text.setText("  " + clicked1 + " SHOTS ");
    }

});
}

final TextView text = (TextView) findViewById(R.id.textView1);
text.setText("");
final ImageButton button2 = (ImageButton)findViewById(R.id.imageButton1);
button2.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        clicked2++;
        text.setText("  " + clicked2 + " CUPS ");
    }

});

}
}

Solution

  • you have to clear both counters in the onClick of the third button, and set both textviews with the new cleared counters (zero).

    final ImageButton buttonClearCounter = (ImageButton)findViewById(R.id.imageButton);
        button.setOnClickListener(new View.OnClickListener() {
    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            clicked1 = 0;
            clicked2 = 0;
            text1.setText("  " + clicked1 + " SHOTS");
            text2.setText("  " + clicked2 + " CUPS");
    
        }