Andrei Herford
Andrei Herford

Reputation: 18729

How to create a layout where a Button is at least as long as a TextView?

I am trying to achieve the following:

This should look something like this:

Short Label
+-------------+
+-------------+

Very Long Label Text
+------------------+
+------------------+

I tried different solutions with LinearLayout or RelativeLayout as root layout but the problem is always the same: I was not able to fit the width of the Button to the width of the TextView. Giving the Button a minWidth of 150dp is no problem of course. But how to solve the second case with the long label?

<SomeRootLayout ...>
    <!-- also tried LinearLayout, but problem is the same... -->
    <RelativeLayout
        android:id="@+id/editTextAndLabelContainer"
        ...>

        <TextView
            android:id="@+id/labelTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="A " />

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/labelTV" />
    </RelativeLayout>
</SomeRootLayout>

If the Button has a "match_parent" width the width is not aligned the the TextView but set to the complete screen width.

How can I solve this?

Upvotes: 2

Views: 191

Answers (2)

Ranjit
Ranjit

Reputation: 5150

Ok..do something like: layout:

 <LinearLayout
    android:id="@+id/editTextAndLabelContainer"
    android:weightSum="2"
    ...>

    <TextView
        android:id="@+id/labelTV"
        android:layoutWeight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A " />

    <Button
        android:id="@+id/button"
        android:layoutWeight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/labelTV" />
</LinearLayout>

Or as suggest by Lena..

And in your Java code: Add a TextWatcher in your textview ontextchange method..and in side after textchange method do something like:

int txtWidth = your_textViw.getWidth();
int btnWidth = your_button.getWidth();

if(txtWidth >= btnWidth)
  your_button.setWidth(txtWidth);

Upvotes: 1

Lena Bru
Lena Bru

Reputation: 13937

<RelativeLayout
        android:id="@+id/editTextAndLabelContainer"
        ...>

        <TextView
            android:id="@+id/labelTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="A " />

        <Button
            android:id="@+id/button"
            android:layout_alignLeft="@+id/labelTV" //<== add this line
            android:layout_alignRight="@+id/labelTV" //<== add this line
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/labelTV" />
    </RelativeLayout>

Upvotes: 4

Related Questions