Issue
I have an autocomplete editText field and I found this code for clearing text on the editText field and also placing it on the right corner of the field, my problem is after I clear text once on the editText field the cross(drawable) disappears. I have to leave the page and come back again for it to be visible again. How can I make it visible all the time?Please help. This is the code I have:
String value = "";
personAccountableAutoCompleteTextView.setText(value);
final Drawable x = getResources().getDrawable(R.drawable.clear);
personAccountableAutoCompleteTextView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (personAccountableAutoCompleteTextView.getCompoundDrawables()[2] == null) {
return false;
}
if (event.getX() > personAccountableAutoCompleteTextView.getWidth() - personAccountableAutoCompleteTextView.getPaddingRight() - x.getIntrinsicWidth()) {
personAccountableAutoCompleteTextView.setText("");
x.setVisible(true,true);
personAccountableAutoCompleteTextView.setCompoundDrawables(null, null, null, null);
}
return false;
}
});
Solution
give yout edittext drawable right in xml like this:
<EditText
android:layoutheight="wrap_content"
android:layoutwidth="match_parent"
android:drawableright="@drawable/yourimg"/>
and then give click listner to that drawable like this:
edittext.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_LEFT = 0;
final int DRAWABLE_TOP = 1;
final int DRAWABLE_RIGHT = 2;
final int DRAWABLE_BOTTOM = 3;
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getRawX() >= (edittext.getRight() - edittext.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
// your action here
return true;
}
}
return false;
}
});
Answered By - Satish Silveri
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.