Android nested linear layout for custom design
<p>I want to add two linear layouts after one layouts to an activity programatically each of same width. the problem is i am not able to set the weights of these layouts programatically. I could do this within xml, but I want to do this in program. here is what i want: </p>

<p><a href="https://i.stack.imgur.com/OA3oK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OA3oK.png" alt="all are button"></a></p>

<p>first button contains full width of screen but after this two button will share equal width and so on...</p>

<p>obviously, need to do it programmatically.</p>

<p>my layout:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="@drawable/background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".HomeActivity"&gt;

    &lt;ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"&gt;

        &lt;LinearLayout
            android:id="@+id/linearLayout1"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"&gt;
        &lt;/LinearLayout&gt;


    &lt;/ScrollView&gt;
&lt;/LinearLayout&gt;
</code></pre>

<p>code behinds:</p>

<pre><code>Button button = new Button(HomeActivity.this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 564);
                        layoutParams.setMargins(60, 80, 60, 40);

 Drawable drawable = Drawable.createFromStream(getAssets().open("image/GridButton/NovoTel_IGW_lg.png"), null);
button.setBackground(drawable);
button.setLayoutParams(layoutParams);

linearLayout.addView(button);
</code></pre>

<p>I got output like this:</p>

<p><a href="https://i.stack.imgur.com/7m4dK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7m4dK.png" alt="enter image description here"></a></p>

<p>Please suggest, which is the best way.</p>