I am make a image button .I need to change the image when I click on image button .Actually it change the background image but only for few seconds .why ?
here is my code
<ImageButton android:id="@+id/favorite"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/start"
android:background="#00ffffff"
/>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/on" /> <!-- pressed -->
<item android:drawable="@drawable/off" /> <!-- default -->
</selector>
I need to show on image when I click on image button ..? can I write on java side ? can I write on click listener of image button ?
I think it's obvious,those items in drawable are for states that ImageButton is pressed by user or in normal mode.
Yes, and yes again :D if you want to change it when user presses it, just do it like this :
XML layout:
<ImageButton android:id="@+id/favorite" android:layout_width="wrap_content" android:layout_height="fill_parent" android:src="@drawable/off" android:background="#00ffffff" />
Java side (eg. in you onCreate method)
change the source of the image by providing an absolute path or an internet url dynamically in the onClickListener of the image.
and also can be like this
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".
EDIT: