Popsta
Popsta

Reputation: 99

How to check whether the navigation drawer is open?

I have an activity with navigation drawer. Below is my code.

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if(dLayout.isDrawerOpen(GravityCompat.START)==true) {
                 dLayout.closeDrawers();
                }

            else
            {
            doExit();
            }
        }

        return super.onKeyDown(keyCode, event);
    }

    private void doExit() {

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                FirstSelection.this);

        alertDialog.setPositiveButton("Yes", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });

According to this code, when the user presses back button, the navigation drawer is closed if it is opened, else the doExit(); method is called. But in my case, the else condition works fine, but when I press back button when the drawer is open, the complete app closes. I followed this code How to detect if navigation drawer is open?

Any help would be appreciated.

Upvotes: 7

Views: 6699

Answers (2)

Code on the Rocks
Code on the Rocks

Reputation: 17576

Here are the general steps for doing this in Kotlin.

  1. Initialize the DrawerLayout View

    val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
    
  2. Check if the drawer is open

    if(drawerLayout.isDrawerOpen(GravityCompat.START)){
        Log.d("Drawer","open")
    }
    

If you want to take actions automatically when the drawer is opened or closed, you can do the following.

  1. In your main activity, create an inner class that is a subclass of DrawerLayout.DrawerListener. The DrawerLayout class implements the DrawerListener interface.

    inner class CustomDrawer : DrawerLayout.DrawerListener{
      override fun onDrawerStateChanged(newState: Int) {
      }
    
      override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
        imm.hideSoftInputFromWindow(drawerView?.getWindowToken(), 0)
      }
    
      override fun onDrawerClosed(drawerView: View) {
        imm.hideSoftInputFromWindow(drawerView?.getWindowToken(), 0)
      }
    
      override fun onDrawerOpened(drawerView: View) {
        imm.hideSoftInputFromWindow(drawerView?.getWindowToken(), 0)
      }
    }
    
  2. Put your action in the function you want to use. In my example, I'm closing the soft keyboard if the user interacts with the navigation drawer. Declare the InputMethodManager like this in your main activity:

    var imm: InputMethodManager = this.getSystemService(Activity.INPUT_METHOD_SERVICE)
    
  3. Add your custom DrawerListener to the drawerLayout (I put it in the onCreate method)

    var drawerListener = CustomDrawer()
    drawerLayout.addDrawerListener(drawerListener)
    

Upvotes: 1

amodkanthe
amodkanthe

Reputation: 4530

DrawerLayout has method isDrawerOpen(listView) returns true or false this will help you I guess

if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
            mDrawerLayout.closeDrawer(mDrawerList);
        } 

Add above code inside onBackPressed

Upvotes: 9

Related Questions