Recently, I want to use Vector Drawable to manage all the icons. Vector Drawable works well on Android API 21+ but when I tried to use it on one of my test device [Amazon KFTHWI Android 4.4.3 API 19], all the icons cannot be loaded at all!
I followed the tips Here and Here. But still it doesn't work....
Here is my configuration:
In project gradle script file:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
classpath ...
}
}
So my gradle plugin version should be 2.0+
In main module gradle script file:
android{
...
defaultConfig{
...
vectorDrawables.useSupportLibrary = true
...
}
}
dependencies{
...
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:gridlayout-v7:23.4.0'
...
}
This main module contains the app. The reason that I compile gridlayout-v7 library is because I use some Gridlayouts in the app.
Here is my implementation:
Because I need to generate some views according to the data I received, I set the image source in java code.
ImageView icon = new ImageView(this);
icon.setImageResource(R.drawable.ic_lock_black_24dp);
ic_lock_black_24dp.xml is generated from Material Icon library in Vector Asset Studio.
My expectation:
I believe my configuration in my main module gradle script file will prevent Android Studio from generating Pngs for Vectors and will allow Android to generate Drawables for Vectors directly at run time.
But I'm only partially correct... My configuration actually prevents Android Studio from generating Pngs while building. BUT my old android device[API 19] cannot generate Drawables from Vectors!! So there is no icons in the app now...
Here is the exceptions: [Amazon KFTHWI Android 4.4.3 API 19]
W/ImageView:Unable to find resource:2130837702
android.content.res.Resources$NotFoundException: File res/drawable/ic_lock_black_24dp.xml from drawable resource ID#0x7f0200c6
at android.content.res.Resources.loadDrawable(Resources.java:2206)
at android.content.res.Resources.getDrawable(Resources.java:707)
at android.widget.ImageView.resolveUri(ImageView.java:651)
at android.widget.ImageView.onMeasure(ImageView.java:774)
at android.view.View.measure(View.java:17038)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
at android.view.View.measure(View.java:17038)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5191)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:17038)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
at android.view.View.measure(View.java:17038)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5191)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
at android.view.View.measure(View.java:17038)
at android.support.v7.widget.GridLayout.measureChildWithMargins2(GridLayout.java:896)
at android.support.v7.widget.GridLayout.measureChildrenWithMargins(GridLayout.java:906)
at android.support.v7.widget.GridLayout.onMeasure(GridLayout.java:945)
at android.view.View.measure(View.java:17038)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5191)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:17038)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1273)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.widget.ScrollView.onMeasure(ScrollView.java:373)
at android.view.View.measure(View.java:17038)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5191)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:17038)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5191)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:17038)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5191)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
at android.view.View.measure(View.java:17038)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5191)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2346)
at android.view.View.measure(View.java:17038)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2004)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1176)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1368)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1057)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5857)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:815)
at android.view.Choreographer.doCallbacks(Choreographer.java:628)
at android.view.Choreographer.doFrame(Choreographer.java:598)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:801)
at android.os.
Ironnically, all the icons will present well if I don't use AppCompat Support Library but I know this is because Android Studio generates Pngs while building. I can even find those Pngs in the APK.
My problem:
Now, I still want to benefit from Vector Drawable in old android devices, which means I'm still trying to avoid using Pngs... Could somebody give me some tips or tell me in which steps I did something wrong?
To support vector drawable and animated vector drawable on devices running platform versions lower than Android 5.0 (API level 21), or use fillColor , fillType and strokeColor functionalities below Android 7.0 (API level 24), VectorDrawableCompat and AnimatedVectorDrawableCompat are available through two support ...
VectorDrawable Class (Android. Graphics. Drawables) | Microsoft Learn.
Android Studio includes a tool called Vector Asset Studio that helps you add material icons and import Scalable Vector Graphic (SVG) and Adobe Photoshop Document (PSD) files into your project as vector drawable resources.
You have to say new AppCompatImageView(this)
. When creating views manually you have to add the AppCompat
prefix.
There is a library supports api 14+ for VectorDrawable and AnimatedVectorDrawable i'm using: https://github.com/wnafee/vector-compat
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With