How to close the keyboard when opening the navigation box?

I added a navigation box to the application. So far, everything is working well, but I have a problem when the keyboard is not closed when opening the navigation box. The core of navigation is the main activity, and then each page opened from the mailbox is a fragment.

I tried adding the following to each of the EditText areas in the fragments. However, this does not close anything.

InputMethodManager imm1 = (InputMethodManager)getActivity().getSystemService( Context.INPUT_METHOD_SERVICE); imm1.hideSoftInputFromWindow(input1.getWindowToken(), 0); 

I also tried to put this code in the main action, but it also failed there. Any ideas on what I can do differently to get this to work?

+6
source share
8 answers

To hide the open keyboard when opening or closing the navigation box, please override the onDrawerSlide method in onDrawerListner and add the line below

 InputMethodManager inputMethodManager = (InputMethodManager) actionBarActivity .getSystemService(Activity.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow( actionBarActivity.getCurrentFocus().getWindowToken(), 0 ); 
+14
source
 final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) { @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); } @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); } @Override public void onDrawerSlide(View drawerView, float slideOffset) { super.onDrawerSlide(drawerView, slideOffset); InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); } }; drawer.setDrawerListener(actionBarDrawerToggle); actionBarDrawerToggle.syncState(); 
+8
source

This is all the code that I added to achieve the desired result:

  // Hide keyboard when navigation drawer is open drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener(){ @Override public void onDrawerSlide(View drawerView, float slideOffset) {} @Override public void onDrawerOpened(View drawerView) {} @Override public void onDrawerClosed(View drawerView) {} @Override public void onDrawerStateChanged(int newState) { //DeviceUtils.hideVirtualKeyboard(LaunchActivity.this, drawerLayout); final InputMethodManager imm = (InputMethodManager)LaunchActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(drawerLayout.getWindowToken(), 0); } }); 
+6
source

This worked very well for me:

 private ActionBarDrawerToggle aDrawerToggle; private DrawerLayout aDrawerLayout; 

I use this code in the void after creating the class:

  aDrawerToggle = new ActionBarDrawerToggle(getActivity(), aDrawerLayout, R.drawable.main_icon, R.string.drawer_open, R.string.drawer_close){ @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); if (!isAdded()) { return; } InputMethodManager inputMethodManager = (InputMethodManager)getActivity().getSystemService( Context.INPUT_METHOD_SERVICE); //inputSearch is my EditText inputMethodManager.hideSoftInputFromWindow(inputSearch.getWindowToken(), 0); getActivity().supportInvalidateOptionsMenu(); } } 
+4
source

Add a DrawerListener to your DrawerLayout. Then you can use the code above to close the keyboard in the onDrawerOpened () method

+3
source

In onCreate:

 DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() { @Override public void onDrawerSlide(View drawerView, float slideOffset) { } @Override public void onDrawerOpened(View drawerView) { } @Override public void onDrawerClosed(View drawerView) { } @Override public void onDrawerStateChanged(int newState) { InputMethodManager imm = (InputMethodManager)getSystemService(Context. INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); } }); 
+1
source

Try this code, I use it in my applications and, for example, if I open a dialog containing EditText I set this code to create.Hope, it helps

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

0
source
 This does not solve issue. Correct solution is: Override below method on your main activity: public boolean dispatchTouchEvent(MotionEvent ev) { View view = getCurrentFocus(); boolean ret = super.dispatchTouchEvent(ev); if(drawer_open) { 

// do nothing if the slider is open} more {if (view an instance of EditText) {View w = getCurrentFocus (); int scrcoords [] = new int [2]; w.getLocationOnScreen (scrcoords); float x = ev.getRawX () + w.getLeft () - scrcoords [0]; float y = ev.getRawY () + w.getTop () - scrcoords [1];

  if (ev.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; } 
0
source

Source: https://habr.com/ru/post/952458/


All Articles