1

I want to shift a TextView to next row automatically if it doesn't fit in the current row. For example, I'm having 20 TextViews in a layout and want to set customer names in that. Some customer names are big in length and some are small. Based on the space availability in that row a TextView should get displayed. If no space then automatically it should get move to next row in that layout.

Can anyone help me with this?

Alireza Noorali
  • 3,129
  • 2
  • 33
  • 80
Sabir Syed
  • 422
  • 3
  • 19

3 Answers3

5

There's nothing in the standard Android framework that will let you do this, but there is a very nice external library from Google called FlexboxLayout that will do it for you.

You'd use a FlexboxLayout as the parent for all of your TextViews instead of e.g. a LinearLayout, and it will take care of wrapping them for you.

<com.google.android.flexbox.FlexboxLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:flexWrap="wrap">

    <!-- add your 20 textviews here -->

</com.google.android.flexbox.FlexboxLayout>

Note that it's important to specify app:flexWrap="wrap" because the default is to not do wrapping. I'm not sure why that is, since the whole point of using this library is to get wrapping, but hey.

Ben P.
  • 52,661
  • 6
  • 95
  • 123
  • Dear Ben P, as per your suggestion, I used this code and added 5 text views, but in layout nothing is getting displayed. – Sabir Syed Dec 16 '18 at 11:22
  • you should check my complete answer https://stackoverflow.com/a/59430605/2326640 – Zhar Dec 20 '19 at 20:31
0

Sounds like you are trying to Chipify some text views. You will need some kind of layout manager to measure the views and determine how to display them. Check out this on Github or anything that has to do with ContactChips may be useful. Chips Layout Manager

syntakks
  • 121
  • 1
  • 6
-1

create linear layout using height as wrap_content and width as match_parent and than inside that create Textview inside that and give its height as wrap_content so it will automatically expand itself when your content will increase. its as simple as that.you can check below example.

 <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:padding="4dp"
                        android:singleLine="false"
                        android:textColor="@color/black"
                        android:textSize="16sp" />

                </LinearLayout>

            </LinearLayout> 
androider
  • 102
  • 13