Last active
November 30, 2018 16:42
-
-
Save mderazon/6700044 to your computer and use it in GitHub Desktop.
a sub class of AutoCompleteTextView that has a dismiss button with OnClearListener when the clear button is clicked
This file contains 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
import android.content.Context; | |
import android.graphics.drawable.Drawable; | |
import android.util.AttributeSet; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.widget.AutoCompleteTextView; | |
/** | |
* sub class of {@link android.widget.AutoCompleteTextView} that includes a clear (dismiss / close) button with | |
* a OnClearListener to handle the event of clicking the button | |
* based on code from {@link http://www.gubed.net/clearableautocompletetextview} | |
* @author Michael Derazon | |
* | |
*/ | |
public class ClearableAutoCompleteTextView extends AutoCompleteTextView { | |
// was the text just cleared? | |
boolean justCleared = false; | |
// if not set otherwise, the default clear listener clears the text in the | |
// text view | |
private OnClearListener defaultClearListener = new OnClearListener() { | |
@Override | |
public void onClear() { | |
ClearableAutoCompleteTextView et = ClearableAutoCompleteTextView.this; | |
et.setText(""); | |
} | |
}; | |
private OnClearListener onClearListener = defaultClearListener; | |
// The image we defined for the clear button | |
public Drawable imgClearButton = getResources().getDrawable( | |
R.drawable.abc_ic_clear_holo_light); | |
public interface OnClearListener { | |
void onClear(); | |
} | |
/* Required methods, not used in this implementation */ | |
public ClearableAutoCompleteTextView(Context context) { | |
super(context); | |
init(); | |
} | |
/* Required methods, not used in this implementation */ | |
public ClearableAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(); | |
} | |
/* Required methods, not used in this implementation */ | |
public ClearableAutoCompleteTextView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
void init() { | |
// Set the bounds of the button | |
this.setCompoundDrawablesWithIntrinsicBounds(null, null, | |
imgClearButton, null); | |
// if the clear button is pressed, fire up the handler. Otherwise do nothing | |
this.setOnTouchListener(new OnTouchListener() { | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
ClearableAutoCompleteTextView et = ClearableAutoCompleteTextView.this; | |
if (et.getCompoundDrawables()[2] == null) | |
return false; | |
if (event.getAction() != MotionEvent.ACTION_UP) | |
return false; | |
if (event.getX() > et.getWidth() - et.getPaddingRight() - imgClearButton.getIntrinsicWidth()) { | |
onClearListener.onClear(); | |
justCleared = true; | |
} | |
return false; | |
} | |
}); | |
} | |
public void setImgClearButton(Drawable imgClearButton) { | |
this.imgClearButton = imgClearButton; | |
} | |
public void setOnClearListener(final OnClearListener clearListener) { | |
this.onClearListener = clearListener; | |
} | |
public void hideClearButton() { | |
this.setCompoundDrawables(null, null, null, null); | |
} | |
public void showClearButton() { | |
this.setCompoundDrawablesWithIntrinsicBounds(null, null, imgClearButton, null); | |
} | |
} |
I resolved it watching my adapter I am was overriding my style android:dropDownSelector
@OverRide
public View getView
I did a simpler version
checkout http://lison.cc/440.html
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
android:dropDownSelector not working
In my XML:
<com.test.ClearableAutoCompleteTextView
android:id="@+id/search_box"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginTop="0dp"
android:drawablePadding="0dp"
android:dropDownVerticalOffset="5dp"
android:dropDownWidth="wrap_content"
android:dropDownSelector="@color/my_custom_selector"
</>
in my_custom_selector.xml
I tried modifiying styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> @color/my_custom_selector </style>but not working I need your help!