This is in reference to the question asked in the following link

Android ImageButton with a selected state?

How will we use this as a selector? if we have 4 background images eg:

select_all_up.png, select_all_down.png deselect_all_up.png, deselect_all_down.png

I Have already checked it with state_selected attribute but it doesn't seem to work. Another option was to use checkbox instead of an image button, but i was still researching if we have an alternative.???

I am just a beginner in Android, just discovering how things work. Wished to have help in this topic. Thanks in Advance.

This is the selector file i wrote for it:

<item android:drawable="@drawable/select_all_down" android:state_pressed="true" android:state_selected="false"/>
<item android:drawable="@drawable/select_all_up" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@drawable/deselect_all_down" android:state_pressed="true" android:state_selected="true"/>
<item android:drawable="@drawable/deselect_all_up" android:state_pressed="false" android:state_selected="true"/>

and have used this drawable as a background for an image button:

. . . .

    <ImageButton
        android:id="@+id/selectAllImageButton"
        android:layout_width="300dp"
        android:layout_height="35dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/buttonanim_select_all_button"
        android:contentDescription="@string/todo"/>

. . . .

I wish to handle this issuein XML file itself rather than coming to code for Handling it.

有帮助吗?

解决方案

I got a way around with it, making use of ToggleButton concept. But still wonder why the ImageView could not get these properties.

<ToggleButton
        android:id="@+id/selectAllImageButton"
        android:layout_width="300dp"
        android:layout_height="35dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/buttonanim_select_all_button"
        android:contentDescription="@string/todo"
        android:textOn="@string/empty_text"
        android:textOff="@string/empty_text"/>

Still.. It works well, to my satisfaction.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top