Search code examples
androidtextviewwidth

Android - Move textView if it doesn't fit


I have 3 TextViews inside a linearLayout (horizontal), and when the last textview is too big or there is no space for it, it is matching parent and looking like the image below:

Image Here

What I want is to, IF the textview reaches the linearlayout's width, then, the textview should be moved below "Property" textView. (next line)

Take a look at how it should be:

Image here

That's a snippet of my XML:

  <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="5dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearance"
                    android:text="Property"
                    android:id="@+id/txtPropertyType" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearance"
                    android:text="/ 127 m"
                    android:id="@+id/txtArea"
                    android:layout_marginLeft="10dp" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearance"
                    android:text="/ 3 roomsssssssssdddd"
                    android:singleLine="false"
                    android:id="@+id/txtNumRoom"
                    android:layout_marginLeft="5dp" />
            </LinearLayout>

Thanks in advance x)


Solution

  • Modify your xml and add another TextView below the LinearLayout:

            <LinearLayout
    
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="5dp">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearance"
                    android:text="Property"
                    android:id="@+id/txtPropertyType" />
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearance"
                    android:text="/ 127 m"
                    android:id="@+id/txtArea"
                    android:layout_marginLeft="10dp" />
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearance"
                    android:text="/ 3 roomsssssssssdddd"
                    android:singleLine="false"
                    android:id="@+id/txtNumRoom"
                    android:layout_marginLeft="5dp" />
            </LinearLayout>
    
            <TextView
                android:visibility="gone"
                android:layout_below="@+id/linearLayout"
                android:id="@+id/long_room"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="dfgdfdfgddfgdfgdd"/>
    

    Now, you have 2 options:

    OPTION 1: Check the length of the room number textview. If it's more than, say, 12 (you have to find the number considering all letters to be widest ones - like w), then make the visibility of txtNumRoom GONE and that of long_room visible.

    OPTION 2: Find the height of the txtNumRoom before it gets rendered and displayed. If this height is greater than height of txtArea, that means the textview has gone multiple-lines. Again, in that case,make the visibility of txtNumRoom GONE and that of long_room visible. Refer to this question to know how to find height of a textview programmatically before it gets rendered.