Added search clear text

This commit is contained in:
M66B 2021-01-23 09:20:46 +01:00
parent 5579fb8e3f
commit fb4d4d51f7
2 changed files with 90 additions and 1 deletions

View File

@ -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 <http://www.gnu.org/licenses/>.
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
}
});
}
}

View File

@ -89,7 +89,7 @@
app:layout_constraintTop_toBottomOf="@id/tvCaption"
app:srcCompat="@drawable/twotone_info_24" />
<AutoCompleteTextView
<eu.faircode.email.TextViewAutoCompleteClearable
android:id="@+id/etQuery"
android:layout_width="0dp"
android:layout_height="wrap_content"