Is it possible to set the Margins of a LinearLayout, programmatically but not with pixels, but dp?
margin); int marginTopPx = (int) (marginTopDp * getResources(). getDisplayMetrics(). density + 0.5f); layoutParams. setMargins(0, marginTopPx, 0, 0); recyclerView.
You can use DisplayMetrics and determine the screen density. Something like this:
int dpValue = 5; // margin in dips float d = context.getResources().getDisplayMetrics().density; int margin = (int)(dpValue * d); // margin in pixels
As I remember it's better to use flooring for offsets and rounding for widths.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With