android Layout weight programmatically
Asked Answered
P

6

14

i have hard-coded the layout_weight on layout xml.but now i want to give the layout_weight and weightSum from java code.

How we do that from our class?

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="25" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:background="#F51215"
        android:paddingLeft="5dip"
        android:text="5" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="20"
        android:background="#1AC92B"
        android:paddingLeft="5dip"
        android:text="20" />
</LinearLayout>
Pya answered 23/7, 2012 at 11:22 Comment(0)
C
30
//set as like this below for different view set different float value.

myview.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,5f));
Chablis answered 23/7, 2012 at 11:24 Comment(2)
Note that unlike in XML, you should NOT set the height or width to 0 as the Layout will simply not appear otherwise.Moreta
@Moreta May be its changed now. Setting width zero worked.Prognosis
W
44

Something like this:

               ......
               LinearLayout layout = (LinearLayout)findViewById(YOUR_LAYOT_ID);
               layout.setWeightSum(25f);
               LinearLayout.LayoutParams lParams = (LinearLayout.LayoutParams) layout.getLayoutParams(); //or create new LayoutParams... 

               lParams.weight = 0.5f;
               .......
               someView.setLayoutParams(lParams);
               .......  
Wellheeled answered 23/7, 2012 at 11:31 Comment(3)
I cannot do this for someView being a TextView. Any other solution?Idaline
@NarayanaJ, are you sure? developer.android.com/reference/android/widget/…Wellheeled
@VyacheslavShilkin, thanks for pointing out. I seem to have lost the context of why I was trying this, but Studio threw an error when I put that method in my Activity, for some reason, leading me to believe the method was deprecated. Apparently, that method is very much in the API. Sorry for the hasty comment, shall I delete it? Cheers!Lister
C
30
//set as like this below for different view set different float value.

myview.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,5f));
Chablis answered 23/7, 2012 at 11:24 Comment(2)
Note that unlike in XML, you should NOT set the height or width to 0 as the Layout will simply not appear otherwise.Moreta
@Moreta May be its changed now. Setting width zero worked.Prognosis
D
13

I just wanted to add an example of how to use the weightsum with e.g a TableRow with TextViews inside:

           TableRow row = new TableRow(this);
           TableRow.LayoutParams params1 = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT, 10f);
           row.setLayoutParams(params1);
           row.setPadding(10, 10, 10, 10);
           row.setBackgroundColor(R.color.black);

           TextView tv = new TextView(this);
           TableRow.LayoutParams params2 = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 6f);
           tv.setLayoutParams(params2);
           tv.setTextSize(30);
           tv.setBackgroundResource(R.color.white);

           TextView tv2 = new TextView(this);
           TableRow.LayoutParams params3 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, 2f);
           tv2.setLayoutParams(params3);
           tv2.setBackgroundResource(R.color.green);

           TextView tv3 = new TextView(this);
           TableRow.LayoutParams params4 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, 2f);
           tv3.setLayoutParams(params4);
           tv3.setBackgroundResource(R.color.blue); 

Produces the TableRow underneath. The parent whose weightsum is 10, and the children's widths are then equal to 60%, 20% and 20% of the width. The height is here determined by the TextView, tv, which has a height of 30. Hope it helps someone. :-)

text

Doityourself answered 5/12, 2014 at 15:35 Comment(0)
E
7

If You want to change parent weight programmatically and it's child property as it is then use below

Here's how you set it.

 LinearLayout yourLayout=(LinearLayout)findViewById(R.id.container);
 yourLayout.setWeightSum(0.6f);
Elinorelinore answered 10/2, 2016 at 12:38 Comment(0)
T
2

Dynamically Generate TextView with Weight using LinearLayout

LinearLayout lin_hoizontal = new LinearLayout(context);
lin_hoizontal.setOrientation(LinearLayout.HORIZONTAL);
lin_hoizontal.setLayoutParams(new android.widget.LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 10f));
lin_hoizontal.setPadding((int) context.getResources().getDimension(R.dimen.d_8), (int) context.getResources().getDimension(R.dimen.d_2), (int) context.getResources().getDimension(R.dimen.d_8), (int) context.getResources().getDimension(R.dimen.d_2));

android.widget.LinearLayout.LayoutParams params_label = new android.widget.LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 2.5f);
TextView txt_label = new TextView(context);
txt_label.setTextColor(context.getResources().getColor(R.color.listing_header_txt_color));//your text color
txt_label.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.d_13));
txt_label.setTypeface(Typeface.create("sans-serif-medium", Typeface.NORMAL));
txt_label.setLayoutParams(params_label);
txt_label.setPadding(0, 0, (int) context.getResources().getDimension(R.dimen.d_2), 0);
txt_label.setText("Label");


android.widget.LinearLayout.LayoutParams params_value = new android.widget.LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 7.5f);
TextView txt_value = new TextView(context);
txt_value.setTextColor(context.getResources().getColor(R.color.listing_header_txt_color));
txt_value.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.d_13));
txt_value.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
txt_value.setLayoutParams(params_value);
txt_value.setPadding((int) context.getResources().getDimension(R.dimen.d_2), 0, 0, 0);
txt_value.setText("Value");

lin_hoizontal.addView(txt_label);
lin_hoizontal.addView(txt_value);
lin_hoizontal.addView(lin_main);
Thrasher answered 23/2, 2017 at 7:2 Comment(0)
C
0

I like Ajii's answer or:

((LinearLayout)view).setWeightSum(n);

and for individual view weight:

((LinearLayout.LayoutParams)view.getLayoutParams()).weight = n;
  • of course you can do.. LinearLayout view = findViewById(R.id.view_name);
    (and drop the dynamic casting (LinearLayout))

  • or substitute...……….. findViewById(R.id.view_name) for view above.

These both worked for me. And if you want value in dimens file:

<item name="new_weight" format="float" type="dimen">(your number)</item>

and: float n = getResources().getFloat(R.dimen.new_weight);

I'm sure you already know most of this but... I was a newbie once and I always appreciated the extra descriptions.

Cammycamomile answered 2/9, 2019 at 14:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.