4

The Title says it all, hopefully.

1) I create a View programmatically:

RelativeLayout rl = new RelativeLayout(this);

2) I want to add it to an existing LinearLayout and after that i want to add a Style to the RelativeLayout. Something like:

LinearLayout ll = (LinearLayout) findViewById(R.id.MyLinearLayout);
RelativeLayout rl = new RelativeLayout(this);
ll.addView(rl);
//add Style to rl here

I can't find a way to do that!

I know there are ways to add a Style programmatically. Something like:

RelativeLayout rl = new RelativeLayout(this, null, R.style.MyRelativeLayout);

But this will add the style before i've added the relativelayout to the linearlayout. Therefore the relativelayout isn't "printed" because how should he know that the linearlayout is his parent before i add it to the linearlayout.

Does anybody has a way how to add a Style programmatically to a view after this view has been created and added to a parent?

Hope you understand what i mean.

1
  • What do you mean by "printed"? Commented Nov 9, 2014 at 1:58

2 Answers 2

14

You can't apply a style after constructing a view. The correct way to do this is to use the 4-argument constructor on Android 5.0+ or to create a theme attribute that references your style and use the 3-argument constructor.

// Works on versions prior to Android 5.0
RelativeLayout rl = new RelativeLayout(this, null, R.attr.myRelativeLayoutStyle);

// Works on Android 5.0 and above
RelativeLayout r2 = new RelativeLayout(this, null, 0, R.style.MyRelativeLayout);

res/values/attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="myRelativeLayoutStyle" format="reference" />
    ...

res/values/styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyRelativeLayout">
        ...
    </style>
    ...

res/values/themes.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyAppTheme" parent="...">
        <item name="myRelativeLayoutStyle">@style/MyRelativeLayout</item>
        ...
Sign up to request clarification or add additional context in comments.

3 Comments

Where do i put correctly the themes and attrs.XML files? Directly into values or should i create a seperate Folder?? @alanv
And which Resource Type does attrs.xml use? I only can choose Values to get the resource accessible @alanv
Ah, sorry, I forgot to add "values" to the path. They should all go in res/values.
0

Two useful answers.

RelativeLayout layout = (RelativeLayout)getLayoutInflater().inflate(R.layout.template, null);

or

int buttonStyle = R.style.your_button_style;
Button button = new Button(new ContextThemeWrapper(context, buttonStyle), null, buttonStyle).

See https://stackoverflow.com/a/24438579/5093308 and https://stackoverflow.com/a/5488652/5093308

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.