0

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?

Anton Holovin
  • 5,513
  • 1
  • 31
  • 33

3 Answers3

5

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.

Amir Hossein Ghasemi
  • 20,623
  • 10
  • 57
  • 53
  • How would you do this programmatically? – tccpg288 May 20 '16 at 21:26
  • @tccpg288 I Use `setColorFilter(color)` in my view, but by setting this you cannot declare other states, so you should use `selector` s in your code. see this post: http://stackoverflow.com/questions/4697528/replace-selector-images-programmatically – Amir Hossein Ghasemi May 22 '16 at 07:13
2

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

Community
  • 1
  • 1
MojoTosh
  • 1,886
  • 1
  • 18
  • 23
  • Thank you. `colorAccent` works for checked state of the RadioButton. Is it possible to change color for unchecked state? Because if I use DrawableState, I should use images from android sdk for describing each state. I'm interested in the "realtime" coloring. – Anton Holovin Oct 25 '14 at 15:44
  • 1
    @AntonGolovin I've found that setting "colorControlNormal" in my Theme defines the color of the radiobutton when it's not checked. Of course, it's also applying that tint to other controls that use the theme. So, I'm not sure how to color/style the RB _only_ without creating a custom drawable. Note that you do NOT need an image for a custom radiobutton drawable, it can all be done with shapes pretty easily (second link above). You can create a couple of these ahead of time and switch them out at runtime. – MojoTosh Oct 25 '14 at 16:25
  • I found the solution: `@color/colorRes` `@color/colorRes` – Anton Holovin Oct 25 '14 at 19:28
  • @MojoTosh Hi, did you find a solution for the problem with the default colour of radio buttons? I have same problem as you, if I use the colorControlNormal I get other components also tinted and that's not good. – Roberto Feb 03 '15 at 13:36
2

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"/>
saulmm
  • 2,398
  • 15
  • 21