'Click listener for drawableleft in EditText in Android?

Is it possible to add a OnClickListener to on a drawable that is declared in designer as drawableleft

android:drawableLeft="@drawable/ic_password"

Or is there any way to add a ImageView on the left side of that EditText



Solution 1:[1]

For drawableleft click listeners:

 editText.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        final int DRAWABLE_LEFT = 0;
        final int DRAWABLE_TOP = 1;
        final int DRAWABLE_RIGHT = 2;
        final int DRAWABLE_BOTTOM = 3;

       if(event.getRawX() <= (editText.getCompoundDrawables()[DRAWABLE_LEFT].getBounds().width()))
        {
              // your action here
             return true;
        }
        return false;
    }
});

In case if you want click listener for drawable right use following condition

if(event.getAction() == MotionEvent.ACTION_UP) {
            if(event.getRawX() >= (editComment.getRight() - editComment.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) 

Solution 2:[2]

You can achieve it by a RelativeLayout:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp">

    <ImageView
        android:id="@+id/image_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerInParent="true"
        android:src="@drawable/ic_iamge"/>

    <EditText
        android:id="@+id/text_id"
        android:layout_toRightOf="@id/image_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="16dp"
        android:text="Your text" />

 </RelativeLayout>

And add onClickListener to the ImageView in your code.

Solution 3:[3]

There is more elegant way:
Don't use android:drawableStart or android:drawableLeft in the TextInputEditText.

Instead you can use:

<com.google.android.material.textfield.TextInputLayout
    ...
    app:endIconMode="custom"
    app:startIconDrawable="@drawable/..."

and then use the endIconOnClickListener:

textInputLayout.setStartIconOnClickListener {
  // Respond to end icon presses
}

Source: https://stackoverflow.com/a/65940540/13545849

Solution 4:[4]

You need to implement the OnTouchListener and add your click action. I used the below helper class. onDrawableClick returns: true if the listener has consumed the event, false otherwise

public abstract class DrawableClickListener implements View.OnTouchListener {
public static final int DRAWABLE_LEFT = 0;
public static final int DRAWABLE_TOP = 1;
public static final int DRAWABLE_RIGHT = 2;
public static final int DRAWABLE_BOTTOM = 3;

private final int drawableIndex;

public DrawableClickListener(int index) {
    drawableIndex = index;
}

@Override
public boolean onTouch(View view, MotionEvent event) {
    TextView textView = (TextView) view;
    if (event.getAction() == MotionEvent.ACTION_UP) {
        if (event.getRawX() >= (textView.getRight() - textView.getCompoundDrawables()[drawableIndex].getBounds().width())) {
            return onDrawableClick();
        }
    }
    return false;
}

public abstract boolean onDrawableClick();

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2
Solution 3 FreePhoenix
Solution 4 MohamedHarmoush