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); } };
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.
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.
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.
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.
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".
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.
<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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With