I created this solution for my problem and it worked for me
XML
<androidx.appcompat.widget.AppCompatAutoCompleteTextView
android:id="@+id/origin_adrs"
android:layout_width="match_parent"
android:layout_height="35dp"
android:drawableStart="@drawable/ic_baseline_search_24"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:hint="@string/selectionner_votre_addresse"
android:fontFamily="@font/nunito_semibold"
android:textSize="@dimen/standard_text_14"
android:background="@drawable/backgroud_text_input_adress"
/>
JAVA CODE
Boolean isHere2 = false;
autoCompleteTextView.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@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;
autoCompleteTextView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String adress = autoCompleteTextView.getText().toString();
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(charSequence.length() == 0){
autoCompleteTextView.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(getApplicationContext(),R.drawable.ic_baseline_search_24), null, null, null);
isHere2 = false;
}else{
autoCompleteTextView.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(getApplicationContext(),R.drawable.ic_baseline_search_24), null, ContextCompat.getDrawable(getApplicationContext(),R.drawable.ic_baseline_cancel_24), null);
isHere2 = true;
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
if(isHere2){
if (event.getAction() == MotionEvent.ACTION_UP) {
if (event.getRawX() >= (autoCompleteTextView.getRight() - autoCompleteTextView.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
autoCompleteTextView.setText("");
isHere2 = false;
return true;
}
}
}
return false;
}
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…