Search code examples
androidandroid-layoutandroid-edittextandroid-vectordrawable

How to set vector drawable into EditText under api 21?


In my application i want show vector drawable right of EditText.
I added this drawable vector from Vector Asset into android studio!
but when run application show me force close error.
Xml code :

<EditText
        android:id="@+id/signInFrag_phoneEdt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:hint="@string/insertYourPhoneNumber"
        android:maxLines="1"
        android:drawableRight="@drawable/ic_phone_android"
        android:paddingLeft="@dimen/_10sdp"
        android:paddingRight="@dimen/_10sdp"
        android:singleLine="true"
        android:maxLength="11"
        android:inputType="phone"
        android:textColorHint="@color/colorLightGray"
        android:textSize="@dimen/_12sdp"
        android:drawableEnd="@drawable/ic_phone_android"/>

I added vectorDrawables.useSupportLibrary = true into android and defaultConfig on gradle.build file.

error message :

 Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_phone_android.xml from drawable resource ID #0x7f0700b3
    at android.content.res.Resources.loadDrawable(Resources.java:2096)
    at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
    at android.widget.TextView.<init>(TextView.java:814)
    at android.widget.EditText.<init>(EditText.java:60)
    at androidx.appcompat.widget.AppCompatEditText.<init>(AppCompatEditText.java:73)
    at androidx.appcompat.widget.AppCompatEditText.<init>(AppCompatEditText.java:69)
    at androidx.app

Also i added this line into application class : AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)

How can i fix this problem?


Solution

  • Give a try to

    app:drawableEndCompat="@drawable/ic_phone_android"
    

    The support should be included in build.gradle:

    implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'