2

I'm trying to make an ImageView button toggle when I click on it. I've got the below code:

    ImageView button01 = (ImageView) findViewById(R.id.button01);
    button01.setOnClickListener(new OnClickListener() {
        int button01pos = 0;
        public void onClick(View v) {
            if (button01pos == 0) {
                button01.setImageResource(R.drawable.image01);
                button01pos = 1;
            } else if (button01pos == 1) {
                button01.setImageResource(R.drawable.image02);
                button01pos = 0;
            }
        }
    });

But for some reason button01 is underlined in red in Eclipse and it gives the error:

Cannot refer to a non-final variable button01 inside an inner class defined in a different method

Does anyone know why it's doing this and how to fix it?

Thanks

  • Add final in front of ImageView button01... "final ImageView button01". done. – dymmeh May 03 '12 at 19:25
  • Oh sorry it worked... But now for some reason when I click twice on the button it stops toggling and Eclipse throws this error: ActivityManager: Warning: Activity not started, its current task has been brought to the front –  May 03 '12 at 19:31
  • That just means it started your app that's already installed and brought it to the front. Not really an error. Kinda misleading. – dymmeh May 03 '12 at 19:32
  • It toggles once then never works again. The reason for this is because "int button01pos = 0;" is placed inside the onClickListener so it resets to 0 then just stays on 0. If I move it outside the onClickListener then it needs to be final which makes it impossible to toggle with. –  May 03 '12 at 19:37
  • It shouldn't stay on 0. The onClickListener gets initialized only once so it will never get "reset" to 0. The only way that would happen is if you have a listview of items where they get scrolled through and reinitialized while scrolling. I tested this myself on my device and it works fine using your code. – dymmeh May 03 '12 at 19:42
  • Sorry, you're right - it works perfectly - I forgot to put "button01pos = 0;" in the "else if" on my local machine. –  May 03 '12 at 19:51
  • Just out of interest - How can one change global variables from within a final listener? –  May 03 '12 at 20:00
  • A global var should be able to be modified without declaring it final. As long as they are member variables in your class it should work as normal – dymmeh May 03 '12 at 20:08
  • If I create a new global variable "int toggleCounter = 0" then try to put "toggleCount++;" inside "public void onClick(View v)" in the listener then it underlines "toggleCount++;" in red and says: "Cannot refer to a non-final variable toggleCount inside an inner class defined in a different method". –  May 03 '12 at 20:12
  • you likely aren't declaring the variable in the right place, then – dymmeh May 03 '12 at 20:15
  • true true... I moved it to just under my public class definition and now it works fine :) –  May 03 '12 at 20:17

3 Answers3

7

Here is the working code:

final ImageView button01 = (ImageView) findViewById(R.id.button01);
button01.setOnClickListener(new OnClickListener() {
    int button01pos = 0;
    public void onClick(View v) {
        if (button01pos == 0) {
            button01.setImageResource(R.drawable.image01);
            button01pos = 1;
        } else if (button01pos == 1) {
            button01.setImageResource(R.drawable.image02);
            button01pos = 0;
        }
    }
});
0

Try this, it worked for me. Here checkbox visibility is set to "Invisible"...! this code is inside a button OnClickListener...!

@Override
public void onClick(View v) {

    ImageView iv_icon = (ImageView) findViewById(R.id.icon);

    CheckBox cb = (CheckBox) findViewById(R.id.cb);

    if (cb.isChecked()) {
        iv_icon.setImageResource(R.drawable.image01);
        cb.setChecked(false);
    } else if (!cb.isChecked()) {
        iv_icon.setImageResource(R.drawable.image02);
        cb.setChecked(true);
    } else {
        // Nothing happens
    }
}
fawaad
  • 341
  • 6
  • 12
0

Try this,

        int button01pos = 0;

        ImageView button01 = (ImageView) findViewById(R.id.button01);
        button01.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            if (button01pos == 0) {
                button01.setImageResource(R.drawable.image01);
                button01pos = 1;
            } else if (button01pos == 1) {
                button01.setImageResource(R.drawable.image02);
                button01pos = 0;
            }
        }
    });
donmj
  • 379
  • 7
  • 13