Android Linear Layout Weight Programmatically

Shahbaz Pothiawala picture Shahbaz Pothiawala · Oct 3, 2013 · Viewed 32.8k times · Source

I want to add three linear layouts to an activity programatically each of same width. the problem is i am not able to set the weights of these layouts programmatically. I could do this within xml, but I want to do this in program. here is what I want: enter image description here

Answer

Tofeeq Ahmad picture Tofeeq Ahmad · Oct 3, 2013

Here its the solution

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, 100);
    lp.weight = 1;

See Full Solution

LinearLayout ll1, ll2, ll3;
    /* Find these LinearLayout by ID 
     i.e ll1=(LinearLayout)findViewById(R.id.ll1);
     */

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, 100);
    lp.weight = 1;
    ll1.setLayoutParams(lp);
    ll2.setLayoutParams(lp);
    ll3.setLayoutParams(lp);