Android drawable resources issue - android

I've noticed that real device picks up different resource from that eclipse shows in preview.
For example I created drawable resources for devices with smallest width of 720dp as
drawable-sw720dp-mdpi for Samsung Galaxy Tab 1280x800 mdpi
drawable-sw720dp-hdpi for Nexus 10 2560x1600 hdpi.
But Samsung Galaxy Tab 1280x800 mdpi picks up the drawable-sw720dp-hdpi instead of drawable-sw720dp-mdpi ?
Same thing with other devices. If there are resources drawable-sw320dp-hdpi, drawable-sw320dp-xhdpi, the Sony Ericsson xperia-ARC 854x480 hdpi picks up drawable-sw320dp-xhdpi ?
I cant just keep drawable-hdpi resource because there are devices 5'5 inch screen size. For example samsung galaxy 1280x720 hdpi 5'5 inch, and for such devices there is different graphical design so both devices 854x480 hdpi and 1280x720 hdpi will be using the same directory drawable-hdpi wich is not acceptable.
Any ideas?

Some time ago I had similar problem, while writing animated wallpaper which was using canvas and bitmaps. I couldn't achieve what I wanted using default layout folders.
In my case, for example, medium density device with xlarge screen (1280x800), should use the same resources as high density with normal screen (600x1024).
I've decided to write my own solution, by putting resources into three groups and selecting them using custom rules.
DisplayMetrics metrics = resources.getDisplayMetrics();
int screenSize = resources.getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
switch (metrics.densityDpi) {
case DisplayMetrics.DENSITY_LOW:
if (screenSize == Configuration.SCREENLAYOUT_SIZE_SMALL || screenSize == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
return new SmallResourceProvider(resources);
}
case DisplayMetrics.DENSITY_MEDIUM:
if (screenSize == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
return new MediumResourceProvider(resources);
}
default:
return new LargeResourceProvider(resources);
}
Maybe this will be helpful in your case.

I was also facing the same strange issue. In my case Galaxy tab GT P7500 was picking images from drawable-sw720dp-XXHDPI folder as i had the XXHDPI folder also. If i deleted the XXHDPI, it picks up the next lower which is the XHDPI folder.
The issue is with the swxxxdp qualifier for drawable folders. If i remove the screen width and use just 1 set of images of all densities the issue seems to be resolved. However i wanted to have different set of drawables for 7 inch and 10 inch devices. What i did was use a different set of names for the 10 inch images and 7 inch images and use 2 different set of layouts with sw600dp and sw720dp qualifiers.
Here is my current structure which works well. However this is not the expected way of using the resources, as android recommends externalizing the drawables. unfortunately i couldn't find a better solution.
layout-land
layout-sw600dp-land (uses image.png)
layout-sw720dp-land (image_large.png)
and for drawables,
drawble-mdpi (image.png & image_large.png)
drawable-hdpi
drawable-xhdpi
drawable-xxhdpi

Related

Choosing layout folder for supporting all devices

I got a problem related to the layout folder for some device.
So at first I had layout and layout-large folder. I only have 3 xml files for layout inside of these layouts.
layout folder has xml files with using Eclipse configuration of Galaxy Nexus 4.65" (720x1280 xhdpi)
layout-large folder has xml files using Eclipse configuration of 5.1" WVGA (800x480 mdpi)
I tested it on a small Samsung Galaxy Youth 3.3" (320x480), and it's using layout folder... I don't really understand about screen res, I read it in google doc but until now I don't really get enough of it. Google Nexus configuration looked fit for small screen so I chose it without enough understanding in resolution or screen size. Somehow, it fit well in my Galaxy Youth.
While for larger screen device I depend on layout-large folder, so I tested it with Galaxy Tab 7". It fit well too.
Problem arise when I tested it with Galaxy Note 2 and Galaxy Note 4 .. (for other devices I didn't test because I don't have them).
Galaxy Note 2 and Galaxy Note 4 seems to use layout folder not the large one (I'm sure it'll fit well in my layout-large).
I actually want 320x480 or smaller screen size devices to all use layout and any devices with larger screen should use layout-large. How to achieve that?
YOU CAN USE CODE TO SELECT YOUR DESIRED layout for specific smallest-WIDTH of DEVICE..IN
onCreate:
Configuration config = getResources().getConfiguration();
if (config.smallestScreenWidthDp >= 480) {
setContentView(R.layout.main_activity_LARGE);
} else {
setContentView(R.layout.main_activity);
}
the Document say about ( sw NUMBER dp) format :" The exact point of this switch will depend on your particular design—maybe you need a 720dp width for your tablet layout, maybe 600dp is enough, or 480dp, or some number between these. "
so you can put in res :
res/layout/main_activity.xml
res/layout-sw480dp/main_activity.xml
Look here: http://stefan222devel.blogspot.sk/2012/10/android-screen-densities-sizes.html
There is more information about screen sizes and densities of android devices

Drawable folder for 8 inch tablets

I am working in an android application and I want to support my application in all devices. But I don't know from which drawable folder 8 inch tablet takes images.
"drawable-mdpi" for mobiles, containing 320px-width images
"drawable-hdpi" for mobiles, containing 480px-width images
"drawable-xhdpi" for mobiles, containing 720px-width images
"drawable-sw600dp" for 7'' tablets, containing 600px-width images
"drawable-sw720dp" for 10'' tablets, containing 720px-width images
???? for 8'' tablets
You can use : "drawable-sw720dp" for 10'' tablets, containing 720px-width images" for the 8" Tablet .
For more info you can refer here , as 8" is reside between 7-10 Range so that you can use 10" drawable folder
http://developer.android.com/guide/practices/screens_support.html
"drawable-sw600dp" for 7'' tablets, containing 600px-width images
"drawable-sw600dp" is the folder that applies the drawable properties of device with minimum 600dp width.
So you dont need to worry about it.The android system will automatically suites drawable from sw600dp to 8 Inch Tablets
Have a Nice Day ....
The qualifiers hdpi,xhdpi,xxhdpi describes the screen density of the device, not the size of screen. From the official doc
The better practice is to put the following drawables
// for Phones
drawable-ldpi
drawable-mdpi
drawable-hdpi
//for 7 inch tablets
drawable-large-mdpi
drawable-large-hdpi(for Nexus 7)
// for 10 inch tablets
drawable-xlarge-mdpi
The drawable for 8" inches should be implemented if the App uses large background images where should be taken in a serious importance for memory allocation.
U can easily categorize your Drawables to large-mdpi, large-xhdpi , large-port-xhdpi(..) but u are in a situation where u delete the small , normal large and u implementing a swallest width of 600, 720 as any android delevopers will customize early or later.. I really suggest to implement an sw800 where has 1280 x 800!!! and also to make sure in your code to have an ifstatement where directly speaks throw the DisplayMetrics.Density and implements == to 800 widthPixels... This will give an HD performance if the images are recycling and are on in InBitmap to retain instance..

How to include 10" and 7" layouts properly

Nexus 7: 7" 1280x800
Galaxy tab 10.1 10" 1280x800
I want my app to run on 7 and 10 inch tablets. As far as I know, I have to include these layout folders in my app:
for 7 inch tablets
layout-sw600dp
layout-sw600dp-port
for 10 inch tablets
layout-sw720dp
layout-sw720dp-port
It runs fine on the nexus 7, but loads the sw600dp layouts on the 10" tablet.
If I include these default folders:
layout
layout-port
10" galaxy tab loads layouts from these.
If I only include the default layout folders and the sw600dp one, it crashes on the nexus7.
How am I supposed to support phones, 7" tablets and 10" tablets, if the 10" galaxy tab won't load the sw720p layouts?
edit:formatting
The problem was, that I had no default layout folder.
I tried getting by, using only the sw600dp and sw720dp folders. I still have no idea why they don't work, but I don't care. I can't use swxxxdp <3.2 anyway, so screw that.
So if you want to write an app, that has to support phones(2.2+), 7inch tablets and 10 inch tablets, use the following oldschool stuff:
layout this is the default, it is needed even if you don't plan to support phones!
layout-large for 7" tablet (works on emulator and nexus7)
layout-xlarge for 10" tablet (works on emulator and galaxytab10.1)
Other people have came to the same conclusion too.
I am also facing such problem in my application. But I found a good solution for this.
I have only one layout for tablet and directory name is layout-sw600dp.
Now, when part came to height and width problems, I have created several different values directory in which i place dimensions and font size and other stubs. So there will be no constant value in layout of tablet screen.
androd:layout_width:"60dp" // i drop this scenario
androd:layout_width:"#dimen/tab_width" // i used this scenario
and your values directory name will be like
values-xlarge
values-large
All the values will be fetched from your values directory. It will not create different layout, but one layout can be used multiple times.
Following are words of Developer.android site.
Configuration examples
To help you target some of your designs for different types of devices, here are some numbers for typical screen widths:
320dp: a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc).
480dp: a tweener tablet like the Streak (480x800 mdpi).
600dp: a 7” tablet (600x1024 mdpi).
720dp: a 10” tablet (720x1280 mdpi, 800x1280 mdpi, etc).
Using the size qualifiers from table 2, your application can switch between your different layout resources for handsets and tablets using any number you want for width and/or height. For example, if 600dp is the smallest available width supported by your tablet layout, you can provide these two sets of layouts:
res/layout/main_activity.xml # For handsets
res/layout-sw600dp/main_activity.xml # For tablets
===
In this, you can see that, layout for 1280*720 is under layout-sw720dp so instead of creating layout-normal-xlarge you should use this thing which lets you to decide differences. Instead of identify differently using layout-large-mdpi and layout-large-ldpi, are't you just identify by its smallest width? Because, android providing drawables directory for different images, only thing is its resolution. And you have above solution.
Edit
Then you must have to develop different layouts. No other option. I found at http://jamil.fluidsoul.net/2011/03/06/creating-android-applications-for-multiple-screen-sizes.
Low density Small screens QVGA 240x320 (120dpi):
layout-small-ldpi (240x320)
layout-small-land-ldpi (320x240)
Low density Normal screens WVGA400 240x400 (x432) (120dpi):
layout-ldpi (240 x 400 )
layout-land-ldpi (400 x 240 )
Medium density Normal screens HVGA 320x480 (160dpi):
layout-mdpi (320 x 480 )
layout-land-mdpi (480 x 320 )
Medium density Large screens HVGA 320x480 (160dpi):
layout-large-mdpi (320 x 480 )
layout-large-land-mdpi (480 x 320)
Galaxy Tab ( 240 dpi ):
layout-large (600 x 1024)
layout-large-land (1024 x 600)
High density Normal screens WVGA800 480x800 (x854) (240 dpi):
layout-hdpi (480 x 800)
layout-land-hdpi (800 x 480)
Xoom (medium density large but 1280x800 res) (160 dpi):
layout-xlarge (800 x 1280)
layout-xlarge-land (1280 x 800)
Yes, you should use layout-dependent folders but also make sure any device independent layouts go in your res/layout folder.
This is mentioned on the Android developer site but to reiterate their point, if you have a layout that is only available in an layout-xlarge folder on an app that supports say large and normal sized devices as well, the app will crash as smaller devices will not be able to find any match for this resource.
Here is a good folder structure, start with:
res/layout
Keep a device-independent layout in there, you can avoid this if you are accounting for every possible qualifier type but this is still the safer option.
If you want to add specific layouts for say 7 and 10 inch tablets, use all of the following:
res/layout-large
res/layout-xlarge
res/layout-sw600dp
res/layout-sw720dp
res/layout-sw800dp
And so on for any specific device screen widths you want to support. It should be noted that sw600 supports the smallest possible width, so avoids the use of the screen width when the device is held landscape. Using the swxxxdp qualifers are preferred but these were added in API 13 so you will still need large, xlarge for older Android OS.
Regarding use of the dpi, be aware that if you ONLY set one density qualifier for a size, so layout-large-mdpi for example, then any devices that match the large qualifier will use layouts from here instead of another folder, this is due to the Best Match criteria, which you can read about here. It will match it as a large device before it will match the density so non-mdpi density screens will still use these layouts.
To counter this, you will have to include folders for whatever other densities you support as well, so layout-large-hdpi following on from the above example, and include in this folder hdpi versions of layouts that you have used in the mdpi folder if you require them to be different.
Avoid duplicating your layouts as well of course, don't copy device-independent layouts into every unused folder if you only need them in res/layout, try and only keep the layouts that need these qualifiers in the folders and organise them properly, making sure your folders are named with the qualifiers in the right order of precedence to prevent using the wrong folder for a density/size combination.
This is very strange, since you are doing the correct thing.
The sw600dp qualifier should be selected by the Nexus 7.
If available, the sw720dp qualifier should be selected by the Galaxy Tab.
Are you sure it crashes when trying to find an appropriate layout? Android may find the correct layout, but something in the layout xml file may be missing and the crash is caused by that.

Android 2.2 - screen resolutions and sizes issue

The android version is 2.2. And the apps will not support drawable-xhdpi with drawable-xlarge folder. However, i have met many issue when I am processing different screen size from different mobile.
The first problem is that I want to know what is the situation that android will load correct image from drawable-ldpi, drawable-mdpi, drawable-hdpi folder? There are one image in each folder (drawable-ldpi, drawable-mdpi, drawable-hdpi).
(1) 240px x 150px for drawable-ldpi
(2) 320px x 200px for drawable-mdpi
(3) 480px x 300px for drawable-hdpi
But why i cannot see correct image if the output device width screen is 480px? The output device only shown 320px width image. Not the 480px image. The issue seems that what the thing i have missing?
Then, i renamed all drawable folder to drawable-small, drawable-normal, drawable-large. It is working that drawable-large can display 480px width image. Will it be good if i do this way?
The second issue is that some samsung device have got different screen resolutions, i would like to know how to support these screen resolution as I want to make 800px width screen displays 800px width image, 900px width screen displays 900px width image? Any method can do different screen resolution? Many people said the folder drawable-xhdpi can solve issue that image can be auto-scaling. I cannot see any image can be auto-scaling. Maybe i have missing some steps.
Kindly advise.
Thanks a lot.
I will try to give you better insight into this.
Screens can be divided into more or less 4 different categories:
Low-density
medium-density (Baseline)
high-density
extra-high-density
You also get xxhdpi these days...
low-density = ldpi (120dpi)
medium-density = mdpi (160dpi)
high-density = hdpi (240dpi)
extra-high-density = xhdpi (320dpi)
Another good example comes from the android developer pages:
http://developer.android.com/images/screens_support/screens-ranges.png
You can find a list of devices and their pixel densities here:
h ttp://en.wikipedia.org/wiki/List_of_displays_by_pixel_density
h ttp://checkscreensize.appspot.com/listdevice.jsp
You will always be designing apps with multiple layouts/drawables to support different devices. Not just Samsung devices, but most brands have different devices with different pixel densities and resolutions.
In short, by providing multiple resources, android will choose the correct one to best fit the current device and if it does not find one in a certain layout or drawable folder, it will simply use the next best layout/resource with that name.
Note, this is to avoid stretching and incorrect scaling of drawables and layouts.
How to support multiple layouts ?
in your manifest file.
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true" />
Provide different layouts or drawables for different screen sizes.
Now, there are multiple ways of doing this and some ways are dependent on the sdk level.
Pre Android 3.2 / API 13 , use layout-small/normal/large/xlarge.
After Android 3.2 / API 13 , use layout-hdpi, or layout-sw600dp etc.
Here is the official table of qualifiers you can use, by placing a dash between them:
http://developer.android.com/guide/practices/screens_support.html#qualifiers
Generally, I think that you will only design for landscape mode with tablets, therefore, you could consider using only these and:
layout-ldpi
layout-mdpi
layout-hdpi
layout-xhdpi
layout-large
layout-large-land
layout-xlarge
layout-xlarge-land
and for your drawables:
drawable-ldpi
drawable-mdpi
drawable-hdpi
drawable-xhdpi
Further, by default, I currently choose to design for 6 different devices that overall fits best in the profiles ldpi, mdpi, hdpi, xhdpi for phones (4) and tablets (2):
Samsung Galaxy S3 (720x1280, xhdpi, Normal screen size)
Samsung Galaxy S2 (480x800, hdpi, Normal screen size)
Samsung Galaxy Ace Plus (320x480, mdpi, Normal screen size)
Samsung Galaxy Mini (240x320, ldpi, Normal screen size)
Samsung Galaxy Tab 10.1 (800x1280, mdpi, X-Large screen size)
Samsung Galaxy Tab 7.0 (600x1024, hdpi, Large screen size)
Designing for these devices ensures that the app will work great on most other devices aswell.
Hope this information helped.

Android different screen support

I want some help of different screen support. Also want to know the advantage of 9 Patch image over .png image use in the drawable folder.
I recommend you to use this directory structure:
drawable-mdpi // 320 x 480 - Galaxy Ace
drawable-hdpi // 480 x 800 - Desire, Nexus One
drawable-xhdpi // 720 x 1280 - Sony Xperia S, Galaxy S
drawable-large-nodpi // 800 x 1280 - Nexus 7
drawable-xlarge-nodpi // Galaxy Tab, Xoom
Note: nodpi is necessary to prevent resources scaling.
In general it is not guaranteed that tablet will come with hdpi density screens (especially cheap 7"). Resolution and density are two separated beasts, even they look like come together. Please read this article Supporting Multiple Screens. I strongly recommend the whole one, however section "Using configuration qualifiers" shall help you addressing your issue, as instead of relying on density, you could consider putting your resources in resolution qualified resource folder instead (depending on target API, as these appeared in HC)

Categories

Resources