How to Create onClickListener in Drawable EditText

21 May 2022

Home » Blogs » Android » How to Create onClickListener in Drawable EditText

You can create the onclicklistener() function in various ways by adding a click functionality to the EditText icon.

For example, if you click on the “add” icon, it will add value to the one displayed by the EditText . And if you click on the “remove” icon, it will reduce its value.

drawable click


In the layout, create an EditText by adding icons to the ” drawableStart ” and ” drawableEnd “.

The complete code for activity_main.xml is as follows:



In MainActivity add “ OnTouchListener ” as follows

public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        EditText myText = findViewById(;
        myText.setOnTouchListener((view, motionEvent) -> {
            final int DRAWABLE_LEFT = 0;
            final int DRAWABLE_TOP = 1;
            final int DRAWABLE_RIGHT = 2;
            final int DRAWABLE_BOTTOM = 3;
            if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                if (motionEvent.getRawX() >= (myText.getRight() - myText.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                    Toast.makeText(MainActivity.this, "You clicked on Add Button", Toast.LENGTH_LONG).show();
                    return true;
                if (motionEvent.getRawX() >= myText.getCompoundDrawables()[DRAWABLE_LEFT].getBounds().width()) {
                    Toast.makeText(MainActivity.this, "You clicked on Remove Button", Toast.LENGTH_LONG).show();
                    return true;
            return false;

Related Articles