Android drawable handling based on qualifiers - java

A friend asked me to help debug her app. The app provides drawables for many types of device configurations.
On Samsung Galaxy Mini, the app crashes, because it can't find any matching resources. (If all resources are copied to drawable folder, it fixes the problem.) I'm wondering about the reason of the crash.
The content of the res folder is as follows:
The only places where actual resources are stored are drawable-large-tvdpi and the drawable-normal-* folders. The rest of the folders contain only a test image for debug purposes (e.g. drawable-mdpi contains a picture with the text 'MDPI', so the app can use it to detect where the current device searches for resources). Obviously, the crash is not due to these debug images, but because the app can't find the actual resources referenced by the app's XML.
The Galaxy Mini has a "small" screen and an "ldpi" density. My guess is that since none of the folders that contain actual resources are for "small" screen size (they either have the "normal" or "large" qualifier), Android will not find any matching resource. (Remember that not all folders store the actual resources.) How should I reorganize everything to ensure that the app doesn't crash on any device at all?
(Note that I certainly read the relevant official Android doc.)

Okay, this gets a little confusing, but the issue is (kind of) articulated in the docs. Specifically:
Screen pixel density is the one qualifier that is not eliminated due
to a contradiction. Even though the screen density of the device is
hdpi, drawable-port-ldpi/ is not eliminated because every screen
density is considered to be a match [at first].
and
When selecting resources based on the screen size qualifiers, the
system will use resources designed for a screen smaller than the
current screen if there are no resources that better match (for
example, a large-size screen will use normal-size screen resources if
necessary). However, if the only available resources are larger than
the current screen, the system will not use them and your application
will crash if no other resources match the device configuration (for
example, if all layout resources are tagged with the xlarge qualifier,
but the device is a normal-size screen).
So, the issue is that you are providing density specific resources for normal/large screen qualifiers (e.g. drawable-normal-ldpi), but none for small (drawable-small-ldpi).

Related

Drawable V21 vs V24 vs custom folders for hdpi,mdpi,xhdpi etc.. ? In which folder should I put my images for multi-device support?

I have an android application, In which home page comprises of 8 ImageViews, they act as menu options for users, So where should I put the multi-density images for these ImageViews in custom drawable folders(ldpi,mdpi,hdpi,xhdpi,xxhdpi,xxxhpi) or drawableV21 or drawableV24 ?
It changes due to your need. For images, you can design them for the highest resolution and put them in xxxhdpi folder. Android will automatically downscale resolution for other devices. Also, you can put different drawables for different resolutions. But they should have the same name.
For drawableV21, drawable24 depends on the version you want to run your app. if you put a drawable v24 and not put anything with the same name in the drawableV21 folder. Your app will crash below Android API24.
So you should use this folder to show different drawable or other assets for different API Versions and resolutions.

Background/UI for multiple devices

I’m new to Android programming and I’m stuck with a very specific problem. I want my application to run on multiple screen sizes and densities.
I have several real devices with different screen resolutions (Samsung Galaxy S7 Edge [xxxhdpi], Samsung Galaxy A5(6) [xxhdpi], Samsung Galaxy Note 10.1 tablet [xhdpi].
I made one background and scaled it down for every generalized density. I saved the files as “background.png” in separate folders like suggested in Supporting Multiple Screens.
After testing the APK on the devices, everything worked fine except the background on the S7. It will always load the next lower resolution bitmap (xxhdpi instead of xxxhdpi). I looked countless threads how to fix this problem but nothing worked so far.
The resolutions of my bitmaps are:
drawable-mdpi //320x480
drawable-hdpi //480x800
drawable-xhdpi //720x1280
drawable-xxhdpi //1080X1920
drawable-xxxhdpi //1440X2560
I hope that someone can help me on this one. I’m really losing my mind.
This is a little complex but I'll try to keep it simple. The android system chooses the right resources based on a lot of factors (which you can see when you create a new resource file under the "Available qualifiers" list) which you can group to create a resource shared by different configurations or use only one of them to specify one particular configuration. But you cannot control witch resource the device will use for the most part (there are some you can specify yourself ex: night mode). Your S7 probably uses the wrong resource because probably one of the settings for the display resolution is set at a lower setting (ex: battery saving modes lower resolution, gaming mode.. etc, accessibility settings). And if you want a tablet to use a different drawable then a phone then you should use the "smallest screen width" qualifier since you can specify different screen sizes in Dip(ex: sw320dp is a normal phone, sw440dp is a big pixel phone, sw720dp is a medium tablet and sw1048dp is a bigger tablet). Read more here
I'm also facing like this. My app only works on high resolution mobile only. Then I found that error. I placed my drawable on drawable-hdpi only . For every assets we have to place default drawable in drawable folder
put default drawable in that folder.
drawable
For handling images on the s7 device we can give height and width to the image view. Set the values for height and width from the dimens file. For handling s7 UI need to add the dimens-sw411 file and specify the height and width in that.

What happens if the device's characteristics don't match any of the available resources?

when you make an android app you can store every string in the app in one file, and you can make multiple files, each file will be for specific language or country, and the app will select the right file automatically.
You can do the same thing with drawables for multiple densities.
but what happen when the app don't find the one it needs, for example if there are 4 drawable folders:
drawable-mdpi
drawable-hdpi
drawable-xhdpi
drawable-xxhdpi
and each one of theme contains the same picture but with different size, and the developer used this picture.
What will happen if the developer tried his app in a device with low density, or a device with extra-extra-extra density, I mean what will happen if you launched an app in a device that have characteristics that don't match any of a specific resource qualifiers?
For the narrow case of drawables varying by density, Android will choose the closest density and scale the image.
More generally, this is why for things other than drawables/mipmaps, you always have a master set of resources in the base resource directories (e.g., res/layout/, res/menu/, res/values/), and override them as needed in directories with resource set qualifiers (e.g., res/layout-sw640dp/).
Because, more generally, if you do wind up in a situation where you request a resource and there is no possible match, you get a ResourceNotFoundException.
For example, suppose that you want to support two languages in your app: English and Spanish (and you are willing to ignore regional differences for the moment, such as UK English versus US English). You then have two sets of string resources: English and Spanish. If you put one set in res/values/ and the other in a language-specific directory (e.g., res/values-es/ for Spanish), and the app winds up on a device whose locale is set to something else (e.g., French), the user gets whatever you put in res/values/. If, on the other hand, you decide to put both languages in language-specific directories (e.g., res/values-es/ and res/values-en/), the French user's app crashes with a ResourceNotFoundException when the app tries to load a string resource and cannot find one that works for the device as configured.

How to obtain that a picture have the same dimension on different devices screen in Android?

I am absolutly new in Android development (I am developing my first app so be patient) and I have the following doubt.
I have to insert a picture as background of a view into my application and this image have to be correctly shown on all the different devices having different screen size.
So I found this article: http://www.survivingwithandroid.com/2012/07/how-to-support-multiple-screen-in.html
My doubt is: what exactly I have to do in my project?
I think that I have to do the following steps (but I am absolutly not sure of it so I am asking to you):
1) Into the res folder of my project I create the following subfolder: drawable-hdpi, drawable-ldpi, drawable-mdpi, drawable-xhdpi
2) Then I use Photoshop to change the density of my picture and so I put the 120dpi version of my picture into the drawable-ldpi folder, the 160dpi version of my picture into the drawable-mdpi and so on.
Is it my reasoning correct? It ensure that my picture it is displayed at the same size on different screens?

Android studio - Different device is using the same layout, but everything looks good only on one of them

I have 4 sets of layout xml and corresponding drawbles, including hdpi, tvdpi, xhdpi, and xxhdpi
I have 2 phones to test the app on, one is a virtual Nexus 5x, another one is a physical Galaxy Note 4
The xml layout works out perfectly on the Nexus 5x, but since (I assume) Note 4 has a smaller resolution / different ratio, the content is overlapping a little bit on it.
This app is very different than regular ones since it has a lot of image views in a relative layout arranged in a specific pattern. Problem is that the system is using xxhdpi resource and layout on both of the device. How do I fix it in a situation like this?
Thanks!
I faced the same problem few days ago.Use dimen.xml which is lacated under values folder.Make folder like values-sw600dp and make dimen.xml under this. Define all the relations in there and call it using #dimen/....
Android automatically will choose best match for your device.

Categories

Resources