Created
September 9, 2015 04:51
-
-
Save chantellosejo/8c6608e719e7b03064b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bigoven.android.widgets; | |
import android.content.Context; | |
import android.graphics.drawable.Drawable; | |
import android.support.v7.widget.AppCompatAutoCompleteTextView; | |
import android.text.Editable; | |
import android.text.TextUtils; | |
import android.text.TextWatcher; | |
import android.util.AttributeSet; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import com.bigoven.android.R; | |
/** | |
* Copyright 2015 Alex Yanchenko, with modifications by Chantell | |
* <p/> | |
* 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 | |
* <p/> | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* <p/> | |
* 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. | |
*/ | |
public class ClearableAutoCompleteTextView extends AppCompatAutoCompleteTextView implements View.OnTouchListener, | |
View.OnFocusChangeListener, TextWatcher { | |
/** | |
* To change the clear icon, set | |
* <p/> | |
* <pre> | |
* android:drawableRight="@drawable/custom_icon" | |
* </pre> in the XML | |
*/ | |
public interface OnClearListener { | |
void onTextCleared(); | |
} | |
public void setOnClearListener(OnClearListener onClearListener) { | |
this.onClearListener = onClearListener; | |
} | |
private Drawable mCloseDrawable; | |
private OnClearListener onClearListener; | |
public ClearableAutoCompleteTextView(Context context) { | |
super(context); | |
init(); | |
} | |
public ClearableAutoCompleteTextView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
public ClearableAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(); | |
} | |
@Override | |
public void setOnTouchListener(OnTouchListener l) { | |
this.l = l; | |
} | |
@Override | |
public void setOnFocusChangeListener(OnFocusChangeListener f) { | |
this.f = f; | |
} | |
private OnTouchListener l; | |
private OnFocusChangeListener f; | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
if (getCompoundDrawables()[2] != null) { | |
boolean tappedX = event.getX() > (getWidth() - getPaddingRight() - mCloseDrawable | |
.getIntrinsicWidth()); | |
if (tappedX) { | |
if (event.getAction() == MotionEvent.ACTION_UP) { | |
setText(""); | |
if (onClearListener != null) { | |
onClearListener.onTextCleared(); | |
} | |
} | |
return true; | |
} | |
} | |
if (l != null) { | |
return l.onTouch(v, event); | |
} | |
return false; | |
} | |
@Override | |
public void onFocusChange(View v, boolean hasFocus) { | |
if (hasFocus) { | |
setClearIconVisible(!TextUtils.isEmpty(getText())); | |
} else { | |
setClearIconVisible(false); | |
} | |
if (f != null) { | |
f.onFocusChange(v, hasFocus); | |
} | |
} | |
@Override | |
public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { | |
super.onTextChanged(text, start, lengthBefore, lengthAfter); | |
setClearIconVisible(!TextUtils.isEmpty(text)); | |
} | |
@Override | |
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { | |
} | |
@Override | |
public void afterTextChanged(Editable editable) { | |
} | |
private void init() { | |
mCloseDrawable = getCompoundDrawables()[2]; | |
if (mCloseDrawable == null) { | |
mCloseDrawable = getResources() | |
.getDrawable(R.drawable.ic_close_black); | |
} | |
mCloseDrawable.setBounds(0, 0, mCloseDrawable.getIntrinsicWidth(), mCloseDrawable.getIntrinsicHeight()); | |
setClearIconVisible(false); | |
super.setOnTouchListener(this); | |
super.setOnFocusChangeListener(this); | |
addTextChangedListener(this); | |
} | |
protected void setClearIconVisible(boolean visible) { | |
boolean wasVisible = (getCompoundDrawables()[2] != null); | |
if (visible != wasVisible) { | |
Drawable x = visible ? mCloseDrawable : null; | |
setCompoundDrawables(getCompoundDrawables()[0], | |
getCompoundDrawables()[1], x, getCompoundDrawables()[3]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment