Jeff Axelrod
Jeff Axelrod

Reputation: 28188

Left-align a TextView to the text inside an EditText?

I'd like to align the text in a TextView with text inside an EditText that is directly below it. Is this possible without using padding tricks?

If not, is this because it's discouraged? I could see it being discouraged because it could confuse the user into missing the distinction between the two controls.

enter image description here

Here's the layout that created the above screenshot:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/text_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="Please align my first character" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text_view"
        android:hint="To my first character" />

</RelativeLayout>

Upvotes: 4

Views: 1630

Answers (3)

Evos
Evos

Reputation: 3915

No, you can't do this without using padding or layout_margin. EditText has a background with borders and padding, so its content area is a little bit smaller than the area of TextView. I think it's normal to use padding in such situations.

Upvotes: 1

WSBT
WSBT

Reputation: 36323

I feel you will have to use padding to the TextView. It actually looks better to me that the first character in TextView and that in EditView are NOT aligned perfectly. They also have different font size, so it actually looks pretty natural the way it is right now - or maybe with just a little bit of padding, but not too much. When padding, don't forget to consider users with different screen sizes and resolutions, so please use dp with you are doing padding with android:layout_marginLeft. For example:

<TextView
    android:id="@+id/text_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="5dp"
    android:text="Please align my first character" />

Hope this helps.

Upvotes: 0

Egor
Egor

Reputation: 40193

Actually this can be confusing, but you can add a little offset to the TextView to make it look better. Use android:layout_marginLeft and choose a value that will match your requirements. Hope this helps.

Upvotes: 0

Related Questions