-2

I am creating a native android app and I am trying to make the textview (clickable button) relative to the text, so if there are a lot of text, the "button" will move to the next line. If there is space, I want to align the "button" next to the text. I am thinking of using relative layout, but don't know exactly what attributes to modify. It would be great if anyone can share his/her expertise.

Example of Alignment

This question is very similar to this post, but it is different because I want the button also to be the same line if there is space (the third option).

Community
  • 1
  • 1
Xiaofan Wu
  • 109
  • 1
  • 13
  • I don't think you can make your third shown text + button. The wrapped TextView makes a rectangle, so there's no "gap" to put a button – OneCricketeer Jan 29 '17 at 05:38

1 Answers1

1

Use RelativeLayout is correct, try below

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginRight="16dp"
        android:layout_toLeftOf="@+id/btn"
        android:text="Hello World!asdfasdfasfasdfdasfsadfsfsafasfasfasdasdasdaas" />

    <Button
        android:id="@id/btn"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test"
        android:layout_centerVertical="true"/>
</RelativeLayout>

Result: enter image description here

WenChao
  • 3,586
  • 6
  • 32
  • 46