Reduce ListView height when there are fewer items - android

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.

Related

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.

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

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.

GridView Header and Footer with header filled the screen width

Hi I am working on GridView which has header and footer.
In my case, the header's width should fill the screen width like below
But I am getting the header filled the grid items' width.
I have used the library.
What should I have to do to make the header's width match with screen width.
Note : The header only should filled the screen width not the grid items. And the grid items should have right and left margins.
Use the HFGridview
`https://github.com/SergeyBurish/HFGridView/tree/master/HFGridView
By this you will acheve waht you want with little bit customisation. I already implemented that. If you face any problem let me know I will share code with you.

Get size of GridView when creating items within its adapter

I want to arrange a set of custom widgets inside a GridView. There are predefined templates which describe a count of elements and arrangement config. Max width and heigh for each of them have to be provided in order to avoid scrolling. How can I get a size of the displayed GridView when computing items size in adapter's "GetView" method?
You have access to few functions in order to get a GridView Height and Width as any other View. Look at the documentation here.
The size of a view is expressed with a width and a height. A view
actually possess two pairs of width and height values.
The first pair is known as measured width and measured height. These
dimensions define how big a view wants to be within its parent (see
Layout for more details.) The measured dimensions can be obtained by
calling getMeasuredWidth() and getMeasuredHeight().
The second pair is simply known as width and height, or sometimes
drawing width and drawing height. These dimensions define the actual
size of the view on screen, at drawing time and after layout. These
values may, but do not have to, be different from the measured width
and height. The width and height can be obtained by calling getWidth()
and getHeight().
Try doing a quick search on their doc next time, even if it's Xamarin, the Android doc is pretty reliable and you only need some adjustment to make it work.
(e.g.) : grivView.Width; instead of gridView.getWidth(); one being a property on Xamarin to follow C# standards, the other is a function.

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