1

I have been trying to find a solution for this for weeks and keep putting this on my backlist, I am unable to wrap the height of a LinearLayout.

I have a simple LinearLayout

<LinearLayout
android:id="@+id/tagsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="left"
android:paddingRight="10dp"
android:paddingBottom="5dp"
android:padding="5dp"/>

Then I am adding multiple TextViews using my code into that LinearLayout with Tags Link style

Txt.SetBackgroundResource(Resource.Drawable.roundtext);
Txt.SetPadding(30, 10, 30, 10);
Txt.SetTextColor(global::Android.Graphics.Color.ParseColor("#373944"));
Txt.SetTextSize(Android.Util.ComplexUnitType.Dip, 12);

The results are the following enter image description here

See the last TextView never goes to the next line

I looked up so many threads and solutions and it turns out that LinearLayout cannot Wrap contents like this Instead I will have to create my own Layout such as FlowLayout

Is there any simple solution to this problem?

Ali
  • 2,702
  • 3
  • 32
  • 54

2 Answers2

2

You can use GridView instead of LinearLayout.

<GridView
    android:id="@+id/tagsLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numColumns="auto_fit"
    android:horizontalSpacing="5dp"
    android:verticalSpacing="5dp"/>

This should automatically put the TextView to next line. If you want something more dynamic then you use FlowLayout library or StaggeredLayout.Check out this SO which claims that it has alternative to FlowLayout.

Sagar
  • 23,903
  • 4
  • 62
  • 62
  • Hi, thanks for your reply, sound like a good solution but as i am adding `TextViews` such as `myGridView.AddView(TEXTVIEW)` i get error `Java.Lang.UnsupportedOperationException` seems like `AddView` is not supported – Ali Jun 09 '18 at 13:50
  • Used the ArrayAdapter to add items into GridView but it is not giving the desired results, it just divides textboxes to Columns. – Ali Jun 09 '18 at 14:06
  • @aliusman use custom adapter as Indicated in this [SO](https://stackoverflow.com/questions/21107064/how-to-add-view-to-gridview-programmatically-android) or Array adapter with default Ids as shown in this [blog](https://android--code.blogspot.com/2015/08/android-gridview-add-item.html) – Sagar Jun 09 '18 at 14:15
  • @aliusman Check out this [SO](https://stackoverflow.com/questions/47240981/dynamically-add-boxviews-to-grid-xamarin-forms) for Xamarin – Sagar Jun 09 '18 at 14:18
  • I used `ArrayAdapter` which works (sorta) it actually divides text into columns where i am looking for the solution like this https://i.stack.imgur.com/yFf2W.png – Ali Jun 09 '18 at 14:19
  • @aliusman Try to reduce the spacing to `2dp` and check if it works. But it would be still column based. Not sure you would be able to achieve your intended output. – Sagar Jun 09 '18 at 14:23
  • reducing spacing gives the same results, – Ali Jun 09 '18 at 14:25
  • @aliusman Have you checked out [FlowLayout](https://github.com/nex3z/FlowLayout) ? Looks like this library is what you want. – Sagar Jun 09 '18 at 14:26
  • yeah, I mentioned in my comment that if there is another solution, other then the `FlowLayout` – Ali Jun 09 '18 at 14:28
  • @aliusman You can also check out [StaggeredLayout](https://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager). Sometime ago I came across this [SO](https://stackoverflow.com/questions/4474237/how-can-i-do-something-like-a-flowlayout-in-android) which claimed it has alternative for it – Sagar Jun 09 '18 at 14:33
  • that's very interesting, You should change your answer to this - I will make it accepted – Ali Jun 09 '18 at 14:34
2

Take a look at flexbox-layout. Easy to use and import with Gradle.

user1504495
  • 666
  • 8
  • 17
  • thanks for your reply, any xamarin related ideas, I found Binding android libraries in C# is so painful – Ali Jun 09 '18 at 14:05
  • Sorry, didn't see you needed a Xamarin solution and unfortunately I haven't got an answer for that. – user1504495 Jun 09 '18 at 14:14
  • I voted this up as I figured out that there is already a c# binding available for this. Thanks for your suggestion – Ali Jun 09 '18 at 16:16