0

this is my code

final boolean identifier = true;
        if (i2 > i){
            if (identifier){
                v5.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        identifier = false;
                    }
                });
            }
            if (!identifier){
                v5.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        identifier = true;
                    }
                });
            }
        }

but this doesnt seem to be working out fine. what i want is when v5 is clicked it will run a code that will change v5 onClicklistener

5
  • 1
    Android change value of variable on click : why identifier is final ? Commented Dec 22, 2016 at 12:42
  • why 2 onclicklistner? Commented Dec 22, 2016 at 12:43
  • im trying to make my view act as a switch on off. on the first click it will show contents and then on second click it will hide contents. the cycle continues like odd even Commented Dec 22, 2016 at 12:48
  • @ρяσѕρєя K it needs to be final because it was access inside onClickListener Commented Dec 22, 2016 at 12:50
  • final boolean identifier = true; would not allow to change value remove final Commented Dec 22, 2016 at 13:02

4 Answers 4

2

Try this.

  boolean identifier = false;

  v5.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    if(!identifier){
                     // first click
                     identifier=true;
                    }else{
                    // secondclick
                     identifier=false;
                    }
                }
            });
Sign up to request clarification or add additional context in comments.

8 Comments

still the same. cant call identifier inside onClick if its not final and final variable doesnt seem to allow change of variable
make the identifier public and then try
@GilbertMendoza define you identifier outside the method(from onCreate).
sorry this code is within a void. i didnt post full code but i will try and if it works ill mark ur answer as correct
@GilbertMendoza then define outside the void.
|
2
  1. Remove final from your identifier declaration, and make it a class private field so you can access it from your OnClickListener.
  2. Set only one OnClickListener and have it check the value of identifier, like so:

    v5.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            identifier = !identifier; // switch the value to false/true
            // update your UI
        }
    });
    

Comments

0
int identifier = 0;
        if (i2 > i){ 
            if (identifier=0){
                v5.setOnClickListener(new View.OnClickListener(){
                    @Override 
                    public void onClick(View v) {
                        identifier = 1;
                    } 
                }); 
            } 
            if (identifier!=0){
                v5.setOnClickListener(new View.OnClickListener() {
                    @Override 
                    public void onClick(View v) {
                        identifier = 0;
                    } 
                }); 
            } 
        } 

1 Comment

identifier needs to be final and final variable cannot change its value
0

Initialize the identifier boolean before the onCreate() method. Also don't make it final. A value of any Final variable cannot be modified.

boolean identifier = true;
...

onCreate() {
...
onClick() {
identifier = false;
//your code
...
 }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.