Question

In my app there is one EditText. I have called keypad show on my activity for this EditText. keypad showing & working fine.

Now in currently, I have to press back button two time, one for hiding keypad & another for performing some task(like data saving to DB).I don't want to press back button two times.

please guide me, or suggestion

here some from onBackPressed()

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();

    hideKeypad();

    //saving EditText data to db.
}

code form hideKeypad()

private void hideKeypad() {
       InputMethodManager imm = (InputMethodManager) 
        getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(edittext1.getWindowToken(), 0);
}
Was it helpful?

Solution

Use this method it is similar what you already done but I little modified it . Register listener of your main layout and pass its object as param so that when you click out side of edit text i,e. main layout keypad will gone.

/** Close Keypad on touch.
 * @param view on which click happens. */

 public void closeKeyPad(View view)
    {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);    
    }

OTHER TIPS

Pressing back to close the keyboard is a standard function of the UI. Why would you want to change this?

Users who are used to using a soft-keyboard on Android devices will be used to pressing back once to close the keyboard, then once more to move back in the application.

If you make the back button move back in the application while the keyboard is displayed, it makes navigation different to standard Android navigation, and many users may get frustrated with it.

Besides that, your onBackPressed is not being called because when the keyboard is displayed, the onBackPressed for the keyboard is run - which, as we've all seen - is what hides the keyboard - and not the onBackPressed for your application.

Mr.Me's answer is the way to do it if you do want to go that way, but I wouldn't recommend it.

The problem is that the keyboard consumes the first BackPressed event. which you can't do anything about. to solve this just listen to the keyboard hide event and finish your activity.

code to do that:

    edit.setOnEditorActionListener(
     new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
        actionId == EditorInfo.IME_ACTION_DONE ||
        event.getAction() == KeyEvent.ACTION_DOWN &&
        event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
    if (!event.isShiftPressed()) {
       // the user is done typing.  finish the activity
       finish();
       return true; // consume.
    }                
}
return false; // pass on to other listeners. 
}
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top