How to remove drawableleft

AndroidUser Interface

Android Problem Overview


I have a button and a text-view, text-view has a drawable-left. After click on button the drawable-left should be removed and a plain text should be set to Text-view, but I don’t know how to remove drawable-left from code.

Thanks in advance.

Android Solutions


Solution 1 - Android

The drawableLeft (or any of the similar attributes) XML attribute can be modified (removing a drawable in your case) via code using something like this:

yourTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
yourTextView.setText("The Text You Need In There");

The constructor for the method is in this order:

setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)

Read more about the method setCompoundDrawablesWithIntrinsicBounds here

Solution 2 - Android

We can sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text.

Use 0 or null if you do not want a Drawable there.

 textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);

The Drawables' bounds will be set to their intrinsic bounds.

Calling this method will overwrite any Drawables previously set using {@link #setCompoundDrawablesRelative} or related methods.

And if we wants to set Drawables then we can use :

textView.setCompoundDrawablesWithIntrinsicBounds( R.drawable.smiley, 0, 0, 0);

Solution 3 - Android

The drawables of a TextView can be set programatically via the setCompoundDrawables method.

So you could try this:

textView.setCompoundDrawables(null, null, null, null);

Or

textView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);

Solution 4 - Android

Add a Kotlin Extension

If you are going to be doing this frequently, adding an extension makes your code more readable

fun TextView.clearDrawables() {
    this.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0)
}

To use the extension, simply call

view.clearDrawables()

Solution 5 - Android

How to do this and apply to any of the four drawables with DataBinding

@BindingAdapter(value = [
"leftDrawable",
"topDrawable",
"rightDrawable",
"bottomDrawable"], requireAll = false)
fun setCompoundDrawables(textView: TextView,
                     @DrawableRes left: Int?,
                     @DrawableRes top: Int?,
                     @DrawableRes right: Int?,
                     @DrawableRes bottom: Int?) {

textView.setCompoundDrawablesWithIntrinsicBounds(
    left ?: 0,
    top ?: 0,
    right ?: 0,
    bottom ?: 0
)}

Solution 6 - Android

Set All Position is Zero Your Drawable item is remove

Its Working Using This setOnFocusChangeListener method

et_feedback.setCompoundDrawablesWithIntrinsicBounds(0,0, 0, 0);

et_feedback.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                //TODO using this Drawable icon remomve  onclick
                et_feedback.setCompoundDrawablesWithIntrinsicBounds(0,0, 0, 0);
                et_feedback.setTextColor(Color.BLACK);

            }
        });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1965504View Question on Stackoverflow
Solution 1 - AndroidSiddharth LeleView Answer on Stackoverflow
Solution 2 - AndroidA-Droid TechView Answer on Stackoverflow
Solution 3 - AndroidsdabetView Answer on Stackoverflow
Solution 4 - AndroidGiboltView Answer on Stackoverflow
Solution 5 - AndroidJuan MendezView Answer on Stackoverflow
Solution 6 - AndroidKeshav GeraView Answer on Stackoverflow