Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Friday, July 29, 2016

How do I close the keyboard when the navigation drawer opens?

How do I close the keyboard when the navigation drawer opens?


I have added a navigation drawer to my application. So far everything else works well but I am having an issue where when the navigation drawer opens the keyboard is not closed. The navigation drawer is the main activity and then each page opened from the drawer is a fragment.

I have tried adding the following to each one of my EditText areas in the fragments. However, this is not closing anything.

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

I have also tried placing that code in the main activity but have been unsuccessful there as well. Any ideas on what I can do differently to get this working?

Answer by dumazy for How do I close the keyboard when the navigation drawer opens?


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

Answer by Manolescu Sebastian for How do I close the keyboard when the navigation drawer opens?


Try this code, I use it in my apps and for example if I open a dialog that contains a EditText I set this code in on create.Hope this helps

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

Answer by Nikolai Hristov for How do I close the keyboard when the navigation drawer opens?


This worked very well for me:

private ActionBarDrawerToggle aDrawerToggle;  private DrawerLayout aDrawerLayout;  

I'm using this code in a 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();          }  }  

Answer by Ramz for How do I close the keyboard when the navigation drawer opens?


To hide an open keyboard while opening or closing the navigation drawer please override method onDrawerSlide in onDrawerListner and add below line

InputMethodManager inputMethodManager = (InputMethodManager) actionBarActivity      .getSystemService(Activity.INPUT_METHOD_SERVICE);  inputMethodManager.hideSoftInputFromWindow(      actionBarActivity.getCurrentFocus().getWindowToken(),      0  );  

Answer by silwalprabin for How do I close the keyboard when the navigation drawer opens?


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 slider open } else { if (view instanceof 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;        }  

Answer by Alex Bravo for How do I close the keyboard when the navigation drawer opens?


This is the entire code I added to achieve 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);              }          });  

Answer by llBuckshotll for How do I close the keyboard when the navigation drawer opens?


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);              }          });  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.