Be carefull, as you can read one the Reference: setMargins(int left, int top, int right, int bottom)
method use px
and your layout use dp
. You have to do a conversion like:
// where "dp" is your value dip
int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
(float) dp, getResources().getDisplayMetrics());
// then set your margins
params.setMargins(0, px, 0, px);
See this answer: What is the correct way to specify dimensions in DIP from Java code? and the reverse one: Converting pixels to dp.
Also, you can read another thing one the Reference:
Sets the margins, in pixels. A call to requestLayout() needs to be done so that the new margins are taken into account.
Hope this helps.