Reputation: 453
I am using Image selector for changing my tab-widget image icons , when I selected any one of image view, image of that image view should change. For that i have used following code, but it is working good , but problem is android:state_pressed="true" is working but android:state_selected="true" not working in any image view , i am getting stuck from this issue , can anyone help me, answer will be appreciable.Thanks in advance.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_contacts_vippie_selected" android:state_selected="true"/>
<item android:drawable="@drawable/ic_contacts_vippie"/>
Here is my Imageview:
<ImageView
android:id="@+id/hiiMoblieContact"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/flexi_contact_selector" />
Upvotes: 0
Views: 3123
Reputation: 2812
Instead of image view use toggle button or check-box or radio button. For example I am using check box.
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/hiiMoblieContact"
android:button="@drawable/flexi_contact_selector"/>
flexi_contact_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_contacts_vippie_selected" android:state_checked="true" />
<item android:drawable="@drawable/ic_contacts_vippie" />
</selector>
Hope this will help you..
Upvotes: 2
Reputation: 2319
Imageview doesn't save states. But don't worry, there is a workaround. Either use toggleButtons
and set background or use CheckableLinearlayout
as parent of imageview. Xml of imageview should be like this-
<package_name.CheckableLinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/hiiMoblieContact"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="false"
android:duplicateParentState="true"
android:src="@drawable/flexi_contact_selector"/>
</package_name.CheckableLinearLayout>
The changes are highlighted in bold. Now you can handle the checkablelinearlayout's set checkedmethod. If for example you set the checkacle linearlayout's set checked true, the image will automatically change. You will have to make this CheckableLinearLayout
class in your project and its link is - http://developer.android.com/intl/es/samples/CustomChoiceList/src/com.example.android.customchoicelist/CheckableLinearLayout.html
Upvotes: 1