Unable to use drawable in EditText inside TextInputLayout

て烟熏妆下的殇ゞ 提交于 2019-11-28 14:24:34

I had the exact same issue, All I did was add drawableStart along with drawableLeft and it's shown as expected.

Based on the answer by Ahmed Ashraf G, I was able to find the solution. Changing the following code:

setCompoundDrawables(getCompoundDrawables()[0],
        getCompoundDrawables()[1], icon, getCompoundDrawables()[3]);

to:

setCompoundDrawablesRelative(getCompoundDrawablesRelative()[0],
            getCompoundDrawablesRelative()[1], icon, getCompoundDrawablesRelative()[3]);

From other places on StackOverflow (the documentation does NOT mention this) the difference between xxxCompoundDrawablesXxx and xxxCompundDrawablesRelativeXxx is that the relative versions take into account RTL-languages, ie they are the same as using drawableStart instead of drawableLeft.

So in conclusion, Google has broken all methods that are not RTL-methods for EditText inside TextInputLayout. Since the new methods are added in API level 17 I see this as major bug, that will probably be fixed in future updates

try replacing EditText by android.support.design.widget.TextInputEditText, for example:

 <android.support.design.widget.TextInputLayout
     android:id="@+id/email_input"
     android:layout_width="match_parent"
     android:layout_height="wrap_content">

 <android.support.design.widget.TextInputEditText
          android:id="@+id/email"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:hint="@string/email_hint"
          android:drawableStart="@drawable/ic_clear" />
 </android.support.design.widget.TextInputLayout>

This is the easiest way I could find:

Step 1.

On your layout, set your endIconDrawable, It's important to know that it will not show if you don't set the endIconMode.

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/custom_end_icon"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/hint_text"
    app:endIconMode="custom"
    app:endIconDrawable="@drawable/custom_icon"
    app:endIconContentDescription="@string/custom_content_desc">

  <com.google.android.material.textfield.TextInputEditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

</com.google.android.material.textfield.TextInputLayout>

Step 2. Depending on what you want to do with the button, these are the three methods you can call on it

TextInputLayout textInputCustomEndIcon = view.findViewById(R.id.custom_end_icon);

// If the icon should work as button, set an OnClickListener to it.
textInputCustomEndIcon
    .setEndIconOnClickListener(/* custom OnClickListener */);

// If any specific changes should be done when the EditText is attached (and
// thus when the end icon is added to it), set an OnEditTextAttachedListener
textInputCustomEndIcon
    .addOnEditTextAttachedListener(/* custom OnEditTextAttachedListener */);

// If any specific changes should be done if/when the endIconMode gets changed,
// set an OnEndIconChangedListener
textInputCustomEndIcon
    .addOnEndIconChangedListener(/* custom OnEndIconChangedListener */);

For more on Customization, and additional features check this Link cheers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!