Search code examples
androidandroid-layoutandroid-linearlayoutandroid-relativelayout

How to make view at the end of textview even if the textview is so long?


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 :(?


Solution

  • 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>