This repository was archived by the owner on Jun 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathEditTextWithClear.java
More file actions
169 lines (153 loc) · 6.6 KB
/
EditTextWithClear.java
File metadata and controls
169 lines (153 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (C) 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.customedittext;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.res.ResourcesCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**
* Custom view that is an extension of EditText.
* It provides a Clear ("x") button within the text field
* that, when tapped, clears the text from the field.
*/
public class EditTextWithClear
extends android.support.v7.widget.AppCompatEditText {
Drawable mClearButtonImage;
public EditTextWithClear(Context context) {
super(context);
init();
}
public EditTextWithClear(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public EditTextWithClear(Context context,
AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
// Initialize Drawable member variable.
mClearButtonImage =
ResourcesCompat.getDrawable(getResources(),
R.drawable.ic_clear_opaque_24dp, null);
// If the X (clear) button is tapped, clear the text.
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// Use the getCompoundDrawables()[2] expression to check
// if the drawable is on the "end" of text [2].
if ((getCompoundDrawablesRelative()[2] != null)) {
float clearButtonStart; // Used for LTR languages
float clearButtonEnd; // Used for RTL languages
boolean isClearButtonClicked = false;
// Detect the touch in RTL or LTR layout direction.
if (getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
// If RTL, get the end of the button on the left side.
clearButtonEnd = mClearButtonImage
.getIntrinsicWidth() + getPaddingStart();
// If the touch occurred before the end of the button,
// set isClearButtonClicked to true.
if (event.getX() < clearButtonEnd) {
isClearButtonClicked = true;
}
} else {
// Layout is LTR.
// Get the start of the button on the right side.
clearButtonStart = (getWidth() - getPaddingEnd()
- mClearButtonImage.getIntrinsicWidth());
// If the touch occurred after the start of the button,
// set isClearButtonClicked to true.
if (event.getX() > clearButtonStart) {
isClearButtonClicked = true;
}
}
// Check for actions if the button is tapped.
if (isClearButtonClicked) {
// Check for ACTION_DOWN (always occurs before ACTION_UP).
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Switch to the black version of clear button.
mClearButtonImage =
ResourcesCompat.getDrawable(getResources(),
R.drawable.ic_clear_black_24dp, null);
showClearButton();
}
// Check for ACTION_UP.
if (event.getAction() == MotionEvent.ACTION_UP) {
// Switch to the opaque version of clear button.
mClearButtonImage =
ResourcesCompat.getDrawable(getResources(),
R.drawable.ic_clear_opaque_24dp, null);
// Clear the text and hide the clear button.
getText().clear();
hideClearButton();
return true;
}
} else {
return false;
}
}
return false;
}
});
// If the text changes, show or hide the X (clear) button.
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) {
if (count > 0)
showClearButton();
else
hideClearButton();
}
@Override
public void afterTextChanged(Editable s) {
// Do nothing.
}
});
}
/**
* Shows the clear (X) button.
*/
private void showClearButton() {
// Sets the Drawables (if any) to appear to the left of,
// above, to the right of, and below the text.
setCompoundDrawablesRelativeWithIntrinsicBounds
(null, // Start of text.
null, // Top of text.
mClearButtonImage, // End of text.
null); // Below text.
}
/**
* Hides the clear button.
*/
private void hideClearButton() {
setCompoundDrawablesRelativeWithIntrinsicBounds
(null, // Start of text.
null, // Top of text.
null, // End of text.
null); // Below text.
}
}