Calculate maximum itemview height in recyclerview before setting data (onBindViewHolder) - android

We have horizontal recycler view. Each item view of recycler view contains a textView.
Width of each item view is constant, 100dp. Height is dynamic & we have to set it as per the max data.
List of string is coming from server & hence any data can come. So, e.g. 1st cell can have text of 1 line whereas 2nd cell can have text of 4 lines.
Requirement:
We have to calculate maximum height of the cell (i.e cell consuming max height due to biggest text) and set that maximum height to all cells of recycler view.
This is required because we want height of all item views to be same & also making biggest text visible to user without getting cut (ellipse).
One of the ways that I know:
Before setting adapter, we can iterate the list containing Strings. We can create a dummy textview (invisible to user & width 100 dp). We can set each of the text in the textview & then get height of the textview. We will save the maximum height to adapter while calling setAdapter.
Is this good approach?
Please help.

Related

Reduce ListView height when there are fewer items

I have ListView to be used in the app widget.
The list view has a fixed height of some 140 dp to accommodate 3 items, now when there is only one item to show, there is space left below the item.
I want to be able to set the list view like this -
set wrap_content and a maximum height.
Is this possible?
when you use fixed height it will always take that height no matter how many items u have.
In that case u can use maxHeight and set it to 140 dp.So when u have less item the height will reduce. If u have shared the code and a image it would have been better.

How to set a constraint layout width of an item in a recyclerview to a percentage of the parents width, not a fixed dp value?

I have a problem with recyclerview items width:
i already looked through android docs and stack so i think, there is no solution to this problem.
i have an ordinary recyclerview with a horizontal gridlayout
sorry, but i dont have enough reputation to embed pictures
Picture of recyclerview
and i want the items of it being shown next to each other, but I want to make the width of the items depend on the width of the parent (in this case, the recyclerview width used in the other fragment).
I can show you what i want by using a fixed value (in this example 200dp)
Picture of the solution I want, but here a fixed value for width is used
However if my constraint of the item is set to "match parent" like here:
Picture constraint set to match parent
The result is, that the width of the items seen in the phone always depends of the width of the recyclerview. in my case its cut nearly in half:
recyclerview cut in half
also android studio only allows one view as a top level layout, so i cannot use a guideline that is set to any %
possible solution to that?
If you have width of recycler view (in pixel not dp), just divide it by number of items in a row, and in your adapter in "onBindViewHolder" method set item width. this solution is when all of your item has same width.
In addition for height of every item use WRAP_CONTENT.

Android - GridLayout cell sizes

I am trying to use GridLayout with ImageViews. My problem is that the images I have are all different sizes. I have set the row count and column count, but somehow I need to set a fixed cell size (somehow uniform - as in cell width = total / num of columns, and cell height = total / num of rows). I want to do this so that when I set height and width of imageview to match_parent, the image view doesn't expand beyond the cell size and the layout looks uniform.
I need 5 rows and 3 columns. Can someone suggest how to do this ?
I answered a similar question here. The theory is to use a wrapper layout for your cell and provide the margins that it needs to it. Then use the imageview inside this layout. Thus the outer layout acts as a container for your imageview and it will never go out of the bound of its parent.

Check if a TextView is completely visible

I have a ListView with an adapter attached. The data behind it is a couple of articles. The articles have a title and a subtitle, the length of both varies.
Sometimes the text of either one is so long that the TextView doesn't fit the View created by the adapter which has a fixed height.
Is there a possibility to find out if both TextViews are completely visible within the View?
I know I would have to wait for the layout to be drawn, would do it with getViewTreeObserver().addOnGlobalLayoutListener(...)
It seems like you can only completely fit two lines per title and subtitle. If I were you, I'd measure the length of the text with the paint that's used to draw it and see if it's less than (width of line) * (number of lines).
For example, consider the following:
boolean doesTitleFitBounds = titleTextView.getPaint().measureText(titleText) < (TITLE_LINE_WIDTH * TITLE_NUM_ROWS);
where TITLE_LINE_WIDTH is the available width for your text in pixels (accounting for paddings/margins etc) and TITLE_NUM_ROWS is the number of rows you have per title.
Similarly, you can do a check for the subtitle to see if it fits its own bounds.

How to resize the rows so that they always fill the full height of the parent?

I have a parent LinearLayout with a fixed size. At onCreate() I load data and add rows to the parent layout dynamically. Anywhere from 1-10 rows are loaded. I want the rows to always fill the parent, for example if I have 2 rows each row should have 50% height of the parent (50% + 50% = 100%). If I have 3 rows each row should use 33% of the parent height etc.
I tried to use an OnGlobalLayoutListener to get the height of the parent and resize the rows dynamically, unfortunately when I run the app the resizing is arbitrary at best. Sometimes the rows get resized, sometimes they get resized after a noticeable delay of about 0.5 seconds.
Is there a better way to achieve this? Maybe already in the XML as for example with layout_weight (given that the rows only get added in code)?
You can do this by setting the weight sum on the parent LinearLayout to the number of rows you have inside of it. If this is a vertical list of items you would then set the height of each row to 0 and set its weight to 1. If this is a horizontal list of items you do the same but set the width to 0 and set the weight to 1.

Categories

Resources