Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying a Theme to Dynamic Radio Buttons in Android

I have a Theme in my Application which defines default textappearance and Button Style.

<style name="AppTheme" parent="@android:style/Theme.Light.NoTitleBar.Fullscreen">
    <item name="android:textAppearance">@style/TextAppearance.Medium</item>
    <item name="android:textAppearanceLarge">@style/TextAppearance.Large</item>
    <item name="android:textAppearanceMedium">@style/TextAppearance.Small</item>
    <item name="android:textAppearanceSmall">@style/TextAppearance.Medium</item>
    <item name="android:textColorPrimary">#000000</item>
    <item name="android:textColorSecondary">#000000</item>
    <item name="android:textColorTertiary">#000000</item>
    <item name="android:buttonStyle">@style/Button</item>
</style>

<style name="TextAppearance" parent="@android:style/TextAppearance">
    <item name="android:textColor">?android:attr/textColorPrimary</item>
    <item name="android:textColorHint">?android:attr/textColorHint</item>
</style>

<style name="TextAppearance.Large">
    <item name="android:textColor">?android:attr/textColorPrimary</item>
    <item name="android:textSize">32dp</item>
    <item name="android:textStyle">bold</item>
</style>

<style name="TextAppearance.Medium">
    <item name="android:textColor">?android:attr/textColorSecondary</item>
    <item name="android:textSize">22dp</item>
</style>

<style name="TextAppearance.Small">
    <item name="android:textColor">?android:attr/textColorTertiary</item>
    <item name="android:textSize">18dp</item>
</style>

<style name="Button" parent="@android:style/Widget.Button">
    <item name="android:textColor">?android:attr/textColorSecondary</item>
    <item name="android:textColorHint">?android:attr/textColorHint</item>
    <item name="android:textStyle">normal</item>
    <item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
</style>

When I create a RadioButton using

RadioButton radioButton = new RadioButton(this, null, R.style.AppTheme);

The RadioButton is created and the text is styled properly but the checked marker disappears. Why?

I have the Radiobutton in a Buttongroup, where I dynamically add the buttons. When I add a Button via the layout the marker is displayed properly. But the dynamically added buttons don't.

like image 719
H0LL0 Avatar asked Sep 13 '12 08:09

H0LL0


Video Answer


2 Answers

The solution was to use the RadioButton(this) constructor.

All stylings set with the defStyle-Constructor were ignored and the marker were removed. I am not sure if this is a bug or it is not documented.

I actually don't need it since the other stylings from my styles.xml apply to the button automatically now, but if anyone is interested:

adding

<item name="android:radioButtonStyle">@style/MyRadioButton</item>

to my "AppTheme"-Style and

<style name="MyRadioButton" parent="@android:style/Widget.CompoundButton.RadioButton">
    <item name="android:textSize">5dp</item>
</style>

to the style.xml makes all my radiobuttons display the marker AND style the text (without setting it in the constructor).

like image 159
H0LL0 Avatar answered Oct 10 '22 00:10

H0LL0


As for me, it has been successfully solved just by using inflater:

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ...
    RadioButton rb = (RadioButton) inflater.inflate(R.layout.radio_butt, null);

Take a look at the full answer!

Hope, it'll help somebody.

like image 31
Maria Avatar answered Oct 09 '22 23:10

Maria