Neoh
Neoh

Reputation: 16174

Dynamically Created Radio Button or CheckBox does not use Color Accent

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?

Upvotes: 5

Views: 3179

Answers (5)

pierre
pierre

Reputation: 11

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>

Upvotes: 1

IRPdevelop
IRPdevelop

Reputation: 281

Try using AppCompatCheckBox from support library:

import android.support.v7.widget.AppCompatCheckBox;

AppCompatCheckBox cb =  new AppCompatCheckBox(context);

Upvotes: -1

AhmedW
AhmedW

Reputation: 576

Was having the same issue. You need to set the Radio Button Theme , not the Style.

android:theme="@style/RadioButtonStyle"

Example

<RadioButton 
android:theme="@style/RadioButtonStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp" />

P.s this is working on dynamically created Radio Buttons

Upvotes: 0

Rolf ツ
Rolf ツ

Reputation: 8801

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:

  • AppCompatAutoCompleteTextView
  • AppCompatButton
  • AppCompatCheckBox
  • AppCompatCheckedTextView
  • AppCompatEditText
  • AppCompatMultiAutoCompleteTextView
  • AppCompatRadioButton
  • AppCompatRatingBar
  • AppCompatSpinner
  • AppCompatTextView
  • AppCompatSeekBar (since 23.1)
  • AppCompatImageButton (since 23.1)
  • AppCompatImageView (since 23.1)

Upvotes: 11

Simon
Simon

Reputation: 13283

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" />

Upvotes: 6

Related Questions