Stephen McCormick
Stephen McCormick

Reputation: 1796

Aligning TextViews and EditText

I am attempting to align a couple TextViews on either side of an EditText on a single line. Some caveats to this:

  1. I want them to pull tightly to each other, so for the TextView to the right of the EditText I want to make sure there is not a ton of space
  2. Both TextViews may or not be there, depending on the customer setup, but it needs to be determined at runtime. So the fields need to shift if I set the visibility to NONE for either or both of the TextViews.
  3. All 3 fields need to pull right on the screen.
  4. The "suffix" (TextView to the right) will vary in size up to 10 characters, but I would like it to autofill the space needed.

This is what I would like to see:

enter image description here

But this is what I have so far:

enter image description here

As you can see the prefix and suffix ($ and US) are merging together, rather than splitting on each side of the EditText. I have played with the android:layout_height and android:layout_width for all fields concerned without achieving the desired affect. I tried the TableLayout, but since I have the requirement to shift the EditText value right if there is no TextView to the right (the Suffix or "US" in this instance), I don't think that will work (unless I am missing something there) so I have landed with the following, which I think is getting close and which represents the current screen shot above:

<LinearLayout 
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textStyle="normal"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingBottom="5dp"
        android:text="Test" />

    <RelativeLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:focusable="true"
        android:focusableInTouchMode="true">

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="$" />

        <EditText
            android:id="@+id/entry"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:text="12456"/>

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="US" />

    </RelativeLayout>


    <View
        android:layout_marginTop="8dp"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray"
        android:visibility="gone" />

</LinearLayout>

Upvotes: 0

Views: 34

Answers (2)

diegoveloper
diegoveloper

Reputation: 103421

This is what you need :)

 <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true">

        <TextView
            android:id="@+id/name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textStyle="normal"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingBottom="5dp"
            android:text="Test" />

        <RelativeLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:focusable="true"
            android:layout_marginRight="10dp"
            android:focusableInTouchMode="true">

            <TextView
                android:layout_toLeftOf="@+id/entry"
                android:id="@+id/symbol"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:layout_centerVertical="true"
                android:text="$" />

            <EditText
                android:layout_toLeftOf="@+id/currency"
                android:layout_centerVertical="true"
                android:id="@+id/entry"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:text="12456"/>

            <TextView
                android:id="@+id/currency"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_width="wrap_content"
                android:textStyle="bold"
                android:layout_height="wrap_content"
                android:text="US" />

        </RelativeLayout>


        <View
            android:layout_marginTop="8dp"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@android:color/darker_gray"
            android:visibility="gone" />

    </LinearLayout>

Upvotes: 1

joao86
joao86

Reputation: 2076

You should use change to LinearLayout and use android:layout_weight

 <LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="XXdp"
        android:text="$" />

    <EditText
        android:id="@+id/entry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="XXdp"
        android:inputType="number"
        android:text="12456"/>

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="XXdp"
        android:text="US" />

</LinearLayout>

See this https://developer.android.com/guide/topics/ui/layout/linear.html

Upvotes: 1

Related Questions