Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to make a view grow to fill available space?

This seems straightforward, but I can't figure out how to do it. I have a horizontal layout with an EditText and two ImageButtons. I want the ImageButtons to be of a fixed size, and the EditText to take up the remaining space in the layout. How can this be accomplished?

<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> 
like image 434
ab11 Avatar asked Mar 23 '11 13:03

ab11


People also ask

How can I add space between two views in Android?

android:layout_marginTop XML attribute is used to specify extra space on the top side of this view. Thus android:layout_marginTop="10dp" on 2nd textView is specifying 10 dp of space between it and above view.


2 Answers

If you want to keep the layout the same (ie, 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> 

Giving the EditText 'weight' makes it get figured out last and gives precedence to the buttons. Play with the different layout_weights and see how much fun you can have!

like image 191
Jonathan Avatar answered Oct 22 '22 10:10

Jonathan


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> 
like image 38
Sunil Pandey Avatar answered Oct 22 '22 08:10

Sunil Pandey