Android appcompat-v7:21.0.0 change material checkbox colors
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:
Notice: When you get different effect you probably use "wrong" theme - make sure you set it correctly.
appcompat-v7 custom button colors
Create a new file named "shape_btn.xml" in your drawable directory like this, change the android:color to what you like:
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="@dimen/abc_button_inset_horizontal_material"
android:insetTop="@dimen/abc_button_inset_vertical_material"
android:insetRight="@dimen/abc_button_inset_horizontal_material"
android:insetBottom="@dimen/abc_button_inset_vertical_material">
<shape android:shape="rectangle">
<corners android:radius="@dimen/abc_control_corner_material" />
<solid android:color="@android:color/black" />
<padding android:left="@dimen/abc_button_padding_horizontal_material"
android:top="@dimen/abc_button_padding_vertical_material"
android:right="@dimen/abc_button_padding_horizontal_material"
android:bottom="@dimen/abc_button_padding_vertical_material" />
</shape>
</inset>
and set to button background:
<Button
...
android:background="@drawable/shape_btn"
/>
If you want to apply it to all buttons in your app, change your theme like this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:buttonStyle">@style/MyButtonStyle</item>
</style>
<style name="MyButtonStyle" parent="Widget.AppCompat.Button" >
<item name="android:background">@drawable/shape_btn</item>
</style>
Cannot get Material Design CheckBox to use styling
OK, after much research and testing, I have a solution that works both for my CheckBox and EditText controls.
The (bad) assumption on my part was that one theme could take care of AppCompat and Material Design, which is not true. Basically, for AppCompat (< API 21), you have to define your styles with names without the android:
prefix, such as this for the CheckBox and EditText:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Base application theme -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.EditText" parent="Widget.AppCompat.EditText">
<item name="colorControlNormal">@color/almanac_red_dark</item>
<item name="colorControlActivated">@color/almanac_red_light</item>
<item name="colorControlHighlight">@color/almanac_red_light</item>
</style>
<style name="AppTheme.CheckBox">
<item name="colorAccent">@color/almanac_red_dark</item>
</style>
<style name="AppTheme.FlatButton">
<item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
<item name="android:textColor">@android:color/white</item>
</style>
<style name="AppTheme.FacebookFlatButton" parent="AppTheme.FlatButton">
<item name="android:background">@drawable/facebook_flat_button</item>
</style>
<style name="AppTheme.TwitterFlatButton" parent="AppTheme.FlatButton">
<item name="android:background">@drawable/twitter_flat_button</item>
</style>
</resources>
But you have to have another version of the style for Material Design versions (> API 21) in the values-v21
directory (with the Material Design parent theme), with the android:
prefix:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="@android:style/Theme.Material.Light">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.EditText">
<item name="android:colorControlNormal">@color/almanac_red_dark</item>
<item name="android:colorControlActivated">@color/almanac_red_light</item>
<item name="android:colorControlHighlight">@color/almanac_red_light</item>
</style>
<style name="AppTheme.CheckBox">
<item name="android:colorAccent">@color/almanac_red_dark</item>
</style>
</resources>
I have tried on the Nexus 4 (API 16), Nexus 5 (API 19) and Nexus 6 (API 22) emulators and my physical Nexus 6 device and everything looks as I expect.
Android AppCompat Dark Theme Settings Checkbox
I solved this problem by using the solution on this page to create a compatible PreferenceFragment class by copying the code into my project. I then replaced my former PreferenceActivity with a class inheriting from ActionBarActivity and instantiate a Fragment class derived from the new PreferenceCompatFragment class. This now work well to display my preferences with an Action Bar of the expected color as well as checkboxes that are properly accented. Here is the code for my new settings activity:
public class SettingsFragActivity extends ActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frame = new FrameLayout(this);
frame.setId(R.id.content);
setContentView(frame);
this.getSupportFragmentManager().beginTransaction()
.replace(R.id.content, new SettingsFragment (), null).commit();
}
public static class SettingsFragment extends PreferenceCompatFragment {
@Override
public void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
addPreferencesFromResource(R.xml.prefs);
}
}
}
Note that the addPreferencesFromResource belongs to the custom PreferenceFragment class.
appcompat-v7 custom button colors
Create a new file named "shape_btn.xml" in your drawable directory like this, change the android:color to what you like:
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="@dimen/abc_button_inset_horizontal_material"
android:insetTop="@dimen/abc_button_inset_vertical_material"
android:insetRight="@dimen/abc_button_inset_horizontal_material"
android:insetBottom="@dimen/abc_button_inset_vertical_material">
<shape android:shape="rectangle">
<corners android:radius="@dimen/abc_control_corner_material" />
<solid android:color="@android:color/black" />
<padding android:left="@dimen/abc_button_padding_horizontal_material"
android:top="@dimen/abc_button_padding_vertical_material"
android:right="@dimen/abc_button_padding_horizontal_material"
android:bottom="@dimen/abc_button_padding_vertical_material" />
</shape>
</inset>
and set to button background:
<Button
...
android:background="@drawable/shape_btn"
/>
If you want to apply it to all buttons in your app, change your theme like this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:buttonStyle">@style/MyButtonStyle</item>
</style>
<style name="MyButtonStyle" parent="Widget.AppCompat.Button" >
<item name="android:background">@drawable/shape_btn</item>
</style>
Change checkbox/radio button accent color
You can use the OnCheckedChangeListener
and one own image for this. For example:
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
checkbox.setButtonDrawable(R.drawable.yourImage);
}
}
});
Use a one-color-image when you want a one-color-background like the green one.
Related Topics
Why Does Gradle Build My Module in Release Mode When the App Is in Debug
Android 'Unable to Add Window -- Token Null Is Not for an Application' Exception
Incorrect Coordinates from Getlocationonscreen/Getlocationinwindow
Need to Disable Expand on Collapsingtoolbarlayout for Certain Fragments
Opening Webview Not in New Browser
How to Retrieve Data from Cursor Class
Message "Error: Resource Android:Attr/Lstar Not Found"
I Have Forgotten My Keystore Password and I Want to Install My APK on Google Play. What Do I Do
Android Multiple Selection Listview & Textview
What Is Meant by Ems? (Android Textview)
Android: How to Refresh Listview Contents
Receiver Not Registered Exception Error
How to Set Minimum Datepicker Date to Current Date
What Are the Differences Among Internal Storage, External Storage, Sd Card and Removable Storage
Get Temperature of Battery on Android
Change Text Color of Selected Item in Spinner