Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the smallest possible screen width in Android?

I am trying to load some images based on smallest width attribute. To do this, I put the images into the following folders:

drawable-sw320dp 
drawable-sw360dp
drawable-sw480dp 
drawable-sw600dp 
drawable-sw720dp

But I wonder what the smallest size should be to avoid crashes at runtime. What happens if a device (if exists) with smallest width 240 runs my app (it probably crashes at runtime)? Can I make a folder named

drawable-sw0dp

and put some images in it so that if smallest width attribute is between 0 and 320, those images are loaded?

I can do this programmatically like the following, but I wonder whether I can do this without needing to write any code.

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
float scaleFactor = metrics.density;
float widthDp = widthPixels / scaleFactor;
float heightDp = heightPixels / scaleFactor;
float smallestWidth = Math.min(widthDp, heightDp);

if (smallestWidth < 320) {
    //Load necessary images
} 
else if (smallestWidth >= 320 && smallestWidth < 360) {
    //Load necessary images
}else if (smallestWidth >= 360 && smallestWidth < 480) {
    //Load necessary images
}else if (smallestWidth >= 480 && smallestWidth < 600) {
    //Load necessary images
}else if (smallestWidth >= 720) {
    //Load necessary images
}
like image 878
yrazlik Avatar asked Aug 31 '15 10:08

yrazlik


1 Answers

You should categorise the images on the basis of screen densities. Not on the basis of screen width.

There are few types of screen densities :-

ldpi ~ 120dpi

mdpi ~ 160dpi

hdpi ~ 240dpi

xhdpi ~ 320dpi

xxhdpi ~ 480dpi

xxxhdpi ~ 640 dpi

Every device is categorised between them only.

Their folders are like :-

drawable-ldpi

drawable-mdpi

drawable-hdpi

drawable-xhdpi

drawable-xxhdpi

drawable-xxxhdpi

Images for smallest screen densities in ldpi. Images for largest screen densities in xxxhdpi.

Other things will be managed by android itself. It will load the correct image depending on the screen density

Always remember mdpi is said to be the base line. That is 1:1 ratio, means in mdpi, 1 px = 1dp.

like image 93
Akshay Sharma Avatar answered Nov 03 '22 14:11

Akshay Sharma