SetLayoutParams dip values

I'm trying to use the setLayoutParams method to specify the graphical parameters of my table layout, but I'm not sure if the setLayoutParams method accepts pixel values ​​or dip values, especially in java, the stored values ​​seem to be pixel values, for example, if I declare these two variables:

private int blockDimension = 50; private int blockPadding = 2; 

And then call the method:

 tableRow.setLayoutParams(new LayoutParams( (blockDimension + 2 * blockPadding) * numberOfColumnsInMineField,blockDimension + 2 * blockPadding) ); 

Are they treated like pixels or are they converted after passing to the method? Then if this is not the case, how can I set dip values ​​in java?

+4
source share
2 answers

Pixel required. To convert dp to pixels, you can use this:

 public int getPx(int dimensionDp) { float density = getResources().getDisplayMetrics().density; return (int) (dimensionDp * density + 0.5f); } 
+7
source

if you want these values ​​in dip you need to multiply by screen density. You can do this like this:

 float density = getResources().getDisplayMetrics().density 
0
source

Source: https://habr.com/ru/post/1489121/


All Articles