Setting styles of programmatically added Views
Asked Answered
S

2

9

In my code, I add input elements like radioButtons, checkboxes etc to my Layout programmatically. The problem is, that the style of those elements is not the default style that you would get, when you would add let's say a radioButton via xml. (It looks really white and almost see-through on a white application background. A bit like it is transparent) Also, the EditText elements I'm adding have the same style and if you type something in them, the text is too big and overlaps the text line a bit. So I guess it all comes down to somehow giving those elements their default style, like they look when defined via xml.

A sample of my code looks like this:

RadioGroup radioGroup = new RadioGroup(mContext);
    radioGroup.setLayoutParams(fullWidthWrapHeight);

    for (int i = 0; i < arg0.getOptions().size(); i++){
        RadioButton radioButton = new RadioButton(mContext, null);
        radioButton.setPadding(padding16dp , padding8dp, padding16dp, padding8dp);
        radioButton.setText(arg0.getOptions().get(i).getText());
        radioButton.setLayoutParams(wrapBoth);
        radioButton.setGravity(Gravity.CENTER_HORIZONTAL);

        radioButton.setTextAppearance(mContext, R.style.Default_Text);
        radioGroup.addView(radioButton);
    }

My target API lvl is 21 (Lollipop)

Scarabaeid answered 20/1, 2015 at 13:45 Comment(0)
L
19

You can pass a style defined inside styles.xml as an argument of a View constructor. So considering your example, you would have to call:

RadioButton radioButton = new RadioButton(mContext, null, R.attr.radioButtonStyle);

then add custom attribute inside attrs.xml

<attr name="radioButtonStyle" format="reference" />

and inside your application theme in styles.xml add

<item name="radioButtonStyle">@style/YourRadioButtonStyle</item>

YourRadioButtonStyle is custom radio button style defined in styles.xml

Leprosarium answered 20/1, 2015 at 14:15 Comment(8)
Is it also somehow possible to pass the default radioButton style, or do I have to create a new style that has the default style as parent?Scarabaeid
you have to define YourRadioButtonStyle that has @android:style/Widget.CompoundButton.RadioButton as a parent and sets some fields in the way you wantLeprosarium
to pass the default radioButton style you should be able to use com.android.internal.R.attr.radioButtonStyle instead of R.attr.radioButtonStyleLeprosarium
If I do it, the text overlaps the checkbox image... some idea why this ocurs?Foulard
maybe it's somehow related to thisLeprosarium
@Scarabaeid is there a reason why you didn't accept the answer?Leprosarium
@BartekLipinski you mean other than me forgetting about it cause I posted it quite some time ago? :) Fixed!Scarabaeid
@Scarabaeid treat my comment as a gentle reminder then :D thanks!Leprosarium
M
2

As for me, it has been successfully solved just by that way:

Activity.java

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

...

    { 
        RadioButton rb = (RadioButton) inflater.inflate(R.layout.radio_butt, null);
        rb.setText(this_currency_option);
        rb.setTextColor(context.getResources().getColor(R.color.colorWhite));
        rb.setId(100 + i);
        radioGroup.addView(rb);
    }

radio_butt.xml

<?xml version="1.0" encoding="utf-8"?>
<RadioButton
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="14sp"
    android:textColor="@color/colorWhite"
    android:theme="@style/MyRadioButtonStyle"/>

slyles.xml

<style name="MyRadioButtonStyle" parent="@android:style/Widget.CompoundButton.RadioButton">
    <item name="colorControlNormal">@color/colorAlfaWhite</item>
    <item name="colorControlActivated">@color/colorWhite</item>
</style>
Marianomaribel answered 30/5, 2018 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.