0

I have used svg files in my Android project. There are issues in Android 4.4 or lower versions. I have tried these solutions

  1. app:srcCompat,
  2. vectorDrawables.useSupportLibrary = true in gradle file and
  3. AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); in static block of BaseActivity.
Other than gridview, images are not shown in app.
Harish Gyanani
  • 1,366
  • 2
  • 22
  • 43
  • What are your "issues"? Without an idea of what is going wrong, it is going to be hard to help you. – Paul LeBeau Dec 08 '16 at 11:38
  • @PaulLeBeau check last line, Other than gridview, images are not shown in app. – Harish Gyanani Dec 08 '16 at 12:34
  • Don't waste your time with SVG's on Android. I promise you, with whatever SVG's you get from your designer, you *will* be unable to use them directly because there's a *lot* of SVG features that are not supported in Android.Hence, the popularity of paintcodeapp.com – Someone Somewhere Apr 18 '19 at 16:52
  • I just checked www.paintcodeapp.com and under `Our Customers` it shows **GOOGLE** !! That confirms SVG handling in Android is piss-poor! – Someone Somewhere Apr 18 '19 at 16:55

2 Answers2

5

Instead of using above 3 solutions, just replace your ImageView with android.support.v7.widget.AppCompatImageView. No need to do any extra effort. Note:- TextView, EditText and other classes, which use drawableRight and drawableLeft are not supported. For them, create your own compound view class with TextView or EditText and AppCompatImageView in a FrameLayout or RelativeLayout. Example of drawableRight inside EditText:-

Layout file

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/edt_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:maxLines="1"
        android:paddingEnd="40dp"
        android:paddingLeft="5dp"
        android:paddingRight="40dp"
        android:paddingStart="5dp" />

    <android.support.v7.widget.AppCompatImageView
        android:id="@+id/search"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="right|center_vertical"
        android:layout_margin="8dp"
        android:src="@drawable/ic_svg_search" />
</FrameLayout>

Code

public class EditTextWithDrawable extends FrameLayout {
    public AppCompatImageView mDrawableRight;
    public EditText mAppEditText;
    public AppEditTextWithDrawable(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }
    private void init(AttributeSet attrs) {
        if (attrs != null && !isInEditMode()) {
            LayoutInflater inflater = (LayoutInflater) getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inflater.inflate(R.layout.compound_view, this, true);
            mDrawableRight = (AppCompatImageView) ((FrameLayout) getChildAt(0)).getChildAt(1);
            mAppEditText = (EditText) ((FrameLayout) getChildAt(0)).getChildAt(0);

            TypedArray attributeArray = getContext().obtainStyledAttributes(
                    attrs,
                    R.styleable.EditTextWithDrawable);

            int drawableRes =
                    attributeArray.getResourceId(
                            R.styleable.EditTextWithDrawable_drawableRightSVG, -1);
            if (drawableRes != -1) {
                mDrawableRight.setImageResource(drawableRes);
            }
            attributeArray.recycle();
        }
    }
}

attrs.xml file

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="AppEditTextWithDrawable">
        <attr name="drawableRightSVG" format="reference" />
    </declare-styleable>
</resources>
Harish Gyanani
  • 1,366
  • 2
  • 22
  • 43
2

Convert SVG file into XML and use it from Drawable folder. Check this .

Himank shah
  • 131
  • 13
  • Android Studio have this feature. Right click on `drawable` folder-> choose "New" -> choose "Vector asset" and select svg file. It will be converted into VectorDrawable xml file. – Harish Gyanani Dec 08 '16 at 08:36
  • Purpose was that we can use SVG file in manner also,As XML works fine in all android versions. – Himank shah Dec 08 '16 at 09:19