0

I have the fallowing drawable called background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape> <gradient android:angle="90" android:startColor="@color/gradient_bottom" android:endColor="@color/gradient_top" android:type="linear" /> </shape> </item> </selector> 

when the xml is under the drawable folder, in some reproducible cases (not sure why, probably has something to do with high memory consumption) the background disappears.

However, when I recreate the scenario and I put the same xml in all the drawable resolution folders, the background doesn't disappear (i.e drawable-hdpi, drawable-xhdpi etc.)

This kind of thing isn't supposed to happen, can someone explain why does it happen and how to fix it more elegantly ?

P.S -

  1. when i put a solid color as a background this issue doesn't happen

  2. the issue is also occurs for a custom button background, and fixed when i put the custom background in the drawable resolution folders

1 Answer 1

1

I recommend to use resource directories with the dpi qualifiers as possible. Otherwise you can get OutOfMemory errors with some devices. Explanation here:

By default, Android scales your bitmap drawables (.png, .jpg, and .gif files) and Nine-Patch drawables (.9.png files) so that they render at the appropriate physical size on each device. For example, if your application provides bitmap drawables only for the baseline, medium screen density (mdpi), then the system scales them up when on a high-density screen, and scales them down when on a low-density screen. This scaling can cause artifacts in the bitmaps. To ensure your bitmaps look their best, you should include alternative versions at different resolutions for different screen densities.


At runtime, the system ensures the best possible display on the current screen with the following procedure for any given resource:

The system uses the appropriate alternative resource Based on the size and density of the current screen, the system uses any size- and density-specific resource provided in your application. For example, if the device has a high-density screen and the application requests a drawable resource, the system looks for a drawable resource directory that best matches the device configuration. Depending on the other alternative resources available, a resource directory with the hdpi qualifier (such as drawable-hdpi/) might be the best match, so the system uses the drawable resource from this directory.

If no matching resource is available, the system uses the default resource and scales it up or down as needed to match the current screen size and density The "default" resources are those that are not tagged with a configuration qualifier. For example, the resources in drawable/ are the default drawable resources. The system assumes that default resources are designed for the baseline screen size and density, which is a normal screen size and a medium-density. As such, the system scales default density resources up for high-density screens and down for low-density screens, as appropriate. However, when the system is looking for a density-specific resource and does not find it in the density-specific directory, it won't always use the default resources. The system may instead use one of the other density-specific resources in order to provide better results when scaling. For example, when looking for a low-density resource and it is not available, the system prefers to scale-down the high-density version of the resource, because the system can easily scale a high-density resource down to low-density by a factor of 0.5, with fewer artifacts, compared to scaling a medium-density resource by a factor of 0.75.

Source

Sign up to request clarification or add additional context in comments.

2 Comments

But why does the background usually works? The xml that I put in the directories with the dpi qualifiers is the same as the original one. When the system takes the default drawable folder, it is the same one, however it seems that it can't find it there or something like this
@BennyP It depends on resolution of device and resource file. When the system takes the resource from default drawable folder, scales it to fit the screen every time. Scaling process uses memory. If scaling process uses too much memory, may cause errors or performance issues. For example, using a background image(default drawable folder) can work on Galaxy S3 but may crash on Galaxy S4. I faced with similar problem about 4-5 years ago.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.