Resize images in GridView on Android
Asked Answered
T

2

5

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.

Teacup answered 1/6, 2011 at 12:56 Comment(0)
A
8

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;
}
Adlib answered 1/6, 2011 at 13:3 Comment(3)
Perfect, this answer is perfect. Thanks!Teacup
developer.android.com/reference/android/app/…Adlib
Syed, If you are using fragments you'll have to get the Activity first: getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);Marigraph
R
6

Similar to willytate's answer, but you're better off creating a dimension value in an xml (for 85dp) and retrieving it like so:

  int size = resources.getDimensionPixelSize(R.dimen.your_dimension);

That way, the width/height will be sized consistently, without you having to worry about much.

Regain answered 1/6, 2011 at 13:11 Comment(1)
This is a nicer way of doing itNeese

© 2022 - 2024 — McMap. All rights reserved.