Question

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 ?

Was it helpful?

Solution

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);
}

OTHER TIPS

if you want that values in dip you have to multiply it for the density of the screen You can get it this way:

float density = getResources().getDisplayMetrics().density
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top