Gleb Glazyrin
Gleb Glazyrin

Reputation: 87

RadioButton with vector drawables

I'm trying to make gender switcher using RadioButtons. Now it looks like this.

I use vector drawables as icons. I want to make it @color:colorAccent and change its background to a white circle, when button is pressed. How would be better to do it?

layout.xml

<RadioGroup
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/radioButton"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:layout_weight="1"
                android:layout_marginEnd="8dp"
                android:button="@drawable/custom_btn_radio"/>

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/radioButton2"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:layout_weight="1"
                android:layout_marginStart="8dp"
                android:button="@drawable/custom_btn_radio"/>

        </LinearLayout>
    </RadioGroup>

custom_btn_radio.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
    android:drawable="@drawable/human_male"/>
<item android:state_checked="true"
    android:drawable="@drawable/human_male"
    android:background="@android:color/white"
    android:tint="@color/colorAccent"/>

Upvotes: 1

Views: 1235

Answers (1)

weston
weston

Reputation: 54781

You should use a <selector> to select different drawables for different states.

Then for the color, as you are using a vector drawable, you can animate properties of it: AnimVector So you could be able to animate the color, even if it's a zero duration animation (in theory, it's not something I have done).

Upvotes: 1

Related Questions