Question

I have a vertical LinearLayout LL_p with two children horizontal LinearLayouts LL_1 and LL_2 which in turn have their own children. Based on the visible contents of LL_1 and LL_2, I want to dynamically change their relative weight inside LL_p. I already have an xml layout with a great deal of details that I do not wish to lose, so I only have to make the incremental change to the weights. How do I do that? Here is my xml

…
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=“0.2”
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/ll_1”
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.55"
            android:background="@drawable/some_image”
            android:orientation="vertical" >

            <!—- a number of includes —>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_2”
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.45"
            android:layout_marginBottom="@dimen/dim_1”
            android:background="@color/some_color”
            android:orientation="horizontal" >

            <!—- a number of children —>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

So in java activity class I figure I need the following method, but I need help completing it. Notice that I am not instantiating new layout params as that would cause me to lose my xml layout details; instead I am using getLayoutParams to grab the one set from xml. so how do I make weight change to the layout as obtained?

private void adjustMYLayout(boolean flip) {
    LayoutParams layout1 = mLL1.getLayoutParams();
    LayoutParams layout2 = mBLL2.getLayoutParams();
    //now what?
    if(flip) {//set one weight system
    }else {
        //set other weight system
    }
}

UPDATE for @nKn

private void adjustMYLayout(boolean flip) {

  if (flip) {
    mLL1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.8f));
         mLL2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.2f));
  } else {
    mLL1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.55f));
    mLL2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT, 0.45f));
  }
}
Was it helpful?

Solution

You can achieve that by declaring a LinearLayout.LayoutParams object

tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));

The third parameter (1f) is the weight of the layout (in this case 1 (f stands for float)).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top