12

In my app, I use vector drawables added in support library 23.2 for display vector icons and it works perfectly but when I set the vector to drawableLeft of EditText it does not work in pre-lollipop android versions. At runtime, ResourceNotFound exception occurs.

Caused by: android.content.res.Resources$NotFoundException: File 
res/drawable/layer_ic_user.xml from drawable resource ID #0x7f0200b3

This is my gradle configuration:

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
    applicationId "com.example.test"
    minSdkVersion 14
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    vectorDrawables.useSupportLibrary = true
    generatedDensities = []
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),
       'proguard-rules.pro'
    }
}
sourceSets { main { assets.srcDirs = ['src/main/assets', 'src/main/res/assets/'] } }
  }

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
compile 'com.android.support:design:23.3.0'
}
apply plugin: 'com.google.gms.google-services'

EditText :

     <EditText
        android:id="@+id/et_username_or_email"
        android:layout_width="@dimen/edit_text_width"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/layer_list_ic_user"
        android:textColorHint="@color/ColorBlackPrimary"
        android:inputType="textEmailAddress|text"
        android:textColor="@color/ColorBlackPrimary"
        android:textSize="@dimen/text_small"
        />
nachiketkulk
  • 1,141
  • 11
  • 22
Edalat Feizi
  • 1,371
  • 20
  • 32

6 Answers6

6

You can add Vector Drawable in EditText programmatically. Make use of VectorDrawableCompat inorder to add drawableLeft/ drawableRight /drawableTop/ drawableBottom/ drawableStart/ drawableEnd.

Steps:

i. Remove android:drawableLeft="@drawable/layer_list_ic_user"

ii. If EditText is inside Activity:

EditText etUserName= (EditText)findViewById(R.id.et_username_or_email);
VectorDrawableCompat drawableCompat=VectorDrawableCompat.create(getResources(), R.drawable.layer_list_ic_user, etUserName.getContext().getTheme());
etUserName.setCompoundDrawablesRelativeWithIntrinsicBounds(drawableCompat, null, null, null);

iii. If EditText is inside Fragment:

EditText etUserName= (EditText)view.findViewById(R.id.et_username_or_email);
VectorDrawableCompat drawableCompat=VectorDrawableCompat.create(getActivity().getResources(), R.drawable.layer_list_ic_user, etUserName.getContext().getTheme());
etUserName.setCompoundDrawablesRelativeWithIntrinsicBounds(drawableCompat, null, null, null);

For more information on VectorDrawableCompat Refer this link

Arshak
  • 3,197
  • 1
  • 25
  • 33
  • 1
    Tried all the other solutions here, this is a simple and clean solution that actually works. – BarLr May 08 '19 at 22:35
3

Update

Since Android Support Library, revision 23.4.0

Added AppCompatDelegate.setCompatVectorFromResourcesEnabled() method to re-enable usage of vector drawables in DrawableContainer objects on devices running Android 4.4 (API level 19) and lower. See AppCompat v23.2 — Age of the vectors for more information.

You should add static { AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); } to the top of your Activity.


You are using AppCompat 23.3. From Android Developers

For AppCompat users, 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.

John
  • 1,304
  • 1
  • 9
  • 17
  • 1
    app:srcCompat is work fine with image view i want to use vector for drawableLeft of EditText – Edalat Feizi Apr 16 '16 at 07:19
  • 1
    You can do it programmatically: VectorDrawableCompat.create() – John Apr 16 '16 at 07:26
  • 1
    In addition to `setCompatVectorFromResourcesEnabled`, I had to wrap the vector drawable in a selector, just to use it with `drawableRight`. As mentioned in [Age of the vectors](https://medium.com/@chrisbanes/appcompat-v23-2-age-of-the-vectors-91cbafa87c88#.x4dvq5fe4): *The only rule is that the vector needs to be in a separate file*. – arekolek Mar 16 '17 at 13:41
2

I face this problem and solve it by putting the vector image inside layer-list drawable like that: search_grey.xml

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_search_grey" /> 
</layer-list>

and in EditText:

        android:drawableLeft="@drawable/search_grey"
Amal Kronz
  • 1,657
  • 1
  • 18
  • 22
2

AppCompatTextView now supports app:drawableLeftCompat, app:drawableTopCompat, app:drawableRightCompat, app:drawableBottomCompat, app:drawableStartCompat and app:drawableEndCompat compound drawables, supporting backported drawable types such as VectorDrawableCompat.

Include this in your gradle file

implementation 'androidx.appcompat:appcompat:1.1.0'

In your TextView/ EditText you can use

app:drawableLeftCompat
app:drawableStartCompat
Darish
  • 11,032
  • 5
  • 50
  • 70
1

Vector drawables won't work on pre lollipop devices as drawableLeft/drawableRight/.., Don't set drawableLeft on EditText xml. After initialising EditText set drawableLeft like below.

et_username_or_Email.setCompoundDrawablesWithIntrinsicBounds(R.drawable.layer_list_ic_user, 0, 0, 0);
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Zia
  • 1,204
  • 1
  • 11
  • 25
0

In my case,

chage the extends Activity to extends AppCompatActivity in the Activity class file and the problem solved.

djzhao
  • 934
  • 10
  • 9