views:

73

answers:

0

Hi,

I have an EditText with a compound drawable at right. I want to hide the soft keyboard when i press the drawable and clear the text. For that, i have the following code:

        filterText.setOnTouchListener(new OnTouchListener() { 
        public boolean onTouch(View v, MotionEvent event) { 
                if (filterText.getCompoundDrawables()[2] == null) { 
                        // cross is not being shown so no need to handle 
                        return false; 
                } 
                if (event.getAction() != MotionEvent.ACTION_DOWN) { 
                        // only respond to the down type 
                        return false; 
                } 
                if (event.getX() > filterText.getMeasuredWidth() - 
                        filterText.getPaddingRight() - d.getIntrinsicWidth()) { 
                        filterText.setText(""); 
                        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                        return false; 
                } 
                else {
                    return true; 
                } 
        } 
    }); 

But it doesn't work because the editText seems to maintain the focus. I've tried to filterText.clearFocus but no way.

Thanks