user3484582
user3484582

Reputation: 557

Why can't I use fill_parent on EditText's width in my XML file?

When I use fill_parent on the width of my EditText, the autofill of AndroidStudio shows it crossed out. If I still try using it it messes up the positioning of the EditText by throwing it out of the screen. I just want to have a simple text field and next to it an input text field which occupies the remaining of the screen.

Here is the code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.kristmiha.registrationtest.MainActivity">

<TextView
    android:id="@+id/headline1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentTop="true"
    android:gravity="center"
    android:text="@string/hello"
    android:textSize="32sp" />


<TextView
    android:id="@+id/headline2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/headline1"
    android:text="@string/register_here"
    android:gravity="center"
    android:textSize="24sp" />

<TextView
    android:id="@+id/tvFirstName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="First Name"
    android:layout_below="@+id/headline2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="20dp" />

<EditText
    android:id="@+id/etFirstName"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/tvFirstName"
    android:layout_toRightOf="@+id/tvFirstName"
    android:layout_toEndOf="@+id/tvFirstName" />

EDIT: Here is the position of the text field:

Upvotes: 0

Views: 197

Answers (3)

Vishwesh Jainkuniya
Vishwesh Jainkuniya

Reputation: 2839

try this

<TextView
    android:id="@+id/headline1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentTop="true"
    android:gravity="center"
    android:text="Hellow world!!"
    android:textSize="32sp" />


<TextView
    android:id="@+id/headline2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/headline1"
    android:text="Register Here"
    android:gravity="center"
    android:textSize="24sp" />


<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/headline2"
    android:layout_marginTop="20dp">


    <TextView
        android:id="@+id/tvFirstName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="First Name"
         />

    <EditText
        android:id="@+id/etFirstName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginLeft="4dp"
         />

</LinearLayout>

enter image description here

In relative layout

 <TextView
            android:id="@+id/tvFirstName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="First Name"
            android:layout_below="@+id/headline2"
            android:layout_marginTop="20dp"
            android:layout_alignBottom="@+id/etFirstName"
            android:gravity="center"
             />

        <EditText
            android:id="@+id/etFirstName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:layout_marginLeft="4dp"
            android:layout_below="@+id/headline2"
            android:layout_toRightOf="@+id/tvFirstName"
            android:layout_toEndOf="@+id/tvFirstName"

            android:layout_marginTop="20dp"
             />

enter image description here

Upvotes: 1

Dhaval
Dhaval

Reputation: 2884

You should try this. With some modification you can achieve what you want.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textView2"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textView3"
        android:layout_alignBottom="@+id/editText2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText2"
        android:layout_below="@+id/textView2"
        android:layout_toRightOf="@+id/textView3"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textView4"
        android:layout_alignBottom="@+id/editText3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText3"
        android:layout_below="@+id/textView3"
        android:layout_toRightOf="@+id/textView4"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

Upvotes: 1

xAqweRx
xAqweRx

Reputation: 1236

fill_parent value is deprecated use match_parent

Here you can see more and here more

Special value for the height or width requested by a View. FILL_PARENT means that the view wants to be as big as its parent, minus the parent's padding, if any. This value is deprecated starting in API Level 8 and replaced by MATCH_PARENT.

For your problem : you should use this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="test.com.temp.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="label"
        android:id="@+id/labelOne"
        android:layout_alignBaseline="@+id/input"
        />
    <EditText
        android:id="@+id/input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/labelOne"
        android:layout_toEndOf="@+id/labelOne"
        />
</RelativeLayout>

Upvotes: 0

Related Questions