Skip to content Skip to sidebar Skip to footer

Android_relativelayout - How To Put Two Elements On The Same Line?

The question is: I have a TextView and an EditText, how can I place them on the same 'level'? http://postimage.org/image/o9l17a3zv/ As you can see, I want to put 'Username' on the

Solution 1:

TEXT VIEW AND EDIT TEXT ARE ON THE SAME LINE TOUCHING THE BOTTOM LINE OF PARENT LAYOUT Try this one.

android:layout_alignBottom="@+id/editText1"

This will surely help you.

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/parentView"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBottom="@+id/editText1"android:text="TextView" /><EditTextandroid:id="@+id/editText1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_toRightOf="@+id/textView1"android:ems="10" ><requestFocus /></EditText></RelativeLayout>

Thanks.

Solution 2:

android:gravity="center" is the solution

Solution 3:

Use the android:layout_toRightOf or android:layout_toLeftOf attributes, exclusive to relative layouts

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:text="TextView" /><EditTextandroid:id="@+id/editText1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_toRightOf="@+id/textView1"><requestFocus /></EditText></RelativeLayout>

Solution 4:

try this

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent" ><TextViewandroid:id="@+id/account_creation"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="account_creation"android:textSize="20sp"android:textStyle="bold" /><RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_below="@+id/account_creation" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:text="user_text" /><EditTextandroid:id="@+id/editText1"android:hint="Enter_username"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_toRightOf="@+id/textView1" ></EditText></RelativeLayout></RelativeLayout>

Solution 5:

You have to center your text in the TextView widget for that just use the android attribute android:gravity in your TextView xml declaration.

If you want Your text to get centered vertically and horizontally use:

 android:gravity="center"

If you want Your text to get centered only horizontally use:

 android:gravity="center_horizontal"

Post a Comment for "Android_relativelayout - How To Put Two Elements On The Same Line?"