Search code examples
androidandroid-linearlayoutandroid-layout-weight

Android Linear Layout Weight Programmatically


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


Solution

  • 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);