I have used svg files in my Android project. There are issues in Android 4.4 or lower versions. I have tried these solutions
app:srcCompat
,-
vectorDrawables.useSupportLibrary = true
in gradle file and -
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
in static block of BaseActivity
.
Other than gridview,
images are not shown in app.
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>
Convert SVG
file into XML
and use it from Drawable
folder. Check this .