Is that right to make android.view.InflateException by using Vector drawables in android:background: line in a ImageView
Asked Answered
L

2

0

I am facing android.view.InflateException in my app for some particular devices, in others phone it works fine. But i don't know why. Many others says that this problem occurs for drawable vector image. I am using vector image in android:background= "" line. is that make this InflateException.

<RelativeLayout
    android:id="@+id/nameLayoutMain"
    android:layout_width="wrap_content"
    android:layout_height="120dp"
    android:background="@drawable/shadow_164949">

    <ImageView
        android:layout_width="80dp"
        android:layout_height="400dp"
        android:background="@drawable/draw_phone"
        android:id="@+id/im1"
        android:layout_alignParentTop="true" />

    <ImageView
        android:layout_width="80dp"
        android:layout_height="50dp"
        app:srcCompat="@drawable/ic_phone"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_toLeftOf="@+id/linearLayoutName"
        android:layout_toStartOf="@+id/linearLayoutName" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/linearLayoutName"
        android:gravity="center_vertical"
        android:layout_alignBottom="@+id/im1"
        android:layout_toRightOf="@+id/im1"
        android:layout_toEndOf="@+id/im1">

            <TextView
                android:id="@+id/txtPhoneShow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/my_phone_number"
                android:textSize="18sp"
                android:layout_alignParentTop="true"
                android:layout_alignLeft="@+id/txtPhone"
                android:layout_alignStart="@+id/txtPhone" />


            <TextView
                android:id="@+id/txtPhone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="01612548792"
                android:textColor="@color/toolbar1"
                android:textSize="20sp"
                android:textStyle="bold"
                android:layout_below="@+id/txtPhoneShow"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginLeft="20dp"
                android:layout_marginStart="20dp" />

    </RelativeLayout>

</RelativeLayout>
Leonorleonora answered 9/8, 2017 at 4:59 Comment(6)
Can you show me xml code of drawable vector?Diplex
share error logcat..Rocaille
Can you please check this. It contains my drawable vector image, my XML, my error and my Java code. drive.google.com/open?id=0B7XpFVYwQlMYcHc3Q2lHWWU3cUELeonorleonora
Nazmus, I know it's a while back but do you remember if you solved this?Cochise
@n00dles, yes, i can remember. Don't use SVG or Vector image as a background and always use app:srcCompat="@drawable/------"Leonorleonora
Yeh, I guessed that in the end. Thanks for confirming.Cochise
S
1

Make sure you have added vectorDrawables.useSupportLibrary = true inside defaultConfig in app/build.gradle.

Like

defaultConfig {
        vectorDrawables.useSupportLibrary = true
        ....
 }

EDIT 1 In order to support vector in API less then 21. Also add this in onCreate method of Activity before setContentView.

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
Succussion answered 9/8, 2017 at 5:5 Comment(2)
Yes, I added this line in build.gradleLeonorleonora
@NazmusSaadat Just added one more.Succussion
D
0

Use color code replace of color name from color.xml in your drawable vector xml like below;

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">
<path
    android:fillColor="#4396e6"
    android:pathData="M21,19V5c0,-1.1 -0.9,-2 -2,-2H5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2zM8.5,13.5l2.5,3.01L14.5,12l4.5,6H5l3.5,-4.5z"/>

Diplex answered 9/8, 2017 at 5:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.