Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I resize two fragments in the screen by focus?

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>
like image 847
Gardener Avatar asked Apr 27 '15 09:04

Gardener


1 Answers

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
like image 104
Nico Avatar answered Oct 24 '22 05:10

Nico