Is it possible to set the Margins of a LinearLayout, programmatically but not with pixels, but dp?
I had the same issue and used this technique to solve it:
First, I added an xml file to my res/values folder called dimensions.xml. It looks like this:
5dip
Second, in my code, I got the pixel equivalent of that margin as follows (note I'm using Xamarin so this is C# code, but the pure Java version should be very similar):
int myMarginPx = Resources.GetDimensionPixelSize(Resource.Dimension.my_margin);
Finally, I create my layout params:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.WrapContent);
layoutParams.SetMargins(myMarginPx, myMarginPx, myMarginPx, myMarginPx);