how to properly size an image view within a grid view on java side; android?

android, gridview, sizing

Solution

Create a dimension in XML :

<dimen name="image_size">90dp</dimen>

Then get it from your code :

int size = (int) getResources().getDimension(R.dimen.image_size);
v.setLayoutParams(new GridView.LayoutParams(size, size));

You can also set different dimensions depending on the screen sizes, by creating different `values` folders.

For example, put a large dimension in a folder named `values-w600dp` (which will be used if the width of the screen is greater or equal to 600dp) and a smaller one in the simple `values` folder (you will have two dimens.xml files).

You will find more details in the documentation.

Problem

this is my code to put some image doodads in my GridView ``` public View getView(int position, View convertView, ViewGroup parent) { ImageView v; if(convertView == null) { v = new ImageView(c); v.setLayoutParams(new GridView.LayoutParams(90,90)); v.setScaleType(ImageView.ScaleType.CENTER_CROP); v.setPadding(2, 2, 2, 2); } else { v = (ImageView) convertView; } v.setImageDrawable(c.getResources().getDrawable(ops[position])); return v; } ``` but for smaller machines the layoutParams are too big, does anyone know how i can do like a (90dp, 90dp) for the width and height on the java side?

Original source