Support library VectorDrawable Resources$NotFoundException - android

I am using Design Support Library version 23.4.0. I have enabled the gradle flag:
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
I am using build tools version 23.0.2, but still, I am getting Resources$NotFoundException on KitKat or lower.
It is occurring when I use android:drawableLeft or imageView.setImageResource(R.drawable.drawable_image).
And yes, I am putting this on every activity where I am using drawables
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
Is this a bug of the support library?

It took 3 separate things for me to get this to work using support library 23.4.0:
Add this to build.gradle
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
Add the following to onCreate of your Application class
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
(From the reference of this link - "https://stackoverflow.com/a/45582033/10752962")
In API less then 21,use this line before setContentView();
For all XML views in which you are setting a vector drawable replace
android:src
with
app:srcCompat
and in the code replace this:
imageView.setImageResource(...);
with
imageView.setImageDrawable(...);

To complement some of the answers here: backward-compatible support for VectorDrawables comes with a price and doesn't work in all cases.
In which cases does it work? I've made this diagram to help (valid for Support Library 23.4.0 to at least 25.1.0).

Try using:
imageView.setImageDrawable(VectorDrawableCompat.create(getResources(), drawableRes, null));
You don't have to add AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
this way.
Just inflate your vector drawables using VectorDrawableCompat and you're all set.

We had the same issue. Vector drawables were not visible on Kitkat. I solved this issue by adding AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); to the onCreate method of Activities.
Before that dont forget to add:
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
and call setImageResource for the view that you use the vector drawable. My view is ImageButton. I have Android SDK build tools version 23.0.3

Sorry for being late to the party but this answer may help users who want to enable the flag AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); for all activities.
1. Create a class which extends to Application (android.app.Application)
public class MyApplicationClass extends Application
{
#Override
public void onCreate()
{
super.onCreate();
}
}
2. Head over to Manifest.xml and add the following line to your tag
<application
android:name=".MyApplicationClass"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
...
</application>
3. Add the following code above onCreate in MyApplicationClass.java
// This flag should be set to true to enable VectorDrawable support for API < 21
static
{
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
Complete code for MyApplicationClass.java
import android.app.Application;
import android.support.v7.app.AppCompatDelegate;
/**
* Created by Gaurav Lonkar on 23-Dec-17.
*/
public class MyApplicationClass extends Application
{
// This flag should be set to true to enable VectorDrawable support for API < 21
static
{
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
#Override
public void onCreate()
{
super.onCreate();
}
}

defaultConfig {
vectorDrawables.useSupportLibrary = true
}
use this in app.gradle
Then use AppCompatDrawableManager to setDrawable and getDrawable. Works for me

Support for vector drawables in places like android:drawableLeft was disabled in support library 23.3. It was announced on Google+:
we’ve decided to remove the functionality which let you use vector
drawables from resources on pre-Lollipop devices due to issues found
in the implementation in version 23.2.0/23.2.1. Using app:srcCompat and setImageResource()
continues to work.
Links to issues:
https://code.google.com/p/android/issues/detail?id=205236
https://code.google.com/p/android/issues/detail?id=204708
However, if you can live with those issues, in 23.4 you can re-enable this functionality using AppCompatDelegate.setCompatVectorFromResourcesEnabled().
If you're curious how this works, the best person to learn from is Chris Banes, who authored this functionality. He explains in detail on his blog.

change
imageView.setImageResource(R.drawable.drawable_image)
to
imageView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.drawable_image));
if you want to use vectordrawable in xml, use this:
app:srcCompat="#drawable/drawable_image"

I had a similar problem long ago, it did not work by setting
vectorDrawables.useSupportLibrary = true
only worked when I created the "mipmap" folder, and the code used
imageView.setImageResource (R.mipmap.drawable_image)
It has more Info here

Inflating Drawable's
`VectorDrawable` and `AnimatedVectorDrawable` in this support library can be inflated in this way:
Calling static getDrawable() methods:
//This will only inflate a drawable with <vector> as the root element
VectorDrawable.getDrawable(context, R.drawable.ic_arrow_vector);
//This will only inflate a drawable with <animated-vector> as the root element
AnimatedVectorDrawable.getDrawable(context, R.drawable.ic_arrow_to_menu_animated_vector);
// This will inflate any drawable and will auto-fallback to the lollipop implementation on api 21+ devices
ResourcesCompat.getDrawable(context, R.drawable.any_drawable);
If inflating the Drawable in java code, it is recommended to always use ResourcesCompat.getDrawable() as this handles Lollipop fallback when applicable. This allows the system to cache Drawable ConstantState and hence is more efficient.
The library has the following morph (bi-directional) animations :
Play-Pause morph animation
Play-Stop morph animation
Arrow-Hamburger menu morph animation
As you can see, I produced the above image on my API 16 phone:
import com.wnafee.vector.compat.AnimatedVectorDrawable;
mdrawable = (AnimatedVectorDrawable) AnimatedVectorDrawable.getDrawable(this.getApplicationContext(), R.drawable.consolidated_animated_vector);
Look at the github README for vector-compat here: https://github.com/wnafee/vector-compat
This will fix your problem (down to API 14) if you merge it with your app module's build.gradle dependencies (usually at the end of file):
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
//Trying to FIX Binary XML file line #2: invalid drawable tag animated-vector
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:design:25.0.0'
//not needed
// compile 'com.android.support:support-vector-drawable:25.0.0'
compile 'com.wnafee:vector-compat:1.0.5'//*******holy grail *******https://github.com/wnafee/vector-compat
// Failed to resolve: com.android.support:support-animated-vector-drawable:25.0.0
//not needed
// compile 'com.android.support:support-animated-vector-drawable:25.0.0'
}

Do not put your vectors in drawable-anydpi
, old devices does not support that
put them in drawable

In my particular case, I had this problem because I was using a drawable selector as the image resource with several vectors in the selector, as in:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#drawable/vector_select_blue"/>
<item android:state_pressed="true" android:drawable="#drawable/vector_select_black"/>
.
.
etc
</selector>
Yes, pretty bad, but didn't know better at the time.
So, the right way of doing this is using the tint property in your vector file, as in:
<vector ..vector properties..
android:tint="#color/vector_color_selector">
<path ..path properties../>
</vector>
(You can also use the app:tint attribute in the AppCompatImageView)
And now, your vector_color_selector file should have the colors you want, as in:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#color/blue"/>
<item android:state_pressed="true" android:color="#color/black"/>
.
.
etc
</selector>
I hope this helps someone if previous answers didn't work for you. Stating the obvious, but I must say that you still need to set vectorDrawables.useSupportLibrary = true in gradle, use AppCompatImageView and use app:srcCompat or setImageDrawable + AppCompatResources.getDrawable to avoid any troubles with the vector compat library.

Use AppCompatImageView instead of ImageView as said by Harish Gyanani in comments , it works fine with this for me.
Official docs

I had the same problem and actually what was missing is I was using app:srcCompat on AppCompatTextView except of AppCompatImageView.
The way I have found the problematic part:
My error looks like:
Fatal Exception: android.content.res.Resources$NotFoundException
Resource ID #0x7f0700d1
Here are the steps I followed the resource id of the mentioned drawable :
APK Analyzer -> classesXXX.dex
In this dex file I opened the directory of my apps package name and went to R$drawable file
R$drawable -> Show as byte code.
Search for ID [0x7f0700d1] (check your own ID)
Find the image and check for all the usages (CMD + F7) of the resource
Fix
Hope it will help somebody.

Related

Tint does not work <21 version in DataBinding

I am using DataBinding to tint vector drawable of ImageView on basic of an boolean flag. This code works well for >=21 version. But fails in <21 version.
<androidx.appcompat.widget.AppCompatImageView
android:tint="#{model.nextEnabled ? #color/primary : #color/silver}"
app:srcCompat="#drawable/ic_right_blue_24dp"
/>
Here ic_right_blue_24dp is a vector drawable.
After checking binding class, I could see that code for <21 version is not getting generated.
if(getBuildSdkInt() >= 21) {
this.mboundView1.setImageTintList(androidx.databinding.adapters.Converters.convertColorToColorStateList(modelBackEnabledMboundView1AndroidColorPrimaryMboundView1AndroidColorSilver));
}
I have tried all things I could think, and could find.
AppCompatImageView
ImageView
app:srcCompat
android:src
app:tint
vectorDrawables.useSupportLibrary = true
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
Here I must tell you that all things work using regular tint without binding.
there still is custom data-binding. even exactly the method, as requested:
#BindingMethods({
#BindingMethod(
type = "androidx.appcompat.widget.AppCompatImageView",
attribute = "android:tint",
method = "setImageTintList"
)
})
This BindingAdapter will set tint programmatically...and it worked for me
#BindingAdapter("android:tint")
fun AppCompatImageView.setImageTint(#ColorInt color: Int) {
setColorFilter(color)
}
Usage
<androidx.appcompat.widget.AppCompatImageView
android:tint="#color/primary"
...
/>

Disabling Android O auto-fill service for an application

Android O has the feature to support Auto-filling for fields. Is there any way I can disable it for a specific application. That is I want to force my application not to use the auto-fill service.
Is it possible ?
To block autofill for an entire activity, use this in onCreate() of the activity:
getWindow()
.getDecorView()
.setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);
Is there any better method than this ?
Currently there is no direct way to disable the autofill for an entire application, since the autofill feature is View specific.
You can still try this way and call BaseActivity everywhere.
public class BaseActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
disableAutofill();
}
#TargetApi(Build.VERSION_CODES.O)
private void disableAutofill() {
getWindow().getDecorView().setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);
}
}
You can also force request autofill this way.
public void forceAutofill() {
AutofillManager afm = context.getSystemService(AutofillManager.class);
if (afm != null) {
afm.requestAutofill();
}
}
Note: At the moment autofill feature is only available in API 26 Android Oreo 8.0
Hope this helps!
I believe the accepted answer is incorrect:
So I have my own class which is extends the android.support.v7.widget.AppCompatEditText and all I did is overwrote the following method with the following value:
#Override
public int getAutofillType() {
return AUTOFILL_TYPE_NONE;
}
no other solutions worked, not even android:importantForAutofill="no".
getAutofillType() comes from the View class, so it should work for every other class such as TextInputEditText too!
I ran into this too. It turns out the issue was caused by setting the hint text on the EditText nested inside the TextInputLayout.
I did some digging and found this nugget in the 26.0.0 Beta 2 release notes.
Andorid Support Release Notes June 2017
TextInputLayout must set hints on onProvideAutofillStructure()
That led me to try setting the hint on the TextInputLayout instead of the nested EditText.
This resolved the crashing issue for me.
Example:
<android.support.design.widget.TextInputLayout
android:id="#+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Some Hint Text"
android.support.design:hintAnimationEnabled="true"
android.support.design:hintEnabled="true"
android.support.design:layout_marginTop="16dp">
<android.support.design.widget.TextInputEditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>
Seems to be a bug that needs to be fixed : https://issuetracker.google.com/issues/67675432
In the meanwhile a workaround for now is to disable the AutoFill feature for the whole project.
You can add in the values-v26/styles.xml file the following style or you can edit your BaseEditTextStyle if you are using a specific style for your EditText views.
<style name="App_EditTextStyle" parent="#android:style/Widget.EditText">
<item name="android:importantForAutofill">noExcludeDescendants</item>
</style>
and in the values-v26/themes.xml file you can simply add to the default theme that you are using in your app the items editTextStyle and android:editTextStyle like following :
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="android:editTextStyle">#style/App_EditTextStyle</item>
<item name="editTextStyle">#style/App_EditTextStyle</item>
</style>
this way you can apply this changes for all your EditTexts without needing to change your layout files or Activities (and later on you can easily remove it when the bug is fixed).
Is it possible ?
Not that I am aware of. Certainly, nothing is documented.
Is there any better method than this ?
Not that I am aware of.
In your EditText attributes add android:importantForAutofill="no"
This should be a temporary fix and will only apply to api 26+
Create custom EditText style and set android:importantForAutofill to no.
<style name="EditTextStyleWithoutAutoFill" parent="Widget.AppCompat.EditText">
<item name="android:importantForAutofill">no</item>
</style>
Then in your activity theme set this style for editTextStyle.
<item name="android:editTextStyle">#style/EditTextStyleWithoutAutoFill</item>
In my case, our app targets SDK version 21 but newer devices (26+) were still popping up the autocomplete. Pretty big problem if the app runs on devices that are shared between people. Using just android:importantForAutofill="no" did not work for me.
The only solution that I found to work in my case was:
<EditText
android:importantForAutofill="no"
tools:targetApi="o"
android:autofillHints="AUTOFILL_HINT_SMS_OTP" ...
The reason I added android:autofillHints="AUTOFILL_HINT_SMS_OTP" was because if you long-pressed on the EditText it would still bring up autofill. Basically, I told the field's autofill that it is waiting for a text message that will never be sent. Bit of a hack, I know...
Note: you may have to add xmlns:tools="http://schemas.android.com/tools" to your schemas' if it is not there already.
Had the same problem with API 28+ and disable Autofill. For me the only solution was to disable long click for my views.
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:longClickable="false"
android:text="#={model.emailAdress}"/>
With reference to Google issue tracker, it has been fixed.
This is fixed on Android 8.1
If any issue persists, please report at Google issue tracker they will re-open to examine.

VectorDrawable Backwards Compatibility And Installing Unofficial Support Libraries

Bear with me, I'm new!
I want to use vectors in my android app, and I want my app to be backwards compatible. I found this support library that looks pretty cool!*
So I'm confused about how I would I 'install' it. It gives you a link to download the .pom, .aar, javadoc.jar, and the sources.jar file. Which one should I download, and where (what folder) should I put the file?
(I'm using Android Studio!)
*(Anybody know a different VectorDrawable support library? I'd be interested in hearing everybody's experience!)
Here is a option that worked for me
Use this library - https://github.com/wnafee/vector-compat (api 14+)
android {
// use version 22 or higher
buildToolsVersion "22.0.1"
...
}
dependencies {
compile 'com.wnafee:vector-compat:1.0.5'
...
}
And create a custom ImageView class that uses vector compat class -
public class SvgImageView extends ImageView {
private Drawable icon;
public SvgImageView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs,
R.styleable.button_left, 0, 0);
try {
int resId = ta.getResourceId(R.styleable.button_left_b_icon, -1);
if (resId != -1) {
icon = ResourcesCompat.getDrawable(this.getContext(), resId);
}
} finally {
ta.recycle();
}
if (icon != null) {
setImage(icon);
}
}
public void setImage(Drawable icon) {
SvgImageView.this.setImageDrawable(icon);
}
}
Vector image example -
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:width="#dimen/logo_dimen"
android:height="#dimen/logo_dimen"
android:viewportWidth="#integer/view_port_dimen_logo"
android:viewportHeight="#integer/view_port_dimen_logo"
app:vc_viewportWidth="#integer/view_port_dimen_logo"
app:vc_viewportHeight="#integer/view_port_dimen_logo">
<group
android:name="rotationGroup"
android:pivotX="0"
android:pivotY="0"
android:rotation="0">
<path
android:name="v"
android:fillColor="#color/white"
android:pathData="m15.5,15.6c0,-1.5 2.8,-1.9 2.8,-5c0,-1.5 -0.7,-2.6 -1.8,-3.5h1.6l1.7,-1.1h-5c-1.7,0 -3.5,0.4 -4.8,1.6c-1,0.8 -1.6,2.1 -1.6,3.4c0,2.4 1.9,4.1 4.2,4.1c0.3,0 0.5,0 0.8,0c-0.1,0.3 -0.3,0.6 -0.3,1c0,0.7 0.3,1.2 0.8,1.8c-1.6,0.1 -3.4,0.3 -4.9,1.2c-1.1,0.7 -2,1.8 -2,3.2c0,0.6 0.2,1.1 0.4,1.6c1,1.7 3.2,2.2 5,2.2c2.3,0 4.9,-0.7 6.1,-2.8c0.4,-0.6 0.6,-1.3 0.6,-2.1c0.2,-3.5 -3.6,-4 -3.6,-5.6zm-1.7,-1.2c-2.2,0 -3.2,-2.8 -3.2,-4.6c0,-0.7 0.1,-1.4 0.6,-1.9c0.4,-0.6 1.1,-0.9 1.7,-0.9c2.2,0 3.2,3 3.2,4.8c0,0.7 -0.1,1.4 -0.6,1.9c-0.4,0.4 -1.1,0.7 -1.7,0.7zm0,10.5c-1.9,0 -4.5,-0.8 -4.5,-3.2c0,-2.5 2.9,-3.1 4.9,-3.1c0.2,0 0.4,0 0.6,0c1.2,0.8 2.8,1.8 2.8,3.4c-0.1,2.2 -2,2.9 -3.8,2.9zm9.7,-10.5v-2.6h-1.3v2.6h-2.5v1.3h2.5v2.6h1.3v-2.6h2.6v-1.3h-2.6l0,0z"
app:vc_fillColor="#color/white"
app:vc_pathData="m15.5,15.6c0,-1.5 2.8,-1.9 2.8,-5c0,-1.5 -0.7,-2.6 -1.8,-3.5h1.6l1.7,-1.1h-5c-1.7,0 -3.5,0.4 -4.8,1.6c-1,0.8 -1.6,2.1 -1.6,3.4c0,2.4 1.9,4.1 4.2,4.1c0.3,0 0.5,0 0.8,0c-0.1,0.3 -0.3,0.6 -0.3,1c0,0.7 0.3,1.2 0.8,1.8c-1.6,0.1 -3.4,0.3 -4.9,1.2c-1.1,0.7 -2,1.8 -2,3.2c0,0.6 0.2,1.1 0.4,1.6c1,1.7 3.2,2.2 5,2.2c2.3,0 4.9,-0.7 6.1,-2.8c0.4,-0.6 0.6,-1.3 0.6,-2.1c0.2,-3.5 -3.6,-4 -3.6,-5.6zm-1.7,-1.2c-2.2,0 -3.2,-2.8 -3.2,-4.6c0,-0.7 0.1,-1.4 0.6,-1.9c0.4,-0.6 1.1,-0.9 1.7,-0.9c2.2,0 3.2,3 3.2,4.8c0,0.7 -0.1,1.4 -0.6,1.9c-0.4,0.4 -1.1,0.7 -1.7,0.7zm0,10.5c-1.9,0 -4.5,-0.8 -4.5,-3.2c0,-2.5 2.9,-3.1 4.9,-3.1c0.2,0 0.4,0 0.6,0c1.2,0.8 2.8,1.8 2.8,3.4c-0.1,2.2 -2,2.9 -3.8,2.9zm9.7,-10.5v-2.6h-1.3v2.6h-2.5v1.3h2.5v2.6h1.3v-2.6h2.6v-1.3h-2.6l0,0z" />
</group>
</vector>
Example -
<packagename.SvgImageView
app:b_icon="#drawable/google_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView3" />
Google just announced Android Studio 1.4 with backwards compatibility for Vector Drawables. It will generate .png files in the appropriate sizes for the different screen densities for pre-Lollipop devices and will use the vector format for Lollipop and up. See this link: http://android-developers.blogspot.com/2015/09/android-studio-14.html
Just make sure that your Gradle Build version is 1.4.0 or above!
Thanks for the people who ported this lib before Google!
Great news is that google released Android Support Library 23.2 Support Vector Drawables and Animated Vector Drawables !
Note:
- Vector images all the way back to API 7 (Android 2.1 Eclair).
- Animated vectors are a bit more limited, going only as far back as API 11
The best solution I found is the BetterVectorDrawable lib together with the SVG to VectorDrawable Converter.
BetterVectorDrawable is the VectorDrawable implementation for Android 4.0+ with configurable fall-back behavior on Android 5.0+. The lib can be added to a project with just one line (see readme).
SVG to VectorDrawable Converter is the batch converter of SVG images to Android VectorDrawable XML resource files. Online version is here.
Links point to readmes, which provide enough information on how to use the lib and the converter.

svg-android imageview not working

I have a need to show svg files in my android app. svg-android seems like the only library that has any documentation and thus my first approach. The only example available demonstrates how to create an imageview attach an svg image and attach it to the main content view. I however want a svg file to show up on a RelativeLayout I already have defined. I attempted an implementation like so:
ImageView imageView = new ImageView(this);
SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.logo);
imageView.setImageDrawable(svg.createPictureDrawable());
RelativeLayout home_header = (RelativeLayout) findViewById(R.id.home_header);
home_header.addView(imageView);
All appears well (no warnings/errors) but when I test the app log cat reports:
05-27 11:25:43.940: I/Adreno200-EGLSUB(28492): <ConfigWindowMatch:2078>: Format RGBA_8888.
05-27 11:25:43.950: E/(28492): Can't open file for reading
05-27 11:25:43.960: E/(28492): Can't open file for reading
I have verified the following:
- File is not open in any other program
- File is properly formatted
What am I missing here? Any suggestions on what might be going on?
android:hardwareAccelerated="false" will disable hardware rendering for the whole activity. An alternative might be to just use:
imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Which should disable it only for that View.
PS. If you are looking for an SVG library with better documentation (and more features), try mine: http://code.google.com/p/androidsvg/
After some debugging and comparing the emulator to the native app I discovered that the "can't open file for reading" is not related to the svg files not displaying. Instead it was related to hardware acceleration. I had to set the following in my manifest
android:hardwareAccelerated="false"
problem solved. Time wasted. Brain blown.
There is another option now that doesn't require android:hardwareAccelerated="false"
Use this library - https://github.com/wnafee/vector-compat (api 14+)
android {
// use version 22 or higher
buildToolsVersion "22.0.1"
...
}
dependencies {
compile 'com.wnafee:vector-compat:1.0.5'
...
}
And create a custom ImageView class that uses vector compat class -
public class SvgImageView extends ImageView {
private Drawable icon;
public SvgImageView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs,
R.styleable.button_left, 0, 0);
try {
int resId = ta.getResourceId(R.styleable.button_left_b_icon, -1);
if (resId != -1) {
icon = ResourcesCompat.getDrawable(this.getContext(), resId);
}
} finally {
ta.recycle();
}
if (icon != null) {
setImage(icon);
}
}
public void setImage(Drawable icon) {
SvgImageView.this.setImageDrawable(icon);
}
}
Vector image example -
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:width="#dimen/logo_dimen"
android:height="#dimen/logo_dimen"
android:viewportWidth="#integer/view_port_dimen_logo"
android:viewportHeight="#integer/view_port_dimen_logo"
app:vc_viewportWidth="#integer/view_port_dimen_logo"
app:vc_viewportHeight="#integer/view_port_dimen_logo">
<group
android:name="rotationGroup"
android:pivotX="0"
android:pivotY="0"
android:rotation="0">
<path
android:name="v"
android:fillColor="#color/white"
android:pathData="m15.5,15.6c0,-1.5 2.8,-1.9 2.8,-5c0,-1.5 -0.7,-2.6 -1.8,-3.5h1.6l1.7,-1.1h-5c-1.7,0 -3.5,0.4 -4.8,1.6c-1,0.8 -1.6,2.1 -1.6,3.4c0,2.4 1.9,4.1 4.2,4.1c0.3,0 0.5,0 0.8,0c-0.1,0.3 -0.3,0.6 -0.3,1c0,0.7 0.3,1.2 0.8,1.8c-1.6,0.1 -3.4,0.3 -4.9,1.2c-1.1,0.7 -2,1.8 -2,3.2c0,0.6 0.2,1.1 0.4,1.6c1,1.7 3.2,2.2 5,2.2c2.3,0 4.9,-0.7 6.1,-2.8c0.4,-0.6 0.6,-1.3 0.6,-2.1c0.2,-3.5 -3.6,-4 -3.6,-5.6zm-1.7,-1.2c-2.2,0 -3.2,-2.8 -3.2,-4.6c0,-0.7 0.1,-1.4 0.6,-1.9c0.4,-0.6 1.1,-0.9 1.7,-0.9c2.2,0 3.2,3 3.2,4.8c0,0.7 -0.1,1.4 -0.6,1.9c-0.4,0.4 -1.1,0.7 -1.7,0.7zm0,10.5c-1.9,0 -4.5,-0.8 -4.5,-3.2c0,-2.5 2.9,-3.1 4.9,-3.1c0.2,0 0.4,0 0.6,0c1.2,0.8 2.8,1.8 2.8,3.4c-0.1,2.2 -2,2.9 -3.8,2.9zm9.7,-10.5v-2.6h-1.3v2.6h-2.5v1.3h2.5v2.6h1.3v-2.6h2.6v-1.3h-2.6l0,0z"
app:vc_fillColor="#color/white"
app:vc_pathData="m15.5,15.6c0,-1.5 2.8,-1.9 2.8,-5c0,-1.5 -0.7,-2.6 -1.8,-3.5h1.6l1.7,-1.1h-5c-1.7,0 -3.5,0.4 -4.8,1.6c-1,0.8 -1.6,2.1 -1.6,3.4c0,2.4 1.9,4.1 4.2,4.1c0.3,0 0.5,0 0.8,0c-0.1,0.3 -0.3,0.6 -0.3,1c0,0.7 0.3,1.2 0.8,1.8c-1.6,0.1 -3.4,0.3 -4.9,1.2c-1.1,0.7 -2,1.8 -2,3.2c0,0.6 0.2,1.1 0.4,1.6c1,1.7 3.2,2.2 5,2.2c2.3,0 4.9,-0.7 6.1,-2.8c0.4,-0.6 0.6,-1.3 0.6,-2.1c0.2,-3.5 -3.6,-4 -3.6,-5.6zm-1.7,-1.2c-2.2,0 -3.2,-2.8 -3.2,-4.6c0,-0.7 0.1,-1.4 0.6,-1.9c0.4,-0.6 1.1,-0.9 1.7,-0.9c2.2,0 3.2,3 3.2,4.8c0,0.7 -0.1,1.4 -0.6,1.9c-0.4,0.4 -1.1,0.7 -1.7,0.7zm0,10.5c-1.9,0 -4.5,-0.8 -4.5,-3.2c0,-2.5 2.9,-3.1 4.9,-3.1c0.2,0 0.4,0 0.6,0c1.2,0.8 2.8,1.8 2.8,3.4c-0.1,2.2 -2,2.9 -3.8,2.9zm9.7,-10.5v-2.6h-1.3v2.6h-2.5v1.3h2.5v2.6h1.3v-2.6h2.6v-1.3h-2.6l0,0z" />
</group>
</vector>
Example -
<packagename.SvgImageView
app:b_icon="#drawable/google_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView3" />
Using androidx.appcompat.widget.AppCompatImageView instead of ImageView worked for me.
AppCompatImageView
from package android.support.v7.widget
Check this post, I have given all the details to use svg. As per my experience, you can use svg in Android flawlessly.
Pros:
No third party library (official android support library needed) No changes in gradle file
Use `android:src` for all `ImageViews` instead of 'app:srcCompat` for svg and `android:src` for other images.
No need to use AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); in static block of BaseActivity.

Backwards compatible Switch

ICS has a Switch component. It does what we need. Is there anything out there backward compatible to (about) 2.2? Couldn't find anything obvious.
Looks like somebody built this:
https://github.com/Prototik/KFramework-SW.git
Android support AppCompat library from version 21.0.0 contains android.support.v7.widget.SwitchCompat to provide compability back to API v7. https://developer.android.com/reference/android/support/v7/widget/SwitchCompat.html
Include it like this with gradle:
compile 'com.android.support:appcompat-v7:21.0.0'
It can be used in layouts like this:
<android.support.v7.widget.SwitchCompat />
In addition it has showText attribute to make styling easier - which seems to be missing from native andriod Switch.
Switch is only on 4.0+
If you want to make an app that uses switch on 4.0+ devices what you need to do is declare two layouts. The first in layout-v14 which will be what's used on ICS devices. In your layout folder make use of CheckBox.
In your code make use of the CompoundButton class when getting/setting data from the switch or checkbox. You'll find that CompoundButton works well for this.
you should use checkbox when such a thing is impossible , as described here:
https://docs.google.com/a/android.co.il/presentation/d/1mKmwM-HNXukKT_FgAMmyCuwMdL4nQI4aZ6SXIr5wixc/pub?start=false&loop=false&delayms=3000#slide=id.g119cf79b_0_8
(slide 32)
This library is what you're looking for : https://github.com/BoD/android-switch-backport
Here is an example of SwitchCompat
First thing to do make sure you add this lines to your build.gradle and then sync.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
}
Second create a sample Activity , in my case I will call it SwitchActivity.java.
public class SwitchActivity extends ActionBarActivity {
SwitchCompat mySwitch = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_switch);
// here is your switch
mySwitch = (SwitchCompat)findViewById(R.id.myswitch);
}
.....
}
Lastly create your Layout , in my case I will call it activity_switch.xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.neoecosystem.samplex.SwitchActivity">
<android.support.v7.widget.SwitchCompat
android:id="#+id/myswitch"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</RelativeLayout>
If you are using the holoeverywhere library, you can use something like this in your layout file
<org.holoeverywhere.widget.Switch
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

Categories

Resources