Search code examples
androiddrawerlayout

Drawer icon not responding to click


I have a DrawerToggle, to show a DrawerLayout. I am doing it this way:

mDrawerLayout = findViewById(R.id.drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,0, 0) {
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            invalidateOptionsMenu();
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            invalidateOptionsMenu();
        }
    };
    mDrawerToggle.setDrawerIndicatorEnabled(false);
    Drawable drawable= ResourcesCompat.getDrawable(getResources(),R.drawable.menu,getTheme());
    mDrawerToggle.setHomeAsUpIndicator(drawable);
    mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("David", "onClick");
            if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {
                mDrawerLayout.closeDrawer(GravityCompat.START);
            } else {
                mDrawerLayout.openDrawer(GravityCompat.START);
            }
        }
    });
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

But I am not getting response when icon is clicked. How can I fix it?


Solution

  • You have not added code to open the drawer. Add below code in your Activity Class:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                drawerLayout.openDrawer(GravityCompat.START);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    

    Follow: https://developer.android.com/training/implementing-navigation/nav-drawer