Using v21 AppCompat we can set custom color themes like following:
<style name="Theme.MyTheme" parent="Theme.AppCompat">
<!-- customize the color palette -->
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>
</style>
But I have a bunch of dynamically created checkboxes and radio buttons which are not inflated from xml. These dynamically created objects do not inherit the color accent that I have specified. What can I do to set these color accents properly?
You can't do anything about it other than creating a layout file with just one CheckBox in it and than inflate it.
As the developers site stated: The material theme design can only be applied when loading views using a layout inflater.
This is because the new material design backport hooks into the layout inflation process.
Source: http://android-developers.blogspot.nl/2014/10/appcompat-v21-material-design-for-pre.html
Edit:
In newer versions 22.1+ of AppCompat v7 widgets like CheckBox and RadioButton can be created dynamically (no longer hidden/internal API).
Currently these widgets are supported:
I ran into the same problem and now have a material_factory_* xml file for each of my dynamically created views. Its a bit annoying but it works:
// checkbox = new CeckBox(context);
checkbox = (CheckBox) View.inflate(context, R.layout.material_factory_checkbox, null);
And the material_factory_checkbox.xml file:
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" />
You can actually do something, apply a style on the inflating view like:
<CheckBox
android:id="@+id/check_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/save_password_label"
android:theme="@style/CheckBoxStyle"
/>
with the style:
<style name="CheckBoxStyle" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorAccent">@color/colorPrimary</item>
</style>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With