Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android appcompat-v7:21.0.0 change material checkbox colors

I've updated my project to use the latest appcompat support library, the new version uses material design checkboxes and radio buttons. My app is dark themed and the checkboxes are black which is hard to see. I'm trying to change their colors according to Maintaining Compatibility but so far nothing works.

res/values/styles.xml

  <style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">

    <!-- customize the color palette -->
    <item name="colorAccent">@color/silver</item>
  </style>

in build.gradle:

    android {
      compileSdkVersion 21
      buildToolsVersion '21.1.1'

      defaultConfig {
        minSdkVersion 9
        targetSdkVersion 19
      }
    }

.....
.....    

    compile 'com.android.support:appcompat-v7:21.0.0'

AndroidManifest.xml:

  <application
      android:name="ee.mtakso.App"
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppBaseTheme">

The checkboxes, editTexts, radiobuttons etc. remain black.

Edit

I don't know if this makes much difference, but the radiobuttons and checkboxes I'm using are for a CheckedTextView, as following:

Single (radio button): android:checkMark="?android:attr/listChoiceIndicatorSingle"

Multi (check box): android:checkMark="?android:attr/listChoiceIndicatorMultiple"

Since these get the black colored material drawable, I don't think the issue is coming from them.

like image 679
Nima G Avatar asked Nov 10 '14 11:11

Nima G


4 Answers

I had a similar problem with unchecked CheckBoxes and RadioButtons. I found the solution, when I figured out that controls takes their "Off" color from

<item name="android:textColorSecondary">@color/secondary_text</item>


EDIT:

Specifying, if your app's or activity's theme inherite one of L's AppCompat (Dark/Light/Light.DarkActionBar), you can set:

<style name="SampleTheme" parent="Theme.AppCompat">
    <item name="colorAccent">@color/green</item>
    <item name="android:textColorSecondary">@color/red</item>
</style>

And that's result:

enter image description here

Notice: When you get different effect you probably use "wrong" theme - make sure you set it correctly.

like image 137
paulina_glab Avatar answered Nov 20 '22 12:11

paulina_glab


I believe this is an error in the AppCompat theme. My workaround adding two lines of code to each CheckBox in the xml layout file.

android:button="@drawable/abc_btn_check_material"
android:buttonTint="@color/colorAccent"

You never really want to direct reference abc_ drawables but in this case I found no other solution.

This applies to RadioButton widget as well! You would just use abc_btn_radio_material instead of abc_btn_check_material

like image 37
Codeversed Avatar answered Nov 20 '22 11:11

Codeversed


I did this to change at least the border of a checkbox:

<style name="checkBoxComponent" parent="AppTheme">
    //Checked color
    <item name="colorAccent">@color/blueBackground</item> 
    //Checkbox border color
    <item name="android:textColorSecondary">@color/grayBorder</item> 
</style>

And in my layout

<android.support.v7.widget.AppCompatCheckBox
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:theme="@style/checkBoxComponent"
                        android:text="Yay" />

Still figuring out how to get the background of the checkbox though. Hope it helps.

like image 18
Daniel S. Avatar answered Nov 20 '22 10:11

Daniel S.


I had same problems as you. I looked once again at AppCompat v21 — Material Design for Pre-Lollipop Devices!

And I found this "All of your Activities must extend from ActionBarActivity, which extends from FragmentActivity from the v4 support library, so you can continue to use fragments.".

So I changed my activity to ActionBarActivity and it solved my problems. I hope it will solve yours too.

like image 14
Tezi Avatar answered Nov 20 '22 11:11

Tezi