Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android Layout weight programmatically

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>
like image 297
ranjit patel Avatar asked Jul 23 '12 11:07

ranjit patel


People also ask

How do you set linear layout weight programmatically?

android Layout weight programatically LinearLayout layout = (LinearLayout)findViewById(YOUR_LAYOT_ID); layout. setWeightSum(25f); LinearLayout. LayoutParams lParams = (LinearLayout. LayoutParams) layout.

How does layout weight work android?

Layout WeightThis attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view.

What is LayoutParams in Android?

LayoutParams are used by views to tell their parents how they want to be laid out.


1 Answers

//set as like this below for different view set different float value.

myview.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,5f));
like image 125
Padma Kumar Avatar answered Oct 31 '22 04:10

Padma Kumar