EditText drwableLeft does not work with vectors

In my application, I use vector drawings added to support library 23.2 to display vector icons, and it works fine, but when I set the vector to drawableLeft from EditText, it does not work in versions before and after the leopard. At run time, a ResourceNotFound exception is thrown .

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" /> 
+10
android vector
source share
5 answers

Update

Starting with Android Support Library, version 23.4.0

Added the AppCompatDelegate.setCompatVectorFromResourcesEnabled () method to re-enable the use of vector drawings in DrawableContainer objects on devices running Android 4.4 (API level 19) and lower. See AppCompat v23.2 - Age of Vectors for more details.

You must add static { AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); } static { AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); } at the beginning of its activities.


You are using AppCompat 23.3. From Android Developers

For AppCompat users, we decided to remove functionality that allows vector drawings to be used from resources on devices prior to Lollipop, due to problems found in the implementation in version 23.2.0 / 23.2.1. Using the application: srcCompat and setImageResource () continue to work.

+3
source share

You can add Vector Drawable to EditText programmatically. Use VectorDrawableCompat to add drawableLeft / drawableRight / drawableTop / drawableBottom / drawableStart / drawableEnd.

Steps:

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

II. If EditText is inside an 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 the EditText is inside the 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 about VectorDrawableCompat see link

+3
source share

Vector drawables will not work on devices until candy, like drawableLeft / drawableRight / .., Do not set drawableLeft to EditText xml . After initializing the EditText, set drawableLeft as shown below.

 et_username_or_Email.setCompoundDrawablesWithIntrinsicBounds(R.drawable.layer_list_ic_user, 0, 0, 0); 
+1
source share

I am facing this problem and solving it by placing a vector image inside the list of layers as shown: 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" 
0
source share

AppCompatTextView now supports app:drawableLeftCompat , app:drawableTopCompat , app:drawableRightCompat , app:drawableBottomCompat , app:drawableStartCompat and app:drawableEndCompat composite drawn objects that support portable types of drawn objects, such as VectorDrawableCompat .

Include this in your Gradle file

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

In your text view you can use

 app:drawableLeftCompat app:drawableStartCompat 
0
source share

All Articles