Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting styles of programmatically added Views

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)

like image 483
Cuddl3s Avatar asked Jan 20 '15 13:01

Cuddl3s


People also ask

How do I set TextInputLayout style programmatically?

Method 1 - Create TextInputLayout programmatically with wrapping Context with android. view. ContextThemeWrapper and use. TextInputLayout layout = new TextInputLayout(new ContextThemeWrapper(getContext(), R.

How do I change the view on my Android?

Convert a view or layout Click the Design button in the top-right corner of the editor window. In the Component Tree, right-click the view or layout, and then click Convert view.... In the dialog that appears, choose the new type of view or layout, and then click Apply.

How to add styles XML in Android Studio?

Create and apply a style To create a new style or theme, open your project's res/values/styles.xml file. For each style you want to create, follow these steps: Add a <style> element with a name that uniquely identifies the style. Add an <item> element for each style attribute you want to define.

What is TextAppearance in Android?

TextAppearance styles can be seen as the Android equivalent of Material Design type styles. For custom styles, we recommend two approaches to help separate concerns and create a single source of truth for type theming values in your app: Store all TextAppearance styles in a single res/values/type. xml file.


1 Answers

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

like image 185
Bartek Lipinski Avatar answered Sep 22 '22 13:09

Bartek Lipinski