Anton Holovin
Anton Holovin

Reputation: 5673

Change widgets color. Appcompat 21

Is it possible to change color for particular widget with appcompat 21? Actually I'm interested in RadioButton color.

I read that it's possible on api 21+. But what about old apis?

Upvotes: 0

Views: 6136

Answers (3)

saulmm
saulmm

Reputation: 2438

Nice answer above! On the other hand, here you can find a doc of how to style RadioButtons

http://www.materialdoc.com/radio-button/

enter image description here

I. Declare custom style in your styles.xml file.

<style name="MyRadioButton" parent="Theme.AppCompat.Light">  
    <item name="colorControlNormal">@color/indigo</item>
    <item name="colorControlActivated">@color/pink</item>
</style>  

II. Apply this style to your RadioButton via android:theme attribute.

<RadioButton  
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:text="Radio Button"
    android:theme="@style/MyRadioButton"/>

Upvotes: 2

Amirhossein Ghasemi
Amirhossein Ghasemi

Reputation: 22138

100% Working

Just create a style for your View and change colorPrimary and colorAccent like below:

<style name="RadioButtonTeal" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorAccent">@color/md_red_600</item>
    <item name="colorPrimary">@color/md_teal_600</item>
</style>

Then simply add this style to your AppCompatRadioButton:

<android.support.v7.widget.AppCompatRadioButton
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:theme="@style/RadioButtonTeal" />

remember your View should created from AppCompatLibrary like AppCompatRadioButton.

Upvotes: 5

MojoTosh
MojoTosh

Reputation: 1886

If you use appcompat-v7 (rev 21) and extend your theme from Theme.Appcompat, your RadioButton will automatically get the tinting from your extended theme's settings for "color*". For example, the "checked" radiobutton will show with the "colorAccent" value set in your theme.

I'm not sure if the following will change in a later release of appCompat (see the FAQ from this post by Chris Banes: https://chris.banes.me/2014/10/17/appcompat-v21/), but for now, if you want to set the colors of a radiobutton explicitly, you can still create an appropriate statelistdrawable. This will use whatever colors you set. See the following SO answer for an excellent example of this: https://stackoverflow.com/a/19163987/2259418

Upvotes: 2

Related Questions