1

I have one screen in which there is an alert box.

In this alert box there is one spinner control and two text fields.

Problem is when I select any text field it opens keyboard but when I click on outside textfield, I need to hide keyboard, I have used method given below to hide keyboard but it doesn't hide keyboard whether it is working without alert box.

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    View view = getCurrentFocus();
    boolean ret = super.dispatchTouchEvent(event);

    if (view instanceof EditText) {
        View w = getCurrentFocus();
        int scrcoords[] = new int[2];
        w.getLocationOnScreen(scrcoords);
        float x = event.getRawX() + w.getLeft() - scrcoords[0];
        float y = event.getRawY() + w.getTop() - scrcoords[1];

        if (event.getAction() == MotionEvent.ACTION_UP
                && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w
                        .getBottom())) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getWindow().getCurrentFocus()
                    .getWindowToken(), 0);
        }
    }
    return ret;
}

Can anybody please tell me why it is creating issue with alert box only.

Please help me.

Jayeshkumar Sojitra
  • 2,501
  • 4
  • 31
  • 44
  • http://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard?rq=1 This should solve your issue. – Manu Aug 07 '13 at 18:54

1 Answers1

0

try this code :

public void showSoftKeyboard() {
    try {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.toggleSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.SHOW_FORCED, 0);
    } catch (Exception e) {
        e.printStackTrace();
    }
 }

public void hideSoftKeyboard() {
    try {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    } catch (Exception e) {
        e.printStackTrace();
    }

}
user3093253
  • 29
  • 1
  • 3