5

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));
  }
}
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Katedral Pillon
  • 14,534
  • 25
  • 99
  • 199

1 Answers1

6

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)).

nKn
  • 13,691
  • 9
  • 45
  • 62
  • by tv I image you mean mLL1 or mLL2. But then how about my concerns about losing the existing details set in the xml for say mLL1? details such as background, padding, etc. I believe I know how to do what you are suggesting. But doing it that way means starting from scratch so that i have to pass everything else anew: margin, background, etc. That is what I am trying to avoid. Or am I mistaken? – Katedral Pillon Apr 17 '14 at 20:08
  • Yes, sorry, those would be your `mLLX`. You shouldn't lose any value defined in your layout file but the `layout_height`, `layout_width` and `layout_height`, as you're just overriding this. The remaining attributes should just keep as you defined them in the layout file. – nKn Apr 17 '14 at 20:10
  • Thanks. But your way actually lost my marginBottom which means I need to do some work to change 10dp to pixels and then add them back with `setMargins(0,0,0,convertedDp2Px)`. My question is specifically to avoid "messes" like that. I was hoping android provided a nicer facility for changing weights. – Katedral Pillon Apr 17 '14 at 20:30
  • It actually shouldn't change any margin specification, are you sure they're correctly set in your layout file? I'm not sure there's another way of achieving this dynamically than the one I provided. – nKn Apr 17 '14 at 20:34
  • You already have my xml file. I just updated the OP to show you the java method that I am using based on your answer. – Katedral Pillon Apr 17 '14 at 20:39
  • It seems ok to me, still I wonder why your margins get modified. I'll try to do some research about it and post something if I find out something. – nKn Apr 17 '14 at 20:45
  • 1
    I believe I got it to work by changing `LinearLayout.LayoutParams.WRAP_CONTENT` to `0` – Katedral Pillon Apr 17 '14 at 20:52