How to set Card View margins programmatically (Grid Layout Manager)

preview_player
Показать описание
In this tutorial, I explain step by step setting up proper margins for each items based on it's position inside RecyclerView with GridLayoutManager programmatically.

===================

Steps:

1. Create RecyclerView element
2. Create Item layout (FrameLayout as Parent) with CardView
3. Create Item adapter (extending RecyclerView.Adapter)
4. Identify each item's position (most top items, most bottom items, left side item, and right side item)
5. Create unit converter to easily identify margin size in pixel (px) with density-independent pixel (dp)
6. Customize CardView margins based on it's position.

Please comment below if you have any concern or better idea.
Thank you for watching!
Рекомендации по теме
Комментарии
Автор

exactly what I was looking for.. thank you a lot

samshouman
Автор

Hello sir thank you for this useful tutorial. My application gives an error in this line
"FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams)
and the error is
Attempt to invoke virtual method on a null object reference
How can I solve this.

md.ruhulamin
Автор

not a value code this can be easily done giving padding in recycler view and margin in adapter

AMUPYSCREATIONS
Автор

Sir, please explain as you go along with your code. But, nice try.

mrs_aries