Home » Android » android – EditText wont fill up the remaining space

android – EditText wont fill up the remaining space

Posted by: admin June 15, 2020 Leave a comment

Questions:

So i have this textview “To”, an edittext to input the contact and a search button to search for contacts all in one line. The textview and button are all in good place but the problem is the edittext is small, i want it occupy all the remaining spaces. Here’s the code:

<RelativeLayout  
        android:layout_height="wrap_content"  
        android:layout_width="fill_parent"
        android:paddingTop="10dp">
        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="To: "
            android:textColor="#000000"
            android:paddingTop="10dp"
            android:layout_toLeftOf="@+id/editText_recipient"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dp"/> 
        <EditText
            android:id="@+id/editText_recipient"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"   
            android:hint="Enter Number"  
            android:inputType="number"
            android:textSize="12sp"
            android:layout_toLeftOf="@+id/button_recipient_picker"  
            android:layout_marginLeft="5dp"/>
        <Button  
            android:id="@+id/button_recipient_picker" 
            android:layout_height="wrap_content"   
            android:layout_width="wrap_content"  
            android:text="Browse .."  
            android:layout_alignParentRight="true" 
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:onClick="doLaunchContactPicker"/>
        </RelativeLayout>
How to&Answers:
<LinearLayout  
        android:layout_height="wrap_content"  
        android:layout_width="fill_parent"
        android:paddingTop="10dp">
        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="To: "
            android:textColor="#000000"
            android:paddingTop="10dp"
            android:layout_marginLeft="10dp"/> 
        <EditText
            android:id="@+id/editText_recipient"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"   
            android:hint="Enter Number"  
            android:inputType="number"
            android:textSize="12sp"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
         />
        <Button  
            android:id="@+id/button_recipient_picker" 
            android:layout_height="wrap_content"   
            android:layout_width="wrap_content"  
            android:text="Browse .."  
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:onClick="doLaunchContactPicker"/>
        </LinearLayout>

use this layout, i converted your layout to linear (horizontal default). and added a layout_weight to editText, it will occupy the remaining space.

Answer:

For those who need an answer to this and to resolve this post without giving an alternative, the answer is fairly simple.
You need to tell every control where to position according to each other and apply to the control which should take available space android:layout_width = "match_parent" even though in a LinearLayout, this would push every other control aligned out of the screen, in a RelativeLayout it works perfectly

<RelativeLayout  
    android:layout_height="wrap_content"  
    android:layout_width="match_parent"
    android:paddingTop="10dp">
    <TextView  
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="To: "
        android:textColor="#000000"
        android:paddingTop="10dp"
        android:layout_toLeftOf="@+id/editText_recipient"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp"/> 
    <EditText
        android:id="@+id/editText_recipient"
        android:layout_height="wrap_content"
        android:layout_width="MATCH_PARENT"
        android:hint="Enter number"  
        android:inputType="number"
        android:textSize="12sp"
        android:layout_toLeftOf="@+id/button_recipient_picker"  
        android:layout_marginLeft="5dp"/>
    <Button  
        android:id="@+id/button_recipient_picker" 
        android:layout_height="wrap_content"   
        android:layout_width="wrap_content"  
        android:text="Browse .."  
        android:layout_alignParentRight="true" 
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:onClick="doLaunchContactPicker"/>
    </RelativeLayout>