user1205415
user1205415

Reputation: 605

Android Imagebutton change Image OnClick

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

    }
};

Upvotes: 38

Views: 141326

Answers (7)

Aathil Ahamed
Aathil Ahamed

Reputation: 474

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

Upvotes: 1

Oksana Kryvenko
Oksana Kryvenko

Reputation: 61

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

Upvotes: 3

Yassine Souabni
Yassine Souabni

Reputation: 751

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.

Upvotes: 70

misman
misman

Reputation: 1385

<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>

Upvotes: 16

John Smith
John Smith

Reputation: 51

You can do it right in your XML file:

android:onClick="@drawable/ic_action_search"

Upvotes: 3

hsz
hsz

Reputation: 152304

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

Upvotes: 2

Carnal
Carnal

Reputation: 22064

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

Upvotes: 10

Related Questions