3

Android Studio 2.3, Android 4.3, Galaxy Nexus.

Screenshot: enter image description here

I want the selected button (when clicked) to hide soft keyboard.

Questions:

  1. What is the name of this button?
  2. How to handle a click of this button in a fragment?
Michael
  • 2,758
  • 6
  • 28
  • 55
a_subscriber
  • 8,977
  • 15
  • 60
  • 141

2 Answers2

0

You can handle the back button being pressed by using the following method:

// When not using fragments
@Override
public void onBackPressed() {
    // Check if no view has focus:
    View view = this.getCurrentFocus();
    if (view != null) {  
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}

The code inside the onBackPressed method was found here; Close/hide the Android Soft Keyboard

// When using a fragment
fragment.getView().setFocusableInTouchMode(true);
fragment.getView().requestFocus();
fragment.getView().setOnKeyListener( new OnKeyListener()
{
    @Override
    public boolean onKey( View v, int keyCode, KeyEvent event )
    {
        if( keyCode == KeyEvent.KEYCODE_BACK )
        {
            // Check if no view has focus:
            View view = this.getCurrentFocus();
            if (view != null) {  
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        }
        return false;
    }
} );
Community
  • 1
  • 1
Michael
  • 2,758
  • 6
  • 28
  • 55
0

You can use the onKeyPreIme method to handle click of the button.

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) { 
    return true;
}
Shaishav Jogani
  • 1,963
  • 3
  • 20
  • 32
Haris ali
  • 753
  • 1
  • 13
  • 19