Difference between app:srcCompat and android:src in Android's layout XML - android

Whenever I create an ImageView with icon added using Android Studio's Vector Assets, I'm getting an error at the line app:srcCompat="#drawable/ic_play"
When I change the app:srcCompat with android:src, the error is gone but the icon looks pixelated.
What is the main difference between
app:srcCompat="#drawable/ic_play"
and
android:src="#drawable/ic_play"

app:srcCompat
is the most foolproof method of integrating vector drawables into your app.Vector drawables allow you to replace multiple png assets with a single vector graphic, defined in XML. While previously limited to Lollipop and higher devices
Note
As of Android Support Library 23.3.0, support vector drawables can only be loaded via app:srcCompat .
you need to add vectorDrawables.useSupportLibrary = true to your build.gradle file
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
android:src
Sets a drawable as the content of this ImageView.It will display in
its original size. No automatic scaling .

If you are using android:src="#drawable/some_vector" without vectorDrawables.useSupportLibrary = true in build.gradle file and your app have vector images (vector drawable), then while building the apk file Android gradle plugin generates a lot of *.png files for different screens (hdpi, xhdpi...) from each of your vector drawable (only for API =< 19). The result - bigger size of apk.
When using app:srcCompat="#drawable/some_vector" with vectorDrawables.useSupportLibrary = true android uses vector drawable files without generating *.png files.
You can check this with Android Studio apk analyzer tool. Just build apk with and without vectorDrawables.useSupportLibrary = true.
I think this is the main difference.

Use:
app:srcCompat="#drawable/backImage"
The srcCompat attribute is actually defined within AppCompat library.
Important: you will need to add the appropriate namespace for this.
xmlns:app="http://schemas.android.com/apk/res-auto"
Note
What you are getting seems to be just a lint error that can be ignored. I have tried and gotten the same error, but it is working correctly.
You can use tools:ignore="MissingPrefix" to avoid seeing this error, temporarily.
I hope this helps.

app:srcCompat="some_resource"
is refer that it is AppCompatActivity src which comes in support library while
android:src="some_resource"
refers to simple activity.

Vectors and animated vectors were only supported in recent versions of the framework. srcCompat can be used with the compatibility library to make them work, but this only works with the certain views in the support library. Notice that app: is used instead of android:. This means its not part of the framework, but a parameter defined by your app.

Android 5.0 (API level 21) and higher provides vector drawable support so in order to support vector drawables in older versions app:srcCompat was added

Related

PNG generated from XML not found in drawable folder

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

Override drawable resources in sdk

I'm working on android SDK library which external application will use as .aar file, in my SDK i have Drawable resources, the thing is some of the customers that use the SDK want to be able to override some of the Drawables with their own resources.
My question is how this can be done, i tried to put in app module drawable with same name as in sdk module but different image, it seems not stable and random, sometimes it use the actual icon from app module sometimes from sdk.
What i do wrong?
Is this can be fixed with gradle flavors?
Once I had to do it, and to use my own Theme and Styles I had to recreate the intire layout with same name and same components and making the changes that were necessary
After some research i find the solution, in you SDK build.gradle file you have to add config
defaultConfig{
....
....
vectorDrawables.useSupportLibrary = true
}
This will prevent android to create png's from your vector assets

Can't process attribute android:fillColor="#android:color/white" [duplicate]

This question already has answers here:
References to other resources are not supported by build-time PNG generation
(9 answers)
Closed 10 months ago.
I downloaded an icon from google Material.io. while trying to build my project after integrating it, I ran into the error that says: Can't process attribute android:fillColor="#android:color/white"
Here is a screenshot:
In the app build.gradle add the following line within the android section:
defaultConfig{
vectorDrawables.useSupportLibrary = true
}
Check This For Further Detail :Vector drawables overview
Open the drawable you downloaded and replace android:fillColor="#android:color/white" with android:fillColor="#ffffff". In vector drawables the fillColor attribute must be set explicitly and not reference other resources
There are two ways to fix this.
One quick option is to go to the problematic XML file and change android:fillColor="#android:color/white" to android:fillColor="#FFFFFF". The error would disappear immediately. However, this problem would still recur if you have any other file with a similar line in the future.
Here's permanent solution:
Go to your build.gradle file and add this:
defaultConfig{
vectorDrawables.useSupportLibrary = true
}
Sync and the error would disappear immediately.
You should use AppCompatTheme to access to ?attr/colorControlNormal
AS 3.3.2 / gradle-4.10.1
I had the same compiler problem:
Error: java.lang.RuntimeException: java.lang.RuntimeException: Error while processing .../main/res/drawable/ic_white_set.xml : Can't process attribute android:fillColor="#color/selector_tab_color": references to other resources are not supported by build-time PNG generation.
I opened the incriminated file and got the following Lint warning:
Resource references will not work correctly in images generated for this vector icon for API < 21; check generated icon to make sure it looks acceptable.
Inspection info:Vector icons require API 21 or API 24 depending on used features, but when minSdkVersion is less than 21 or 24 and Android Gradle plugin 1.4 or higher is used, a vector drawable placed in the drawable folder is automatically moved to drawable-anydpi-v21 or drawable-anydpi-v24 and bitmap images are generated for different screen resolutions for backwards compatibility. However, there are some limitations to this raster image generation, and this lint check flags elements and attributes that are not fully supported. You should manually check whether the generated output is acceptable for those older devices. Issue id: VectorRaster
Then I checked my build.gradle files and, sure enough, they all had minSdkVersion to 16.
So, as an alternative to the #Bhavesh Moradiya solution, I set minSdkVersionto 21 and the problem was solved.
The drawback is that you lose support for devices with SDK < 16 though.

Compound Android Xml Shape: Consisting of a Line, an Arc and Another Line

Could you please help me draw this shape as one xml file:
The thing is, I would like to have it as one drawable xml shape to be used as a background image.
Please help.
Not really an answer. Just stating that the problem was solved using a vector drawable:
Refs:
How to convert a PNG image to a SVG?
Error importing Vector Asset into Android Studio
Code Analysis Error (Unexpected namespace prefix) after upgrading Android Support Library 23.2.0
https://developer.android.com/studio/write/vector-asset-studio.html#svg
I tried importing the svg in Android studio through the vector assets tool but got numerous errors about the <svg tag not being declared and also an error about a sudden end of file.
The thing is you are not supposed to use *.svg files in android studio.
You have to convert a, e.g. png file to an svg file and the tool you use to convert the svg to a readable xml is svg2android. There is a minimum version for the gradle dependency and some configuration to be done in the app:build.gradle file. The app:srcCompat resource is not immediately recognisable which is why I provided the third link.
Shout out if you need further explanation.

Drawable picked from library instead of my project

I have dependency on a library, which has ic_launcher.png[for all densities] as drawable. My project also has ic_launcher.png for all densities].
I reference the drawable in xml. It works in api level 23,16. I have galaxy tab api 19. It picks up the ic_launcher from the library.
I tried to clean and build. But the issue remained.
How to fix this issue? Apart from the workaround of renaming my drawables.

Categories

Resources