Resize images in GridView on Android

Josue picture Josue · Jun 1, 2011 · Viewed 9.1k times · Source

I follow this tutorial to create a GridView:

This line:

imageView.setLayoutParams(new GridView.LayoutParams(85, 85));

Size 85x85 is great for my Wildfire, but on HTC Desire it looks very small. How can I change the image size according to the screen?

I have 2 different layouts, but Gridview hasn't any attribute in the XML files to change the image size.

Answer

Will Tate picture Will Tate · Jun 1, 2011

You could try to make an adjustment based off the pixel density of the current device you are on.

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch(metrics.densityDpi){
     case DisplayMetrics.DENSITY_LOW:
                imageView.setLayoutParams(new GridView.LayoutParams(lowVal, lowVal));
                break;
     case DisplayMetrics.DENSITY_MEDIUM:
                imageView.setLayoutParams(new GridView.LayoutParams(medVal, medVal));
                break;
     case DisplayMetrics.DENSITY_HIGH:
                 imageView.setLayoutParams(new GridView.LayoutParams(highVal, highVal));
                 break;
}