adding the views dynamically with layout weight property in Android
Asked Answered
N

2

5

I need to add the views in dynamically into my LinearLayout which I already have in my xml file. I tried to add the layouts and able to add it, but the problem is I could not able to set the layout weight property to the custom added views properly. It is always having the problem.

Here is my XML (the view which I am expecting)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_weight="1">

    <TextView
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_weight="0.1"
        android:text="text 1" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_weight="0.6"
        android:text="text 2" />

    <CheckBox
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_weight="0.3"
        android:text="Check Box 1" />

</LinearLayout>

He is my Java code where i am adding the views dynamically to the layout

public class MyActivity extends Activity {

    private ViewGroup mLinearLayout;  // this should be your main layout where your planning to add the views programatically 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout1);
        mLinearLayout = (ViewGroup) findViewById(R.id.linear_layout);
        addLayout("text 1", "text 2", "check box");
    }

    private void addLayout(String textView1Text, String textView2Text, String checkBoxText) {
       LinearLayout.LayoutParams param = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);  
       mLinearLayout.setLayoutParams(param);

       TextView tv1 = new TextView(this);
       tv1.setText(textView1Text);
       LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT , 0.1f);  
       tv1.setLayoutParams(lp);
       mLinearLayout.addView(tv1);


       TextView tv2 = new TextView(this);
       tv2.setText(textView2Text);
       LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT , 0.6f);  
       tv2.setLayoutParams(lp1);
       mLinearLayout.addView(tv2);

       CheckBox cb = new CheckBox(this);
       cb.setText(textView2Text);
       LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT , 0.3f);  
       lp2.weight =  0.1f;
       cb.setLayoutParams(lp2);
       mLinearLayout.addView(cb);

    }
}

Please help me to figure out the issue. TIA

Neural answered 4/8, 2017 at 5:21 Comment(4)
what error your getting ? can you post any screenshot of the output ?Nydia
No error but weight property not applied / not workingNeural
try setting width 0 in LayoutParam constructers.Goof
if you are assigning weight then you should keep width or height to zero according to horizontal or vertical orientation.Salpingectomy
N
6

Problem is with your layout params. You need to use like below

   private void addLayout(String textView1Text, String textView2Text, String checkBoxText) {
       LinearLayout.LayoutParams param = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);  
       mLinearLayout.setLayoutParams(param);

       TextView tv1 = new TextView(this);
       tv1.setText(textView1Text);
       LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, 100);  
       lp.weight =  0.1f;
       tv1.setLayoutParams(lp);
       mLinearLayout.addView(tv1);


       TextView tv2 = new TextView(this);
       tv2.setText(textView2Text);
       LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(0, 100);   
       lp1.weight =  0.6f;
       tv2.setLayoutParams(lp1);
       mLinearLayout.addView(tv2);

       CheckBox cb = new CheckBox(this);
       cb.setText(textView2Text);
       LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(0, 100);  
       lp2.weight =  0.3f;
       cb.setLayoutParams(lp2);
       mLinearLayout.addView(cb);

    }
Nydia answered 4/8, 2017 at 5:31 Comment(2)
Hi, your code seems wrong to me, look at all the "lp.weight" lines. You probably want to use lp, then lp1, then lp2, right?Cockaigne
Note also, "cb.setText(textView2Text);" should be "cb.setText(checkBoxText);"Cockaigne
A
2

Try this,

Changes

  1. Added mLinearLayout.setOrientation(LinearLayout.HORIZONTAL)
  2. width = 0
  3. you added some other linear layout my_linear_layout

    private void addLayout(String textView1Text, String textView2Text, String checkBoxText) {

        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, 100, 1.0f);
        mLinearLayout.setLayoutParams(param);
        mLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
    
        TextView tv1 = new TextView(this);
        tv1.setText(textView1Text);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT , 0.1f);
        tv1.setLayoutParams(lp);
        mLinearLayout.addView(tv1);
    
    
        TextView tv2 = new TextView(this);
        tv2.setText(textView2Text);
        LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT , 0.6f);
        tv2.setLayoutParams(lp1);
        mLinearLayout.addView(tv2);
    
        CheckBox cb = new CheckBox(this);
        cb.setText(textView2Text);
        LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT , 0.3f);
        cb.setLayoutParams(lp2);
        mLinearLayout.addView(cb);
    

    }

Archer answered 4/8, 2017 at 5:31 Comment(2)
Yes. but need to add custom height as well . i mean 100 dp . +1Nydia
you can add it in LinearLayout.LayoutParamsArcher

© 2022 - 2024 — McMap. All rights reserved.