Question about tutorial 19 event listener

0 Steve Lu · April 20, 2015

A real beginner's question.
After I done the code like the tutorials says.
Is it possible to click the same button 2nd time to make the TEXT return to original TEXT?

        Button testButton = (Button)findViewById(;

                new Button.OnClickListener(){
                    public void onClick(View v){
                        TextView testText = (TextView)findViewById(;
                        testText.setText("TEXT CHANGED");


Post a Reply


Oldest  Newest  Rating
0 steven kitchener · April 21, 2015
above your constuctor write this line
private boolean isClicked = false;

This will create a variable that is accessible anywhere in this class. It creates a boolean (which is either true or false) and initially sets it to false, because the button has not been clicked yet. Then when we click the button for the first time we change it from false to true. And then again when the button is clicked change it from true to false.

If you are completely new to Java then i would recommend going through all of the "Java" and "Java Intermediate" tutorials before trying to tackle Android development. A solid base understanding of how Java works will help you greatly in the long run.

Good luck.
0 Steve Lu · April 21, 2015
Thank you so much for all the helps.
But  still not easy to understand by a beginner like me.

Can you explain how to do this step?
"First off create your variable, lets call it "isClicked" and make it false atm. 

I m a really really beginner. Only try to learn this by Bucky's video for 3 days...
Anyway,  I will keep those code and try it later.
0 steven kitchener · April 20, 2015
Hey Steve,

This can easily be achieved by adding in a boolean variable that you change every time the button is clicked (think about toggle buttons).

First off create your variable, lets call it "isClicked"  and make it false atm. The following code will give you your desired effect.

public void onClick(View v){

//change the boolean var
if(isClicked == true){
isClicked = false;
isClicked = true;

//grab the TextView like normal
TextView testText = (TextView)findViewById(;

//change the text depending on the boolean
testText.setText("TEXT CHANGED");
testText.setText("Whatever the original text was");

This code can be condensed a little bit but i want you to be happy with the implementation of it before we start optomizing the code. 

Im unable to try this code out at the moment as my laptop has broken but it should work. If you have any questions about the code or if you want to learn how to condense it further feel free to ask 


0 Nicholas Eason · April 20, 2015
Yeah, make a clicked variable and increment it every time it's clicked. Then:

if(click%2 == 0)
testText.setText(//original text here);
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator