What is the smallest possible screen width in Android?
Asked Answered
P

4

8

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
}
Preheat answered 31/8, 2015 at 10:33 Comment(3)
Shouldn't Android just default to whatever resource is available in the 'drawable' folder, if the device doesn't fit any of the qualifiers?Champ
@Champ so you are suggesting that i put some images to the drawable folder, and they will be loaded if smallest width is smaller than 320. That seems right.Preheat
I believe that's the case. As for your question on the smallest qualifier I would have thought it would be sw0dp, might be worth testing out.Champ
P
-1

I realized that drawable-sw320dp folder is useless. I put images both in drawable and drawable-sw320dp folders and run it on a device with smallest width 320 dp. The system loads the images from drawable folder, not drawable-sw320dp folder. So, it looks like 320dp is the smallest possible width in android, and you need to put images in drawable folder for those kinds of devices.

Preheat answered 1/9, 2015 at 7:54 Comment(1)
This is not correct. As per the official documentation, the system will choose the resources contained in the drawable-sw320dp folder, as it is more specific. Moreover, there is no documentation about a minimum smallest width in Android, and smaller devices can always came out (someone said wearable?).Effluence
D
2

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.

Dree answered 31/8, 2015 at 10:46 Comment(3)
Thanks, i know that i should categorize images based on densities, but this is a special case and i have to categorize them based on smallest screen size(so that i can understand whether the device is 10-inch tablet etc).Preheat
Ohh,, then its also simple. The folders like drawable-sw320dp, drawable-sw360dp, drawable-sw480dp, drawable-sw600dp, drawable-sw720dp will only be accesed for the images if the device size matches them. If the device is not of any of the following sizes. It will look up in drawable folder. So you can place your images in it.Dree
This is wrong. The system will choose the resources in the drawable-sw<N>dp if the device smallest width is equal or greater than <N>, and the same resources are not contained in other folders with greater <N> which match the same criteria.Effluence
A
1

You need to put your other image files in your drawable folder. No need to create sw0dp folder, it will be ignored.

Aneroid answered 12/9, 2015 at 13:47 Comment(1)
This is wrong. The folder drawable-sw0dp is more specific than the drawable one, so if you have two resources with the same name in those two folder, the one in the drawable-sw0dp one will be chosen.Effluence
E
0

drawable-sw0dp is a valid folder name for Android resources, so yes, you can put your resources there, and they will be preferred over resources with the same name in the drawable folder, as per the official documentation. Since all Android devices have a smallest width equal or greater than 0, the sw0dp selector will not exclude any of them.

So, why to use the drawable-sw0dp folder, and why not simply the drawable one? Suppose that you have a smartphone and tablet project with some resources in common and some not. You can put all the common resources in the drawable folder, and all the "specific" resources in the drawable-sw0dp (for smartphones) and drawable-sw600dp (for tablets) folders. In this way, you can easily check if those two folders contains the same number of files and with the same names, and clearly see which resources are "universal" and which not.

In conclusion, it's only a matter of readability. Everything will work exactly the same if you put all the "universal" and smartphone resources in the drawable folder and the tablet ones in the drawable-sw600dp folder (which is the more intuitive approach), so it's really up to you.

Effluence answered 18/2, 2017 at 14:16 Comment(0)
P
-1

I realized that drawable-sw320dp folder is useless. I put images both in drawable and drawable-sw320dp folders and run it on a device with smallest width 320 dp. The system loads the images from drawable folder, not drawable-sw320dp folder. So, it looks like 320dp is the smallest possible width in android, and you need to put images in drawable folder for those kinds of devices.

Preheat answered 1/9, 2015 at 7:54 Comment(1)
This is not correct. As per the official documentation, the system will choose the resources contained in the drawable-sw320dp folder, as it is more specific. Moreover, there is no documentation about a minimum smallest width in Android, and smaller devices can always came out (someone said wearable?).Effluence

© 2022 - 2024 — McMap. All rights reserved.