1

I have this layout, where I have text view and an icon next to it. However, the text is dynamically changing, so sometime it will be too long which push the icon out of the screen. I tried to add weight to the text but it makes the icon on the right side of the screen which I don't want, I just want it right after the text even if the text go to the next line.

There is my code:

  <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/Text"
                    android:layout_marginLeft="25dp"
                    android:text="llllll"
                    />

                <android.support.v7.widget.AppCompatImageButton
                    android:id="@+id/icon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="10dp"
                    android:background="@android:color/transparent"
                    android:src="@drawable/ic_arrow_drop_down_black_24dp" />

            </LinearLayout>

any idea :(?

ROM
  • 153
  • 3
  • 16

4 Answers4

2

You can use ConstraintLayout to handle this.

<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/image"
        app:layout_constraintWidth_default="wrap"/>

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/text"
        app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>
Ben P.
  • 52,661
  • 6
  • 95
  • 123
0

You could use <img> tags in the HTML

to know how to do that see this qution (is-it-possible-to-display-inline-images-from-html-in-an-android-textview)

humazed
  • 74,687
  • 32
  • 99
  • 138
0

Put the text view inside a relative layout with match parent as width and height wrap content. Set text view to the same dimensions ie march parent as width and height as wrap content.

Have the image button in the same relative layout and use alignParentEnd as true. You'll see it always add the end of your text view.

If you choose to do this, set some maxEms and ellipsize end so that the text does not overlap the button. You'll get the value by testing it yourself, depends on the text size usually.

Since you want it as a button I'm suggesting this. If you want it just be an icon with no use, you should look into drawableEnd property of the text view.

Kushan
  • 5,855
  • 3
  • 31
  • 45
  • On a cell phone at 6 am with no sleep, so unfortunately i won't be able to help you with code snippets but i think this should suffice – Kushan Mar 05 '18 at 00:32
0

Just simply add one property line in textview "android:maxWidth" like below :

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/Text"
                android:layout_marginLeft="25dp"
                android:text="llllll"
                android:maxWidth="100dp" //it can be your specific size
                />

            <android.support.v7.widget.AppCompatImageButton
                android:id="@+id/icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:background="@android:color/transparent"
                android:src="@drawable/ic_arrow_drop_down_black_24dp" />

        </LinearLayout>
amit
  • 659
  • 1
  • 8
  • 21