Set Margin on RecyclerView programmatically
Asked Answered
L

5

17

I need to set the top margin on a RecyclerView programmatically, but I get this exception:

java.lang.RuntimeException: Unable to resume activity java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams

Here is my code:

RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams)recyclerView.getLayoutParams();
int marginTopDp = (int)getResources().getDimension(R.dimen.margin);
int marginTopPx = (int) (marginTopDp * getResources().getDisplayMetrics().density + 0.5f);
layoutParams.setMargins(0, marginTopPx, 0, 0);
recyclerView.setLayoutParams(layoutParams);

If I use the ViewGroup.LayoutParams layoutParams = recyclerView.getLayoutParams as the stack trace suggests, I cannot call setMargin anymore as that method does not exist for ViewGroup.LayoutParams.

Any help would be appreciated.

Lunulate answered 16/8, 2016 at 22:16 Comment(5)
What is the parent ViewGroup of your RecyclerView?Frizette
The parent holding the recyclerView is a android.support.v4.widget.SwipeRefreshLayout.Lunulate
how about wrapping the recyclerview in something like a linearlayout, then setting margins to the linearlayout?Sangria
While wrapping the recyclerView works, it just seems wasteful to add an additional layout just for that purpose. I have accepted the answer below because it works and does not involve adding a layout just for wrapping purposes.Lunulate
TL;DR get layoutParams from RecyclerView's parent viewCharlottcharlotta
D
26

Try this out. You can refer to this for more info.

ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(mRecyclerView.getLayoutParams());
marginLayoutParams.setMargins(0, 10, 0, 10);
mRecyclerView.setLayoutParams(marginLayoutParams);
Dolley answered 16/8, 2016 at 22:49 Comment(2)
android.view.ViewGroup$LayoutParams cannot be cast to android.view.ViewGroup$MarginLayoutParamsAerophagia
mRecyclerView.requestLayout(); is required to apply changesPoseur
C
8

for me, the parent of recyclerview is a constraintLayout,

        val margins = (rv.layoutParams as ConstraintLayout.LayoutParams).apply {
        leftMargin = 0
        rightMargin = 0
    }
    rv.layoutParams = margins

or it will get cast error, viewGroup.LayoutParams can't be cast to COnstarintLayout.LayoutParams

Coheman answered 23/10, 2019 at 12:56 Comment(0)
G
1

You need use new object of MargingLayoutParams

    final FrameLayout.MarginLayoutParams marginLayoutParams = new      
                      FrameLayout.MarginLayoutParams(rvContacts.getLayoutParams());
    marginLayoutParams.leftMargin = left;
    marginLayoutParams.topMargin = top;
    marginLayoutParams.rightMargin = right;
    marginLayoutParams.bottomMargin = bottom;

    recyclerView.setLayoutParams(marginLayoutParams);
    recyclerView.requestLayout();
Grasso answered 29/12, 2016 at 11:44 Comment(0)
A
1

I am also facing this type of issue, Use below code to handle margin issue of recyclerview.

    ViewGroup.MarginLayoutParams marginLayoutParams=
    (ViewGroup.MarginLayoutParams) recyclerView.getLayoutParams();

    // here i try to set only top margin, Get dimension from dimension file.
    int topMargin =  getResources()
    .getDimensionPixelSize(R.dimen.top_margin);

    marginLayoutParams.setMargins(marginLayoutParams.getMarginStart(),
    topMargin, marginLayoutParams.getMarginEnd(), marginLayoutParams.bottomMargin);

    recyclerView.setLayoutParams(marginLayoutParams);
Anglofrench answered 5/6, 2020 at 15:54 Comment(0)
R
0

Padding usage with clipToPadding="false" did the trick in my case. [Kotlin]

So I added android:clipToPadding="false" to my RecyclerView in xml. Can be done programmatically as well.

And then just used:

recyclerView.setPadding(10, 0, 0, someSpace)

someSpace is dp variable in case you need it.

resources.getDimension(R.dimen.some_space).toInt() P.S don't forget to create some_space in dimension file :)

Rabassa answered 20/8, 2021 at 15:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.