]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/AutoCompleteTextViewWithClear.java
move clear button implementation to a new custom class
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / AutoCompleteTextViewWithClear.java
1 /*
2  * Copyright © 2019 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.ui;
19
20 import android.content.Context;
21 import android.graphics.Rect;
22 import android.graphics.drawable.Drawable;
23 import android.util.AttributeSet;
24 import android.view.MotionEvent;
25 import android.view.View;
26
27 import androidx.appcompat.widget.AppCompatAutoCompleteTextView;
28
29 import net.ktnx.mobileledger.R;
30
31 public final class AutoCompleteTextViewWithClear extends AppCompatAutoCompleteTextView {
32     private boolean hadText = false;
33
34     public AutoCompleteTextViewWithClear(Context context) {
35         super(context);
36     }
37     public AutoCompleteTextViewWithClear(Context context, AttributeSet attrs) {
38         super(context, attrs);
39     }
40     public AutoCompleteTextViewWithClear(Context context, AttributeSet attrs, int defStyleAttr) {
41         super(context, attrs, defStyleAttr);
42     }
43     @Override
44     protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
45         if (focused) {
46             if (getText().length() > 0) {
47                 showClearDrawable();
48             }
49         }
50         else {
51             hideClearDrawable();
52         }
53
54         super.onFocusChanged(focused, direction, previouslyFocusedRect);
55     }
56     private void hideClearDrawable() {
57         setCompoundDrawablesRelative(null, null, null, null);
58     }
59     private void showClearDrawable() {
60         setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.drawable.ic_clear_black_24dp, 0);
61     }
62     @Override
63     protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
64         final boolean hasText = text.length() > 0;
65
66         if (hadText && !hasText) hideClearDrawable();
67         if (!hadText && hasText) showClearDrawable();
68
69         hadText = hasText;
70
71         super.onTextChanged(text, start, lengthBefore, lengthAfter);
72     }
73     @Override
74     public boolean onTouchEvent(MotionEvent event) {
75         if (event.getAction() == MotionEvent.ACTION_UP) if (getText().length() > 0) {
76             boolean clearClicked = false;
77             final float x = event.getX();
78             final int vw = getWidth();
79             // start, top, end, bottom (end == 2)
80             Drawable dwb = getCompoundDrawablesRelative()[2];
81             if (dwb != null) {
82                 final int dw = dwb.getBounds().width();
83                 if (getLayoutDirection() == View.LAYOUT_DIRECTION_LTR) {
84                     if ((x > vw - dw)) clearClicked = true;
85                 }
86                 else {
87                     if (x < vw - dw) clearClicked = true;
88                 }
89                 if (clearClicked) {
90                     setText("");
91                     requestFocus();
92                     performClick();
93                     return true;
94                 }
95             }
96         }
97
98         return super.onTouchEvent(event);
99     }
100     @Override
101     public boolean performClick() {
102         return super.performClick();
103     }
104 }