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:

            Asked
            
        
        
            Active
            
        
            Viewed 4.0k times
        
    32
            
            
         
    
    
        ivan_pozdeev
        
- 33,874
- 19
- 107
- 152
 
    
    
        Shahbaz Pothiawala
        
- 1,175
- 5
- 20
- 38
4 Answers
61
            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);
 
    
    
        Tofeeq Ahmad
        
- 11,935
- 4
- 61
- 87
- 
                    1Worked like charm, thanks for quick response. However I had to modify it a bit like this >> LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT); lp.weight = 1; – Shahbaz Pothiawala Oct 03 '13 at 12:10
7
            
            
        Use new LinearLayout.LayoutParams(int width, int height, float weight) to set weights when setting layout params to the subviews
 
    
    
        Ivo
        
- 18,659
- 2
- 23
- 35
4
            
            
        Do this way..
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txtNote = (LinedEditText) findViewById(R.id.txtNote);
    lnr = (LinearLayout) findViewById(R.id.lnr);
    LinearLayout l1 = new LinearLayout(this);
    LinearLayout l2 = new LinearLayout(this);
    LinearLayout l3 = new LinearLayout(this);
    l1.setBackgroundResource(android.R.color.holo_green_light);
    l2.setBackgroundResource(android.R.color.holo_orange_dark);
    l3.setBackgroundResource(android.R.color.holo_blue_bright);
    LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1);
    lnr.addView(l1, param);
    lnr.addView(l2, param);
    lnr.addView(l3, param);
}
 
    
    
        Brijesh Patel
        
- 676
- 1
- 5
- 13
4
            
            
        You can do it by setting layout weight property for your individual linear layouts, pass it in LinearLayout - LayoutParams constructor:
LinearLayout.LayoutParams param = new LinearLayout.LayoutParam(
                         LayoutParams.MATCH_PARENT,
                         LayoutParams.MATCH_PARENT, 1);
or
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                         0,
                         LayoutParams.MATCH_PARENT, 1);
Hope it may help you !
 
    
    
        AndyN
        
- 1,742
- 1
- 15
- 30