0

I'm trying to set style to programmatically created LinearLayout this way:

(Reference: Android: set view style programmatically)

LinearLayout linearLayout = new LinearLayout(this, null, R.style.linear_container);

And styles.xml

<style name="linear_container">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_marginBottom">20dp</item>
    <item name="android:background">#9B1212</item>
    <item name="android:orientation">vertical</item>
</style>

But style is not applied how to make it work?

Hemant N. Karmur
  • 840
  • 1
  • 7
  • 21
PHP User
  • 2,350
  • 6
  • 46
  • 87

2 Answers2

0

you can set layout params as below:

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
         LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    layoutParams.setMargins(10, 10, 10, 10);
    linearLayout.setLayoutParams(layoutParams);
Hemant N. Karmur
  • 840
  • 1
  • 7
  • 21
0

An easier way to set your styles programmatically would be to use the paris library from Airbnb. After implementing the library correctly it's as easy as

myView.style(R.style.MyStyle)
Abel
  • 1,337
  • 1
  • 8
  • 16