Dev
Dev

Reputation: 199

How to put two EditText in the same line

I would like to ask you, how can I put two EditText on the same line?

Upvotes: 12

Views: 15982

Answers (4)

Sekar M
Sekar M

Reputation: 1

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/username_id"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:gravity="center"
            app:shapeAppearance="@style/Rounded">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/username"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="User Name"
                android:maxLines="1" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:gravity="center"
            app:shapeAppearance="@style/Rounded"
            android:layout_toRightOf="@+id/username_id">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/password_id"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Password"
                android:maxLines="1" />
        </com.google.android.material.textfield.TextInputLayout>
</RelativeLayout>

Upvotes: 0

Mohit Verma
Mohit Verma

Reputation: 3025

For Linear layout it will be as follows:

<?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"
    android:orientation="horizontal">
<EditText android:id="@+id/editText1" android:layout_height="wrap_content" android:layout_width="100dip">
    <requestFocus></requestFocus>
</EditText>
<EditText android:layout_height="wrap_content" android:id="@+id/editText2" android:layout_width="100dip"></EditText>
</LinearLayout>

For relative layout it will be as follows:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<EditText android:id="@+id/editText1" android:layout_height="wrap_content" android:layout_width="100dip">
    <requestFocus></requestFocus>
</EditText>
<EditText android:layout_height="wrap_content" android:layout_width="100dip" android:id="@+id/editText2" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"></EditText>
</RelativeLayout>

Upvotes: 1

user658042
user658042

Reputation:

There are multiple ways, here are 2.

With a horizontal LinearLayout

Assign android:orientation="horizontal" to your outer LinearLayout. This way all child elements of this layout will be aligned next to each other.

Semi-layout:

<LinearLayout android:orientation="horizontal"> 
    <EditText />
    <EditText />
</LinearLayout>

With a RelativeLayout

Use android:layout_toLeftOf="@id/otheredittext" or android:layout_toRightOf="@id/.." to tell one of the EditTexts that it belongs to the right/left of the other one and align the first one relative to the parent (the RelativeLayout) by using android:layout_alignParentTop="true", same with left, right or bottom.

Semi-layout:

<RelativeLayout>
    <EditText android:layout_alignParentLeft="true"
              android:layout_alignParentTop="true"
              android:id="@+id/edittext1"
              />
    <EditText android:layout_toRightOf="@id/edittext1" />
</RelativeLayout>

(Also notice that you have a +id when assigning the id for the first time in android:id and when you reference it from the layout via android:layout_to... , it's just id)

Upvotes: 5

hooked82
hooked82

Reputation: 6376

Give this a shot:

<LinearLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <EditText android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <EditText android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
</LinearLayout>

Also, you should try accepting answers if you get one that answers your question. You'll be more likely to get more/better responses.

Upvotes: 22

Related Questions