How to change focus to two fragments on the screen?

I have three fragments, the first two fill 80% of the screen, and the last with the rest (this size will never change). I want, after entering the user (focus) in the fragment, resize the fragment so that it fills 70% of the screen (leaving 10% of the other). Like this:

enter image description here

Can you dynamically change the weight of a fragment? Or is there a better way to achieve this?

This is the code that I have right now in 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> 
+7
android android-fragments android-layout-weight
source share
1 answer

It should be something like:

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

Assuming WEIGHT_HERE is a float (a throw may be required).

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

And if you want to use this with a button or something, just do the same without the WEIGHT options 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 parameter, so if you want everything to do the same, but do not create a new layout, edit the existing one to get it:

 LinearLayout.LayoutParams mLay = (LinearLayout.LayoutParams)myLay.getLayoutParams(); mLay.weight = (float) WEIGTH // WEIGHTf 
+2
source share

All Articles