-1

I have a textview inside a grid layout and I want to change the size of grid cells according to the text area of the text view. If the word size is 5, then the grid size will be calculated accordingly. I am adding both the textview and the grid dynamically. I used the below code for this text area calculation,

        text[i].setText(String.valueOf(finallist.get(i)));
        text[i].measure(0, 0);
        int widths = text[i].getMeasuredWidth();
        params.width=widths;
        text[i].setLayoutParams(params);
        text[i].setTextSize(10);

where 'params' is the layout params for the textview. When I run this code, I am getting output as shown below.

output

Here I am getting extra spaces in between the grids no matter the textview size is wrapped. Instead I wanted the grid layout to occupy the area if it's available. I wanted the output like this,

expected

I wanted equal spacing between the grids. How can I achieve this DYNAMICALLY .Also I am implementing drag and drop here. I am new to this. Please help. Thanks in advance.

Anusha
  • 939
  • 3
  • 13
  • 31
  • No need to use grid layout and calculate size of text etc...stuff !! Just use chip text view , google on it ,it will gives you output as you want . you can refer https://github.com/Plumillon/ChipView this. – Radhey Nov 10 '16 at 05:07
  • I am placing the textview inside the gridlayout. Will the grid adjust its size when i use chip text view ? @Radhey – Anusha Nov 10 '16 at 05:08
  • I am implementing drag and drop in grid layout too. @Radhey – Anusha Nov 10 '16 at 05:09
  • make one demo project first , remove your gird view and set chip text for sample PLUS perform drag and drop operation on textview ,as you can get click events of text also , as per mine opinion no need to use grid view as you want only textual data . – Radhey Nov 10 '16 at 05:13
  • Any possibility we could do using rowspan or column span or row spec @Radhey – Anusha Nov 10 '16 at 05:16
  • Amm ! using row span ,you can't get the exact wrap content of your text view .To check this , you can make one xml file and ad sample data and cross check this . – Radhey Nov 10 '16 at 05:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127760/discussion-between-radhey-and-anusha). – Radhey Nov 10 '16 at 05:21
  • use flowlayout instead of grid layout – Anand Savjani Nov 10 '16 at 05:22

3 Answers3

0

Use StaggeredGridLayoutManager instead of normal GridLayout. A good example of Staggered Grid layout manager will be found here.

A LayoutManager that lays out children in a staggered grid formation. It supports horizontal & vertical layout as well as an ability to layout children in reverse. Staggered grids are likely to have gaps at the edges of the layout. To avoid these gaps, StaggeredGridLayoutManager can offset spans independently or move items between spans. You can control this behavior via setGapStrategy(int).

Hope it helps. Cheers!

O_o
  • 1,103
  • 11
  • 36
  • Any chance I could do it using rowspec , colspec or span? I am implementing drag and drop in grid layout @aa_oo – Anusha Nov 10 '16 at 05:06
  • @Anusha, I am not sure. I have never worked with drag n drop grid layout. sorry. But I am pretty sure you can, since it's android! :p – O_o Nov 10 '16 at 05:08
0

you can use GridLayout.Spec as it has both rowSpec and columnSpec properties.Also try setAlignmentMode asit used for all of the alignments between the children of its container.

Real73
  • 490
  • 4
  • 13
  • Can you give me an example for this @Shahrair Nazim Real – Anusha Nov 10 '16 at 05:32
  • why not!! [How to use GridLayout.Spec for a column extending multiple rows?](http://stackoverflow.com/questions/35924875/how-to-use-gridlayout-spec-for-a-column-extending-multiple-rows) and [Set ColumnSpan and ColumnWeight programmatically](http://stackoverflow.com/questions/35234271/set-columnspan-and-columnweight-programmatically) Also [Android: Create grid with dynamic size in gridlayout or gridview](http://stackoverflow.com/questions/33314550/android-create-grid-with-dynamic-size-in-gridlayout-or-gridview) is what you want :) – Real73 Nov 10 '16 at 05:47
0

You should consider FlexboxLayout. Take a look at the readme at the GitHub project site. I think you will find that it will fit your needs. There is also a FlexboxLayoutManager for RecyclerView. This is all supported by Google.