I created a custom viewGroup by extending ViewGroup, but even though I can access LayoutParams I can't seem to use the setMargins attribute for that object.
Asked
Active
Viewed 348 times
0
-
1Possible duplicate of [setMargins method is not working on custom ViewGroup](http://stackoverflow.com/questions/33317439/setmargins-method-is-not-working-on-custom-viewgroup) – Janki Gadhiya Jul 08 '16 at 07:44
2 Answers
1
If you can access LayoutParams
(they are not null
), try casting them to MarginLayoutParams
. Most of the LayoutParams
(e.g. LinearLayout.LayoutParams
, RelativeLayout.LayoutParams
extend MarginLayoutParams
)
MarginLayoutParams params = (MarginLayoutParams) getLayoutParams();
params.setMargins(left, top, right, bottom);
requestLayout();

Bartek Lipinski
- 30,698
- 10
- 94
- 132
0
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
yourView.setLayoutParams(params);
Hope it will help!

Onkar Nene
- 1,359
- 1
- 17
- 23