Why EditText is not expanding to fill_parent

user1462473 picture user1462473 · Jul 22, 2012 · Viewed 9.1k times · Source

I have written an XML code, where I want the EditText to be expanded to fit the parent width. After spending so much time, I can not find a way to expand the EditText.

Here is my XML:

<?xml version="1.0" encoding="UTF-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">

    <TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TableRow 
        android:layout_marginLeft="10dp" 
        android:layout_marginBottom="15dip">        
        <TextView
            android:text="Comment"
            android:layout_width="match_parent"
            android:textStyle="bold"
            android:padding="3dip" />      
    </TableRow>

    <TableRow 
        android:layout_marginLeft="10dp" 
        android:layout_marginBottom="15dip">        
       <EditText 
            android:id="@+id/EditText02"
            android:layout_height="wrap_content"
            android:lines="5" 
            android:gravity="top|left" 
            android:inputType="textMultiLine"
            android:scrollHorizontally="false" 
            android:minWidth="10.0dip"
            android:maxWidth="5.0dip"/>
    </TableRow>

    <TableRow 
        android:layout_marginLeft="10dp" 
        android:layout_marginBottom="15dip"
        android:gravity="center">           
       <Button 
            android:id="@+id/cancel"
            android:text="Next" />       
   </TableRow>
</TableLayout>
</LinearLayout>

what i have made wrong here. Please help me.

Answer

user1545072 picture user1545072 · Apr 1, 2013

Kind of old, but I believe I have something to add.

I believe that according to the documentation (quoted below) the layout_width and layout_height are pre-determined for all TableRow children.

Instead, the layout_weight can be used to determine the width/height of the elements inside a TableRow. That is, unless you want to set it's android:ems property and give it a width based on it's font size.

For example:

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="@string/place"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

</TableRow>

This produced for me a full-screen wide EditText within a TableRow.

Here's a quote from the documentation of TableRow:

The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT.

The problem is, as we know, that in reality it doesn't happen. I suspect layout_width receives "WRAP_CONTENT" as well.