3

I use following code to show keyboard

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
        InputMethodManager.HIDE_IMPLICIT_ONLY);

I use following code to hide keyboard getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

to hide keyboard, and

also tried for this

InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

but after pressing the home key. apps closes but keyboard remain same on the screen.

please guide me. what am I doing wrong?

I have put my hiding code. I put hide in onDestroy(), onBackPressed() and also onOptionsItemSelected(MenuItem item) Sorry For my code formatting.

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylayout);

    textTv=(EditText)findViewById(R.id.textview1);
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
    InputMethodManager.HIDE_IMPLICIT_ONLY);

    if(getIntent().getExtras()!=null)
    {
        Bundle extra=getIntent().getExtras();
        if(extra!=null)
        {
            // code
        }
    }
}

public void onBackPressed() {
    // TODO Auto-generated method stub
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(noteTv.getWindowToken(), 0);
    }

public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    switch (item.getItemId()) {
    case R.id.menu1:
    //code
    break;
    case R.id.menu2:
    //code
    break;

}

Pankaj Kharche
  • 1,329
  • 4
  • 15
  • 37
  • You have used all the possible cases, every one will work, If it is not worked for you mean there might be some problem in your code, so show us the code where you hide your keyboard. – Pragnani Mar 16 '13 at 09:01
  • I have put showing code in `onCreate()` method and hiding code into `onBackPressed()`works properly . but when I pressed home key with showing keypad its not hided. @Pragnani – Pankaj Kharche Mar 16 '13 at 09:05
  • I have put hide code where I not to hide keyboard. @Pragnani – Pankaj Kharche Mar 16 '13 at 09:13
  • sorry I didn't get you. I have posted answer check that once – Pragnani Mar 16 '13 at 09:14
  • I have tried this but not working.. I have put some code where I am calling hiding code. @Pragnani – Pankaj Kharche Mar 16 '13 at 09:26
  • Sorry for formatting of code. I always try to format code properly. but I don't know how to format it. @Pragnani – Pankaj Kharche Mar 16 '13 at 09:28
  • you can use ctrl+k to format the code, And why did you kept the code to hide keyboard every where..? Post your complete code, because don't need to put the code to hide keyboard everywhere. – Pragnani Mar 16 '13 at 09:35
  • Thanks to tell how to format. ok. then where should I put keypad hiding code. @Pragnani – Pankaj Kharche Mar 16 '13 at 09:37
  • It will helpful for us, if you post your complete code, so that we can identify your problem. – Pragnani Mar 16 '13 at 09:40
  • I have put my all code where I call hiding method. & thanks to tell how to format code.. Please check this. @Pragnani – Pankaj Kharche Mar 16 '13 at 09:46
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26285/discussion-between-pragnani-and-psk) – Pragnani Mar 16 '13 at 09:48

1 Answers1

8

Try

@Override
protected void onPause() {
    final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(noteTv.getWindowToken(), 0);
    super.onPause();
}
Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99