Android app crashes with Drawable resource as background - android

This is my first android project so please bear with me. My app is crashing whenever i change a layout's background, I noticed that the layout won't crash if the background drawable does not include a (v21) next to it. For example setting the background as #drawable/gradient_bg it works fine, on the other hand, #drawable/gradient_bg_purple it crashes the application when the layout is loaded.
What does the (v21) mean ? I'm guessing it has to do with an android API v? Does that mean I should have a different file for all versions ? :/
I'm testing on an HTC - Android V-4.1.2 - HTC SDk level - 5.26
Thanks

v21 means that the resource will be used/available on Android versions 21 and up. So in your case, that resource will be non existent for any Android versions lower than that.
Update
You should take those drawables out of drawable-v21 and place them in the appropriate folder based on your desired density. For example, place it in drawable-mdpi, drawable-hdpi, etc.

v21 represents the minimum API level that will use this drawable.
You are running Android 4.1.2, which uses API v16.
Apparently, you created a drawable-v21 directory in your resources (res) directory.
Please check if that's the case and move the drawable to the drawable directory.
If that is indeed that case, it may be because Android Studio suggested this change and you're using features that are specific to v21 API.
We can provide more help if you show us this gradient_bg_purple.xml file.

Related

Why does Android Studio create the ic_launcher_foreground in res/drawable-v24 and the ic_launcher_background.xml in res/drawable?

Firstly
I noticed when creating a new project in Android Studio it generates drawable-v24 folder with ic_launcher_foreground.xml icon.
And my question is:
If vector drawables were introduced before v24 then why does it put the vector asset in drawable-v24 and not in drawable-v21 folder?
Secondly
Another generated asset is ic_launcher_background.xml which is created in res/drawable folder and was later referenced from mipmap-anydpi-v26/ic_launcher.xml.
If it is only used in API v26+ (adaptive icons) then why was it created in res/drawable folder and not in res/drawable-v24 or in res/drawable-v26? Is it due to some nature of mipmap folders?
The VectorDrawable feature is somewhat integrated in API-24 though it was introduced from API-21. I think Google's developers think API-24+ is ideal or stable for adaptive icons.
For example, Nick Butcher (Android designer + developer # Google) said in his article: Using vector assets in Android apps (Dec 11, 2018),
Both nonZero and evenOdd path fillTypes —the two common ways of defining the inside of a shape, often used in SVGs (evenOdd added to platform impl in API 24)
Gradient & ColorStateList fills/strokes (added to platform impl in API 24)
And about ic_launcher_background, it is referenced only from mipmap-anydpi-v26/ic_launcher.xml and mipmap-anydpi-v26/ic_launcher_round.xml. I think it is just that there is no need to prevent from referencing it directly (It has already prevented by placing ic_launcher.xml and ic_launcher_round.xml in mipmap-anydpi-v26 folder). There should be not so far more, deep meanings.
A codelab answers your questions.
Depending on the min SDK of your app, you may notice that the foreground asset is located in the drawable-v24 folder, while the background asset is in the drawable folder. The reason is because the foreground asset contains a gradient, which was available starting in the Android 7.0 release (also known as API version 24, hence the -v24 resource qualifier). The background asset doesn't contain a gradient, so that can go in the base drawable folder.
As a side note, though we generally don't use ic_launcher_background.xml for any purpose other than the app icon, it is just a vector drawable and its usage is not limited.

Is there a way to target certain Api's in a drawable.xml file?

I created a custom drawable using ripple, which only targets api 21 and above, I know its possible to do it programmatically but is there a way to do it in the drawable itself?
I also tried making a separate folder for higher api's which I heard could be done with layouts but no luck anything is apricated thanks.
You can create multiple resources for different API version and let Android choose one of them based on device's API version. Create a new drawable.xml file but choose API version 21 as a qualifier and put your code on that. Create another drawable.xml with no version qualifier and put another code which runs on pre 21 version.
It sounds like you have already tried it, but this should work. Create a drawable-v21 in your res folder. This should target API 21 and above (or if you have another higher API drawable folder i.e. drawable-v24 your 21 will target from 21 to 23). Place your drawables for that level in this new directory
You can use sth like this:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP)
and then show your drawable.

Android: drawable resource from earlier API level

Is there a way in Android to use a drawable resource from an earlier Android version / API level then the one of the current project? More specifically, is there a way to access it in the project without putting it into the drawable resource folder manually?
In my specific case, I would like to use android.R.drawable.btn_check_buttonless_on from Android 2.2 in a project that uses Android 4.0. Because in 4.0 it's not used anymore, I obviously cannot just reference it like
myMenuItem.setIcon(android.R.drawable.btn_check_buttonless_on);
Is there another way to include the drawable resources of earlier versions?
I found this:
https://androidcookbook.com/Recipe.seam;jsessionid=0443546CEE776318BF6D21552A9D1864?recipeId=3823
many (most?) of the resources are, for one reason or another, marked as non-public; presumably these are intended only for use by particular components. The non-public give a diagnostic that the "Resource is not public". You can still use the non-public one,s but you have to extract the image drawables and copy them into your project.
I'll try it as well.
Edit:
it worked for me:
go to your android SDK/platforms/android-19/
extract android.jar
go to android/res/
there you have all the drawables you want to use (inside the drawable folders).
You have to copy all the .png file into your project.

How can I use system icons in my android app?

I am programming an android application and I'd like to use the system icons for mail, phone, calendar and contacts inside this application. The problem is, that these icons differ from system to system (different android versions and different smartphones) and I want to keep the application and the system consistent. Is there an easy way to achieve this?
In XML use default android drawables from #android:drawable/... and in code use android.R.drawable....
You can use the standard drawable using
#android:drawable/[icon_name] in your xml file
android.R.drawable.[icon_name] in your java code
You can see different icon in your file system in
[SDK PATH]/platforms/platforms/[ANDROID_VERSION]/data/res
Some images are standard...like contacts, calendar :)
You can't keep system consistent. For what? New versions of Android are usually prettier than older ones (well, it's my opinion).
The only way to keep system icons same for your app - copy them into your drawables instead of using direct reference to them (like #android:drawable/).
First of all if you intend to use the default images (icons) for mail , phonr etc.
This default can be used as android.R.drawable.name in code or #android:drawable/ in xml from http://docs.since2006.com/android/2.1-drawables.php
But as this will vary from OS versinos to Versions.
So , if you need to use an unique GUI for the app for all versions and devices.
Please do not use android provided images , simply use your own graphics or images supplied in drawables resources explicitly by you for your app.
If you're ok with icons from each system you can get it via PackageManager and getApplicationInfo method.
If you're not, you should copy the resources from the platform into your own application.

Can't use android spinner drawable?

I'm trying to use one of Android's spinner drawables (spinner_black_16, spinner_black_20, spinner_black_48, or spinner_black_76) as demonstrated on this page. My project is using SDK 2.1, so I updated it to 2.2 as that page states is the SDK version where these are included. Unfortunately, none of the android.R.drawable.spinner_black_xx appear to be defined.
Are these drawables not available for developer usage? The only other way I seem to be able to access them is via the following convoluted method:
final Drawable spinner = new ProgressBar(context).getIndeterminateDrawable();
Unfortunately, that provides me the white spinner, not the black one.
The best way to access those drawables is to copy them from your SDK directory into your project and then access them like any other resources.
The drawables are located in $ANDROID_SDK_DIR/platforms/android-*/data/res/drawable-*/. You can download and select the version of the OS that you'd like to pull drawables from. API level 7/OS version 2.1 has the full complement:
platforms/android-7/data/res/drawable-mdpi/spinner_black_16.png
platforms/android-7/data/res/drawable-mdpi/spinner_black_20.png
platforms/android-7/data/res/drawable-mdpi/spinner_black_48.png
platforms/android-7/data/res/drawable-mdpi/spinner_black_76.png
platforms/android-7/data/res/drawable-hdpi/spinner_black_16.png
platforms/android-7/data/res/drawable-hdpi/spinner_black_20.png
platforms/android-7/data/res/drawable-hdpi/spinner_black_48.png
platforms/android-7/data/res/drawable-hdpi/spinner_black_76.png
Though it doesn't currently, Android's official "Icon Design Guidelines" used to explicitly address this situation and recommend copying:
Because resources can change between platform versions, you should not reference built-in icons using the Android platform resource IDs (i.e. status bar icons under android.R.drawable). If you want to use any icons or other internal drawable resources, you should store a local copy of those icons or drawables in your application resources, then reference the local copy from your application code. In that way, you can maintain control over the appearance of your icons, even if the system's copy changes.

Categories

Resources