You can create the onclicklistener() function in various ways by adding a click functionality to the Drawable 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.
Other Interesting Articles
activity_main.xml
In the layout, create an EditText by adding icons to the ” drawableStart ” and ” drawableEnd .
The complete code for activity_main.xml is as follows:
<?xml version="1.0" encoding="utf-8"?> androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableStart="@drawable/ic_baseline_remove_circle_24" android:drawableEnd="@drawable/ic_baseline_add_circle_24" android:ems="10" android:gravity="center_horizontal" android:inputType="textPersonName" android:text="1" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> /androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
In MainActivity add “ OnTouchListener ” as follows
public class MainActivity extends AppCompatActivity { @SuppressLint("ClickableViewAccessibility") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EditText myText = findViewById(R.id.editText1); 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; }); } }