Reputation: 3412
I have an ImageView
which I want to set the size of programmatically. So I don't have the option of putting different-sized images in their respective drawable
folders.
Basically, instead of doing what I've done below, I want to set the LayoutParams
according to the screen resolution. If the device is ldpi
or mdpi
, then 100,100
... if it was hdpi
, then 150,150
etc. How can I achieve this?
ImageVIew imageView = new ImageView(this);
imageView.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
layout.add(imageView);
Upvotes: 1
Views: 4341
Reputation: 11
To find out which densitity the device has, you can use:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch(metrics.densityDpi){
case DisplayMetrics.DENSITY_LOW:
break;
case DisplayMetrics.DENSITY_MEDIUM:
break;
case DisplayMetrics.DENSITY_HIGH:
break;
}
Upvotes: 1
Reputation: 23344
Use DisplayMetrics
to get the device dpi value in pixels -
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
// Display device dpi value of Y in pixels
int screenDPIy = (int)dm.ydpi;
if (screenDPIy <= 180)
{
ImageVIew imageView = new ImageView(this);
imageView.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
layout.add(imageView);
}
else
{
ImageVIew imageView = new ImageView(this);
imageView.setLayoutParams(new ViewGroup.LayoutParams(150, 150));
layout.add(imageView);
}
Upvotes: 1
Reputation: 5911
Use density independent pixels. Works programmatically using the strategy here: Converting pixels to dp
ImageView imageView = new ImageView(this);
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
int height= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
imageView.setLayoutParams(new ViewGroup.LayoutParams(width, height));
layout.add(imageView);
Upvotes: 0
Reputation: 993
once i have used this:
int density= getResources().getDisplayMetrics().densityDpi;
switch(density)
{
case DisplayMetrics.DENSITY_LOW:
Toast.makeText(context, "LDPI", Toast.LENGTH_SHORT).show();
break;
case DisplayMetrics.DENSITY_MEDIUM:
Toast.makeText(context, "MDPI", Toast.LENGTH_SHORT).show();
break;
case DisplayMetrics.DENSITY_HIGH:
Toast.makeText(context, "HDPI", Toast.LENGTH_SHORT).show();
break;
case DisplayMetrics.DENSITY_XHIGH:
Toast.makeText(context, "XHDPI", Toast.LENGTH_SHORT).show();
break;
}
Upvotes: 1
Reputation: 14398
You can use
ImageVIew imageView = new ImageView(this);
int size = getImageSize();
imageView.setLayoutParams(new ViewGroup.LayoutParams(size , size ));
layout.add(imageView);
and your getImageSize
function will be
private int getImageSize(){
int density= getResources().getDisplayMetrics().densityDpi;
int size =100;
switch(density)
{
case DisplayMetrics.DENSITY_LOW:
size = 100;
break;
case DisplayMetrics.DENSITY_MEDIUM:
size = 100;
break;
case DisplayMetrics.DENSITY_HIGH:
size = 150;
break;
}
return size;
}
where getImageSize
return different size as per different density.
Upvotes: 5
Reputation: 1837
First get screen width
, and multiply it with what you want. I multiplied with 0,2
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screen_width = displaymetrics.widthPixels;
int icon_width = screen_width * 2 / 10;
Then
ImageVIew imageView = new ImageView(this);
imageView.setLayoutParams(new ViewGroup.LayoutParams(icon_width, icon_width));
layout.add(imageView);
Upvotes: 1