I'd like to customize one drawable e.g.
/drawable/image.png
only for APis 19 to 21. For later APIs (after 22 incl. I'd like a drawable to be different). Do I need to create directories
drawable-v19,
drawable-v20,
drawable-v21,
drawable-v22 (for the others?)
? It's a bit unclear to me how it works...
You should only need to create directories for the versions that set a different resource. So your resources directory would look something like:
res/
drawable/ --> SDK version below 19
image.png
drawable-v19/ --> SDK version between 19 and 21 (including both)
image.png
drawable-v22/ --> SDK version 22 and greater
image.png
Edit: Seems to be working this way. I've set up a small project with strings.xml for sdk 19 and 22, launched two emulators, with sdk 21 and 25. The SDK 21 one showed the string in the v19 strings, and the SDK 25 the one in v22 strings. Here's a pic with the results:
Hope it helps. (Couldn't test it with sdk lower than 19, but that should take values from the default file, since no qualifier applies.)
You can create drawable folders like drawable-v19, drawable-v20, drawable-v21, drawable-v22 for each custom images for each versions. If the app is running on one of these devices it picks from these matching version custom respective folders or else falls back to the generic drawable folder.
Related
I’m working on a project with minSdk 23 and compileSDK 31. I’m seeing an issue when running the app on API 23 where when loading a vector resource via
ImageVector.vectorResource(id = R.drawable.vectorID)
I receive an error
android.content.res.Resources$NotFoundException: File res/drawable-xxhdpi-v4/vectorID.png
But I can load the drawable successfully using AppCompatResources.getDrawable(LocalContext.current, R.drawable.vectorID)
Looking at the build folder, folders drawable-[x][xx][etc]hdpi are generated with the xml converted to pngs. But there is no -v4 folders generated.
Why are api level 4 folders being generated for a nexus5 api 23 device? Didn't androidx drop all these version tags? Furthermore, any suggestions on how to get around this?
Appcompat version is androidx.appcompat:appcompat:1.3.1
I wonder, when my new project is minSdkVersion 26 (or later), that I can safely delete these mipmap-*dpi folders and their content ic_launcher icons, and then simply with mipmap-anydpi (renamed from mipmap-anydpi-v26) folder containing vector icons (ic_launcher.xml and ic_launcher_round.xml).
It seems no problem to build a such project. Are there any potential problem I don't aware with this?
Event though it builds without issues, there seems to be a difference. I just did the same—upgrade the minSdkVersion to 26, then rename the folders to drop -v26. Afterwards, the app icon looked different on the same Android 12 device I had used before to test the old behaviour.
This comment supports that observation.
I just converted my app icon to be compatible with android o's adaptive icons using the Image Asset Studio in android studio
when I run my project now on my device running API 25 I get the default green android icon and not my icon.
this is my manifest
<application
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:allowBackup="false"
android:roundIcon="#mipmap/ic_launcher_round"
tools:replace="allowBackup"
tools:ignore="GoogleAppIndexingWarning">
and these are the files the image asset studio created
Is this just an Android Studio bug or did I miss something?
I had the same trouble, and solved it by renaming my mipmap-anydpi directory to mipmap-anydpi-v26.
Apparently the ic_launcher.xml file confuses older Android versions, and this hides it from all but O. At any rate, my icons now work on all versions (down to SDK 11, at least).
Solution is to have mipmap-anydpi-v26/ic_launcher.xml for adaptive icons applicable for API level 26 and above and for other API levels put ic_launcher.png (Note: Not ic_launcher.xml) in all mimpap folders.
Explanation:
Here is the basic problem mipmap-anydpi take precedence over all other mipmap-*. So if resource is found in mipmap-anydpi it will always take preference. Now mipmap-anydpi-v26 is a filter above this which says resources from mipmap-anydpi-v26 will always be picked irrespective of devices density only if API level is 26 or more (Oreo).
Now you manifest has android:icon="#mipmap/ic_launcher"
If your device has API level 26 or above android will choose to use mipmap-anydpi-v26/ic_launcher.xml and everything works fine.
The problem happens when API level is less than 26. Android stats looking for resource named ic_launcher. It will never go and search in mipmap-anydpi-v26 because of API level constraint. Next, it would look for the resource in mipmap-anydpi and if not there then the actual density resource Eg. mipmap-mdpi.
Next, you cannot give ic_launcher.xml to android devices less than sdk 26 because it does not know what adaptive icons is.
So the solution is to have mipmap-anydpi-v26/ic_launcher.xml for adaptive icons applicable for API level 26 and above and for other API levels put ic_launcher.png (Note: Not ic_launcher.xml) in all mimpap folders.
I have been faced same issue and resolved as below.
Put your ic_launcher.xml and ic_launcher_round.xml should be in mipmap-anydpi-v26 (make sure you should not have ic_launcher.png/jpg or ic_launcher_round.png/jpg in same folder).
Put your ic_launcher.png should be in mipmap-hdpi/mdpi/xhdpi/xxhdpi/xxxhdpi (make sure you should not have ic_launcher.xml and ic_launcher_round.xml in these same folder).
By doing this, You will not get any errors building/running project.
Hope it helps some one who are same issue...
If still not working, check your XML schmema in , if you use auto import from Android studio, it will not work, it should be /apk/res/android. Here is the code:
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="#mipmap/ic_launcher_background"/>
<foreground android:drawable="#mipmap/ic_launcher_foreground"/>
</adaptive-icon>
I will conclude this in a very simple manner.
I used android studio just to generate the sources (background and foreground layers of images) which is used in my config.xml file.
So it will look like this :-
<icon background="resources/android/icon/ldpi_background.png" density="ldpi" foreground="resources/android/icon/ldpi_foreground.png">
The above configuration will work well with Android API level > 25.
Here comes the main problem for legacy icon in old versions of android
As per cordova official documentation - adaptive icons are not supported in old versions of android - and with above config.xml it will pick foreground part only as a fallback icon which is the reason your icon doesn't look nice in old versions.
So i applied the following fix for that ( as per cordova official documentation)
Add the src attribute with static image icons - so in old versions instead of using the fallback icon it will use that icon and for latest versions it will give priority to adaptive icons and hence problem will be solved for both.
config will look like this after the fix :-
<icon background="resources/android/icon/ldpi_background.png" density="ldpi" foreground="resources/android/icon/ldpi_foreground.png" src="resources/android/icon/drawable-ldpi-icon.png"/>
I have a question about minSDK. I took a project example in here: Contacts Providers.
This project has a minSDK = 5. And in styles.xml some attribute which require higher sdk version like:
android:textAllCaps --> min 14
android:fontFamily --> min 16
But that project so no any error and run fine.
When I copy some style into my styles.xml in my project. It causes an error:
android:textAllCaps requires API level 14 (current min is 11)
...
Can someone help me with the compile error?
You can use those styles in the respective minSDK version by using resource qualifiers. In your resource directory: there is base styles, add folders to res/styles-v14 (for use in ice-cream sandwich and greater, where minSDK is 14) and styles-v16 (so on so forth if you need the styles to be applied when available)
Now when the style needs a higher SDK version, place it in respective style resource directory. textAllCaps goes into styles-v14, android:fontFamily goes into styles-v16.
More can be found here on resource qualifiers ;)
http://developer.android.com/guide/topics/resources/providing-resources.html
Hope this helps buddy, happy coding!
EDIT: previously misunderstood the question/scenario. Sorry!
I think the problem is your Target API level. Make sure that the target api level is at least the level in which the APIs were introduced. In this case, 14.
If you build from command line you would just do
android update project -p -t
(I think what the command does is just update project.properties and sets target=android-14)
My Android project was working fine when I noticed that I had checked SDK version 4.2 when I meant to target Google 2.33. I set the project back to G2.33 (In the manifest it minsdk = 8 targetsdk=11) and now I have errors on two different styles.xml files in two folders named Values-v11 and values-v14.
the styles.xml in the Values-v14 folder has an error on the following line...
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
the st yles.xml in the Values-v11 folder has an error on ...
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
My other projects, that all target 2.33 don't even have these folders. What is the best way of me getting out of this mess?
Thanks, Gary
You could simply delete these folders if you don't want anything specific for SDK level 11, and the one for 14 won't be used anyway if your targetsdk is 11.
If you want to retain the one for api level 11 for custom behaviour in API level 11, you need to set the project to compile with API level 11 - i.e. if in Eclipse, select the project, go to Properties, select Android and then set the project build target to "Android 3.0" or "Google APIs" for API level 11 if you need the Google APIs.