hyperloris hyperloris - 8 days ago 6
Android Question

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 same level of the EditText...

Here my code:

<TextView
android:id="@+id/account_creation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/account_creation"
android:textSize="20sp"
android:textStyle="bold" />

<!-- Username -->

<TextView
android:id="@+id/username_registration"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/account_creation"
android:layout_toLeftOf="@+id/username_new_account"
android:text="@string/user_text"
android:textSize="15sp" />

<EditText
android:id="@+id/username_new_account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/account_creation"
android:layout_alignParentRight="true"
android:ems="10"
android:hint="@string/user_editext" >

<requestFocus />
</EditText>


I was thinking to put the same value of "height" on both of them, but i don't know if it's the right/better way...

Answer

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"?>
<RelativeLayout xmlns: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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editText1"
        android:text="TextView" />

    <EditText
        android: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.