How to set the width of a layout in Android?

android, java

Solution

If you're doing it the XML way you'd use the layout_width attribute:

<LinearLayout
    android:layout_width ="100dp"
    android:layout_height="wrap_content" />

If you're doing it programatically you'd use the LayoutParams:

LinearLayout ll = new LinearLayout(this);
parentView.addView(ll, new LinearLayout.LayoutParams(100, LayoutParams.WRAP_CONTENT));

Problem

Is there Any way to set the width of the Layout. Because most of the time it doesn't work?

Original source