Dynamically created radio button or CheckBox does not use Color Accent

Using v21 AppCompat, we can set custom color themes, for example the 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 that are not overpriced from xml . These dynamically created objects do not inherit the color accent that I indicated. What can I do to set these color accents correctly?

+5
source share
5 answers

You can do nothing but create a layout file with just one CheckBox, and then inflate it.

According to the website of the developers: the design of the material theme can be applied only when loading representations using the adjustable layout.

This is due to the fact that the new design layout addresses the inflation process of the layout.

Source: http://android-developers.blogspot.nl/2014/10/appcompat-v21-material-design-for-pre.html

Edit:
In newer versions of 22.1+ AppCompat v7 widgets, such as CheckBox and RadioButton, you can create 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)
+11
source

I ran into the same problem and now I have a material_factory_ * xml file for each of my dynamically created views. It annoys him a bit, but it works:

 // checkbox = new CeckBox(context); checkbox = (CheckBox) View.inflate(context, R.layout.material_factory_checkbox, null); 

And the file material_factory_checkbox.xml:

 <?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" /> 
+5
source

You can really do something, apply a blown style, for example:

  <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 style:

 <style name="CheckBoxStyle" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorAccent">@color/colorPrimary</item> </style> 
+1
source

Has the same problem. You need to set the theme of the Radio Button, 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" /> 

Ps it works with dynamically generated radio buttons

0
source

Try using the AppCompatCheckBox from the support library:

 import android.support.v7.widget.AppCompatCheckBox; AppCompatCheckBox cb = new AppCompatCheckBox(context); 
-1
source

Source: https://habr.com/ru/post/1206101/


All Articles