Applying a Theme to Dynamic Radio Buttons in Android
Asked Answered
S

2

6

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.

Splendor answered 13/9, 2012 at 8:47 Comment(0)
S
11

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).

Splendor answered 14/9, 2012 at 8:46 Comment(0)
T
1

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.

Thyself answered 30/5, 2018 at 14:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.