0

I implemented a custom Toolbar and in that custom toolbar, I implemented a custom editText to clear the data in the editText. So for drawable to work I implemented setDrawableClickListener and it works but the problem is when the first time I click on drawable my editText get into text selection mode and drawable click doesn't work but the next time I click then drawable click works and clear the text.

So I need help to in implementing the drawable click work in the first time.

Link to custom editText: Setting onClickListener for the Drawable right of an EditText

enter image description here

So This time when I click the cross then editText blue cursor get visible and when this blue cross disappear after that when I click on the cross then editText gets cleared.

<android.support.v7.widget.Toolbar android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:contentInsetStart="0dp"
        android:elevation="4dp"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/AlertDialog.AppCompat.Light"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <LinearLayout
            android:background="@color/colorPrimary"
            android:padding="2.5dp"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <com.example.mybrowser.customEditText
                android:drawableRight="@drawable/ic_clear_black_24dp"
                android:drawablePadding="5dp"      
                android:inputType="textUri"
                android:hint="Enter URL"
                android:background="@drawable/rectangle"
                android:id="@+id/urlEditText"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent" />
            <Button
                android:layout_margin="2dp"
                android:background="@drawable/ic_send_black_24dp"
                android:layout_width="50dp"
                android:layout_height="50dp"
                />
        </LinearLayout>
    </android.support.v7.widget.Toolbar>
ardiien
  • 767
  • 6
  • 26
Mayank Doda
  • 117
  • 2
  • 11

1 Answers1

0

What you can simply do is , Create a edittext with android:drawableRight = R.drawable.close inside your editText

<EditText
.....
android:drawableRight = R.drawable.close
.....
/>

and then attach these listeners to it

open class SimpleWatcher(val inputLayout: TextInputLayout,
                        val context: Context?,
                        val editText: EditText,val shouldShowCloseButton:Boolean = true): TextWatcher {
override fun afterTextChanged(s: Editable?) {
}

override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

    if(s?.isNotEmpty() == true){
        if(shouldShowCloseButton) {
            context?.let {
   //This is the important one. Sets back the drawable after removing it when the text is empty
                editText.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(it, R.drawable.ic_edit_text_close), null)
            }
        }
        inputLayout.error = null
    }else{
        if(shouldShowCloseButton) {
            editText.setCompoundDrawables(null, null, null, null)
        }
    }
}

}

 class EditTextOnTouchListener(val view: EditText): View.OnTouchListener{
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
    val DRAWABLE_RIGHT = 2
    if(event?.action == MotionEvent.ACTION_UP){
        if(view.compoundDrawables[DRAWABLE_RIGHT] != null) {
            if (event.rawX >= (view.right - 
  view.compoundDrawables[DRAWABLE_RIGHT].bounds.width())) {
  //                        Toast.makeText(context, "Close Clicked", 
  Toast.LENGTH_SHORT).show()
                view.text = Editable.Factory.getInstance().newEditable("")
                view.clearFocus()
                return true
            }
        }
    }
    return false
}

}

Take what is necessary and ignore the rest. mainly the body inside onTextChanged and the EditTextOnTouchListener. Since this works on the touch listener, anytime a touch comes the listener will be called and appropriate action will be taken

Jude Osbert K
  • 940
  • 9
  • 22