darthestvader
darthestvader

Reputation: 1

Android: How to use selector with a single item

I have a ToggleButton with text and I want to show a drawableStart next to it only if it's selected. When it isn't selected, I'd like to show just the text (the drawable should disappear).

I tried setting up my selector with just a single item for when android:state_checked=true but when I run the app it gets completely ignored. How can I do this?

Here's my code ToggleButton code:

<ToggleButton
            android:id="@+id/list_item_service_list_add_button"
            style="@style/OrangeToggleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="10dp"
            android:layout_marginBottom="10dp"
            android:textOff="@string/list_item_service_list_add"
            android:textOn="@string/list_item_service_list_added"
            android:button="@android:color/transparent"
            android:drawableStart="@drawable/add_to_cart_icon_selector"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />

and here's my add_to_cart_icon_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/ic_check_circle"
        android:state_checked="true" />
</selector>

Upvotes: 0

Views: 239

Answers (0)

Related Questions