Adam Smith
Adam Smith

Reputation: 3

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?

Upvotes: 0

Views: 407

Answers (1)

com_run
com_run

Reputation: 103

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.

Upvotes: 0

Related Questions