Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setLayoutParams dip values

I am trying to use the method setLayoutParams in order to specify the graphical parameters of my table layout, but I am not sure whether the method setLayoutParams takes pixel values, or dip values, especially that in java the values stored seem to be pixel ones, for examples 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 considered as being pixels or are they converted once passed to the method ? Then if they aren't, how can I set dip values in java ?

like image 530
akari Avatar asked Mar 24 '23 08:03

akari


1 Answers

It takes pixels. 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);
}
like image 67
Ken Wolf Avatar answered Apr 06 '23 01:04

Ken Wolf