Android: how to create a view to fill the free space?

It seems simple, but I cannot figure out how to do it. I have a horizontal layout with EditText and two ImageButtons. I want ImageButtons to have a fixed size and EditText to take up the remaining space in the layout. How can I do that?

<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content"> </EditText> <ImageButton android:src="@drawable/img1" android:layout_width="50dip" android:layout_height="50dip"> </ImageButton> <ImageButton android:src="@drawable/img2" android:layout_width="50dip" android:layout_height="50dip"> </ImageButton> </LinearLayout> 
+65
android android-layout
Mar 23 2018-11-11T00:
source share
4 answers

try this in relative layout

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageButton android:id="@+id/btn1" android:src="@drawable/icon" android:layout_width="50dip" android:layout_height="50dip" android:layout_alignParentRight="true"/> <ImageButton android:id="@+id/btn2" android:src="@drawable/icon" android:layout_width="50dip" android:layout_height="50dip" android:layout_toLeftOf="@+id/btn1"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/btn2"> </EditText> </RelativeLayout> 
+42
Mar 23 2018-11-23T00:
source share

If you want to keep the layout the same (i.e. buttons after the text), use the layout_weight property along with fill_parent , thus:

 <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <EditText android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="wrap_content"> </EditText> <ImageButton android:src="@drawable/img1" android:layout_width="50dip" android:layout_height="50dip"> </ImageButton> <ImageButton android:src="@drawable/img2" android:layout_width="50dip" android:layout_height="50dip"> </ImageButton> </LinearLayout> 

Providing an EditText 'weight' makes him think last and gives buttons an edge. Play with different layout_weights and see how much fun you can have!

+105
Mar 23 2018-11-11T00:
source share

So:

 <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageButton android:id="@+id/button1" android:src="@drawable/img1" android:layout_width="50dip" android:layout_alignParentTop="true" android:layout_height="50dip"> </ImageButton> <ImageButton android:src="@drawable/img2" android:layout_width="50dip" android:layout_alignParentTop="true" android:layout_toRightOf="@id/button1" android:layout_height="50dip"> </ImageButton> <EditText android:layout_below="@id/button" android:layout_width="wrap_content" android:layout_height="fill_parent"> </EditText> </RelativeLayout> 

The key points of the layout are:

  • Elements with a fixed size are placed first in the layout, so when Android comes to calculate the height of the height of things with fill_parent later in the file, it takes into account only the remaining space, and not all the space that it could take if there was nothing.
  • EditText has a height of fill_parent ( match_parent in version 2.2+), so that it takes up the rest of the available space

Sorry, did not read your question. The above code gives the answer if you want to do it vertically. For horizontal layout, the code sent by @Sunil should work.

+18
Mar 23 2018-11-11T00:
source share

Use set layout_width or layout_height to 0dp (If you wish, you want to fill the remaining space). Then use layout_weight to fill the remaining space.

+5
May 19 '16 at 10:41
source share



All Articles