7

I have three fragments, the first two filling 80% of the screen and the last one with the rest (this one is never going to change in size). I want to, after input from the user (focus) in a fragment, resize the fragment so it fills 70% of the screen (leaving 10% to the other one). Like this:

enter image description here

It is possible to dinamically change the weight of a fragment? Or there is a better way to achieve this?

This is the code I have right now in the XML:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="1.0">

            <FrameLayout android:id="@+id/containerParent"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight=".8">

                <LinearLayout
                    android:id="@+id/MainLinear"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical"
                    android:weightSum="1.0">

                    <FrameLayout android:id="@+id/fragment1"
                        android:layout_width="fill_parent"
                        android:layout_height="0dp"
                        android:layout_weight=".5"/>

                    <FrameLayout android:id="@+id/fragment2"
                        android:layout_width="fill_parent"
                        android:layout_height="0dp"
                        android:layout_weight=".5"/>

                </LinearLayout>

            </FrameLayout>


            <FrameLayout android:id="@+id/fragment3"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight=".2"/>


        </LinearLayout>

    </FrameLayout>


</android.support.v4.widget.DrawerLayout>
Marcus
  • 6,697
  • 11
  • 46
  • 89
Gardener
  • 177
  • 8

1 Answers1

2

It should be something like :

LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                         LayoutParams.MATCH_PARENT,
                         LayoutParams.MATCH_PARENT, WEIGTH_HERE);

Assuming that WEIGHT_HERE is a float (cast may be needed).

(I don't know if it's a good way to do it but it should do what you want to)

And if you want to use this with a button or something just do the same without the WEIGHT params and add :

Button b = new Button(this);
param.weight= (float) 0.5 // 0.5f
b.setLayoutParams(param);

But this method will create a new Layout Paramater, so if you want to kept everything just do the same but do not create a new layout, edit the existing one by getting it :

LinearLayout.LayoutParams mLay =
(LinearLayout.LayoutParams)myLay.getLayoutParams();
mLay.weight = (float) WEIGTH // WEIGHTf
Nico
  • 168
  • 2
  • 12
  • 1
    Thank you for your answer, but I actually want the fragment to move (like an animation) towards the destination point, not relocate instantly. Is this possible? – Gardener May 05 '15 at 15:35
  • 1
    Yeah, and it's realy simple, you just have to import `android.transition.TransitionManager` And write this : `Transition.beginDelayedTransition(mLay);` This will start a transition (default transition) that will look how is your Layout when u start ur app, and then, how is your layout when the action is done, and this will simply add a transition between those 2 steps. But mLay has to be a Layout, and not a LayoutParams. You can get ur layout with : `mLay = (ViewGroup) findViewById(LayoutID);` ` – Nico May 06 '15 at 14:27