Search code examples
androidnavigation-drawermaterial-components-android

Navigation Drawer without custom toolbar


I had implemented Navigation Drawer with fragments which is totally working fine. Now I want to implement Navigation Drawer without using custom Toolbar (with AppTheme== DarkActionbar). I am not able to do such without having custom toolbar. I had already go through many blogs and tutorials but can't get desire solution.

If there is anyone can help me for solving my problem than it will be very helpful to me.

Note: I am not using Navigation Controller for This, I am using Old method which is to add Drawer and DrawerActiobarToogle etc.


Solution

  • If you just want to put away with ActionBar, like @GabrieleMariotti suggests, you can use any of *.NoActionBar themes.

    Or, if you have already known about theme and want to use NavigationUI without Toolbar, there is a way.

    In your MainActivity's onCreate:

    NavigationView navigationView = findViewById(R.id.nav_view);
    navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupWithNavController(navigationView, navController);
    

    You can setup the navigationView like above. That's all. And don't setup appBarConfiguration like below:

    drawer = findViewById(R.id.drawer_layout);
    appBarConfiguration = new AppBarConfiguration.Builder(
        R.id.fragment1, R.id.fragment2, R.id.fragment3
    ).setOpenableLayout(drawer).build();
    NavigationUI.setupWithNavController(toolbar, navController, appBarConfiguration);
    

    For you don't want toolbar, this is needless.

    Without hamburger button, you can still open the drawer by swiping. If you want to have a hamburger button, place a button on the layout which is set onClickListener to open the drawer.

    FloatingActionButton fabNavigation = view.findViewById(R.id.fab_navigation);
    fabNavigation.setOnClickListener(view1 -> onClick());
    

    Drawer opener method example:

    public void onClick(View view) {
        drawer.openDrawer(GravityCompat.START);
    }
    

    screenshot