diff --git a/app/src/main/java/eu/faircode/email/TextViewAutoCompleteClearable.java b/app/src/main/java/eu/faircode/email/TextViewAutoCompleteClearable.java new file mode 100644 index 0000000000..533fb57dac --- /dev/null +++ b/app/src/main/java/eu/faircode/email/TextViewAutoCompleteClearable.java @@ -0,0 +1,89 @@ +package eu.faircode.email; + +/* + This file is part of FairEmail. + + FairEmail is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + FairEmail is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with FairEmail. If not, see . + + Copyright 2018-2021 by Marcel Bokhorst (M66B) +*/ + +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.text.Editable; +import android.text.TextWatcher; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.view.View; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.widget.AppCompatAutoCompleteTextView; + +public class TextViewAutoCompleteClearable extends AppCompatAutoCompleteTextView { + private Drawable drawable = null; + + public TextViewAutoCompleteClearable(@NonNull Context context) { + super(context); + init(); + } + + public TextViewAutoCompleteClearable(@NonNull Context context, @Nullable AttributeSet attrs) { + super(context, attrs); + init(); + } + + public TextViewAutoCompleteClearable(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + init(); + } + + public void init() { + drawable = getContext().getDrawable(R.drawable.twotone_close_24); + drawable.setTint(getCurrentTextColor()); + drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); + + setOnTouchListener(new OnTouchListener() { + @Override + public boolean onTouch(View v, MotionEvent event) { + if (getCompoundDrawables()[2] == null) + return false; + if (event.getAction() != MotionEvent.ACTION_UP) + return false; + if (event.getX() > getWidth() - getPaddingRight() - drawable.getIntrinsicWidth()) { + setText(""); + setCompoundDrawables(null, null, null, null); + } + return false; + } + }); + + addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + // Do nothing + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + setCompoundDrawablesRelative(null, null, s.length() > 0 ? drawable : null, null); + } + + @Override + public void afterTextChanged(Editable s) { + // Do nothing + } + }); + } +} diff --git a/app/src/main/res/layout/dialog_search.xml b/app/src/main/res/layout/dialog_search.xml index 51c71e74f9..3517272206 100644 --- a/app/src/main/res/layout/dialog_search.xml +++ b/app/src/main/res/layout/dialog_search.xml @@ -89,7 +89,7 @@ app:layout_constraintTop_toBottomOf="@id/tvCaption" app:srcCompat="@drawable/twotone_info_24" /> -