mirror of https://github.com/M66B/FairEmail.git
parent
5579fb8e3f
commit
fb4d4d51f7
@ -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
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in new issue