Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);      } }; 
like image 772
user1205415 Avatar asked Sep 03 '12 14:09

user1205415


People also ask

How to change image when button is clicked in Android studio?

You can add images from outside or choose the images available inside android studio. To add images, goto res, right click on drawable folder, select new, goto ImageAsset and select the images you want.

What is an imagebutton in Android?

In android, Image Button is a user interface control which is used to display a button with image and to perform an action when user click or tap on it. By default, the ImageButton looks same as normal button and it perform an action when user click or touches it, but only difference is we will add a custom image to the button instead of text.

How to define button click programmatically in Android?

To define button click programmatically, create View.OnClickListener object and assign it to the button by calling setOnClickListener (View.OnClickListener) like as shown below. This is how we can handle ImageButton click events in android applications based on our requirements.

How to create imagebutton control dynamically in an activity file?

Following is the example of creating ImageButton control dynamically in an activity file. Generally, whenever the user clicks on ImageButton, the ImageButton object will receives an on-click event. In android, we can define button click event in two ways either in XML layout file or create it in Activity file programmatically.

Why does the background image change when the button is pressed?

That's why you are using a selector for your button background and the image changes depending on the state of the button. If it is pressed the image will be "on" and in its normal state (no pressed and no focused) the image will be "off".


Video Answer


2 Answers

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.

like image 97
Yassine Souabni Avatar answered Sep 18 '22 03:09

Yassine Souabni


<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> 
like image 31
misman Avatar answered Sep 19 '22 03:09

misman