38

I just added a new drawable folder under res folder. In the drawable folder, i copied the ic_launcher.png file from drawable-hdpi folder. I wanna change the standard ImageButton image through the new one when i press the button. I wrote some code, but when i start the app, it crashes.

Button imgButton; 

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    findViewById(R.id.imgButton).setOnClickListener(imgButtonHandler);      
}

View.OnClickListener imgButtonHandler = new View.OnClickListener() {

    public void onClick(View v) {

        imgButton.setBackgroundResource(R.drawable.ic_launcher);

    }
};

EDIT: I changed to this, and this also not works.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imgButton = (Button) findViewById(R.id.imgButton);
    imgButton.setOnClickListener(imgButtonHandler);
}


View.OnClickListener imgButtonHandler = new View.OnClickListener() {

    public void onClick(View v) {
        imgButton.setBackgroundResource(R.drawable.ic_launcher);

    }
};

EDIT 2: THIS WORKS. Thanks to all.

ImageButton button;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button= (ImageButton)findViewById(R.id.imgButton);
    button.setOnClickListener(imgButtonHandler);
}


View.OnClickListener imgButtonHandler = new View.OnClickListener() {

    public void onClick(View v) {
        button.setBackgroundResource(R.drawable.ic_launcher);

    }
};
raaaay
  • 496
  • 7
  • 14
user1205415
  • 605
  • 1
  • 7
  • 22

7 Answers7

70

This misled me a bit - it should be setImageResource instead of setBackgroundResource :) !!

The following works fine :

ImageButton btn = (ImageButton)findViewById(R.id.imageButton1);       
 btn.setImageResource(R.drawable.actions_record);

while when using the setBackgroundResource the actual imagebutton's image stays while the background image is changed which leads to a ugly looking imageButton object

Thanks.

raaaay
  • 496
  • 7
  • 14
Yassine Souabni
  • 751
  • 5
  • 3
16
<ImageButton android:src="@drawable/image_btn_src" ... />

image_btn_src.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/icon_pressed"/>
<item android:state_pressed="false" android:drawable="@drawable/icon_unpressed"/>
</selector>
misman
  • 1,347
  • 3
  • 21
  • 39
10

That is because imgButton is null. Try this instead:

findViewById(R.id.imgButton).setBackgroundResource(R.drawable.ic_action_search);

or much easier to read:

imgButton = (Button) findViewById(R.id.imgButton);
imgButton.setOnClickListener(imgButtonHandler);

then in onClick: imgButton.setBackgroundResource(R.drawable.ic_action_search);

Carnal
  • 21,744
  • 6
  • 60
  • 75
  • findViewById(R.id.imgButton).setBackgroundResource(R.drawable.ic_launcher); Ha... this worked. When i start it, it has the new image. Then i don't understand, why the code above is not working in the onClick? – user1205415 Sep 03 '12 at 14:22
  • It doesn't work because you have Button imgButton; declared.. but you don't initiate it a view, that's why. When using findViewById(...) you get the view of the Button (Button in this case), therefore it works. – Carnal Sep 03 '12 at 14:25
  • Look at my answer mate, I put it there for you. – Carnal Sep 03 '12 at 14:33
3

You can do it right in your XML file:

android:onClick="@drawable/ic_action_search"
Zoltan Toth
  • 46,981
  • 12
  • 120
  • 134
John Smith
  • 51
  • 7
  • OK, but what is, when i click the button again, i want the old image. Like a toggle button. – user1205415 Sep 03 '12 at 14:28
  • 3
    but doing this doesnt change the image rather crashes the activity with a log **' java.lang.IllegalStateException: Could not find a method res/drawable-hdpi/menu_mission_touch.png(View) in the activity class com.test.MainMenu for onClick handler on view class android.widget.ImageButton with id 'menu_mission' '** – elixir bash Oct 02 '14 at 15:18
  • it´s not possible! – Jorgesys Apr 06 '17 at 23:48
3

To switch between different images when the ImageButton is clicked I used a boolean like this:

ImageButton imageButton;
boolean buttonOn;

imageButton.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         if (!buttonOn) {
             buttonOn = true;
             imageButton.setBackground(getResources().getDrawable(R.drawable.button_is_on)); 
         } else {
             buttonOn = false;
             imageButton.setBackground(getResources().getDrawable(R.drawable.button_is_off));
         }
     }
});
  • hello...suppose im performing this one activity.user click to glow image... what if user goes from one activity to another and again coming back to activity one will the button remain as glow? – Wini Jan 18 '21 at 13:10
  • @Wini only if you will pass your button status to another activity and then retrieve it when you are back to this activity – Oksana Kryvenko Jan 19 '21 at 17:08
2

You have assing button to your imgButton variable:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imgButton = (Button) findViewById(R.id.imgButton);
    imgButton.setOnClickListener(imgButtonHandler);
}
hsz
  • 148,279
  • 62
  • 259
  • 315
1

It is very simple

public void onClick(View v) {

        imgButton.setImageResource(R.drawable.ic_launcher);

    }

Using set Background image resource will chanage the background of the button

Aathil Ahamed
  • 460
  • 4
  • 16