-1

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 ");
    }

});

}
}
Celeo
  • 5,583
  • 8
  • 39
  • 41
  • What is with the code being cut out? Please provide a [sscce](http://sscce.org/). – Mr. Polywhirl Nov 05 '14 at 23:28
  • 2
    Okay, you've already added to buttons that do something, what is the problem with adding their third and setting `clicked1` and `clicked2` to `0`? – MadProgrammer Nov 05 '14 at 23:28
  • How will the final source code look like? can you please provide me with it? – Eduard Castellano Nov 05 '14 at 23:37
  • Hmmmm. This sounds a bit like a homework question. It would be a useful exercise to follow some advice and work it out (IMHO)! – CodeMonkey Nov 05 '14 at 23:39
  • You got the code you pasted here from [another question you asked](http://stackoverflow.com/questions/26765576/i-want-to-create-two-button-clickers-that-add-up-different-numbers), and still did not implement their recommendations (unique variable names etc.). And you haven't marked the correct answer as accepted. Help us to help you! – CodeMonkey Nov 05 '14 at 23:41
  • @MadProgrammer I know how to adding the third button but how do I set it so that clicked 1 and clicked 2 are 0? – Eduard Castellano Nov 05 '14 at 23:54

2 Answers2

1

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");

    }
Dici
  • 25,226
  • 7
  • 41
  • 82
Silvia H
  • 8,097
  • 7
  • 30
  • 33
  • Thank you so much for answering. I get the part that i need to set clicked1 to 0 and clicked 2 to 0 but the text part, when I put that in the onClick on my third button there is an error. – Eduard Castellano Nov 06 '14 at 00:00
  • yes that's because I was only naming them text1 and text2 to give you an example, you have to use the actual names of your 2 textViews. if you're naming both textViews as "text" like in your code snippet then that's what you should use, but better name each one a different name than the other. – Silvia H Nov 06 '14 at 00:06
-1
    {
        final Button button = (Button) findViewById(R.id.button1);
        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");

            }
        });
    }