简体   繁体   中英

how to change Action bar icon (hamburger icon) when using navigation drawer with navigation component

I want to change the home up indicator but it doesn't work when using navigation component

I tried the solution from here and didn't work how to change toolbar icon (hamburger icon) when using navigation drawer with jetpack navigation component

My Code

    setSupportActionBar(binding.toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.myTeamFragment,
            R.id.myTasksFragment, R.id.meetingsFragment, R.id.freeTimeFragment, R.id.dashboardFilterFragment)
            .setOpenableLayout(binding.getRoot())
            .build();
    NavigationUI.setupWithNavController(binding.toolbar, getNavController(), appBarConfiguration);

then as Solution Mentioned

ActionBar actionBar = getSupportActionBar();
    getNavController().addOnDestinationChangedListener((controller, destination, arguments) -> {
        if (destination.getId() == R.id.dest0
                || destination.getId() == R.id.dest1
                || destination.getId() == R.id.dest2
                || destination.getId() == R.id.dest3
                || destination.getId() == R.id.dest4){

            actionBar.setHomeAsUpIndicator(R.drawable.ic_hambergur_menu);
        } else {
            actionBar.setHomeAsUpIndicator(R.drawable.ic_hambergure_back);
        }
    });

You are using a setup with a Toolbar

NavigationUI.setupWithNavController(binding.toolbar, getNavController(), appBarConfiguration);

Then use the Toolbar API instead of ActionBar API:

    navController.addOnDestinationChangedListener { controller, destination, arguments ->
        if (destination.id == R.id.nav_home){
            toolbar.setNavigationIcon(R.drawable.xxxx)
        }
    }

If you just want to show the back arrow button instead of the burger button, you can remove the fragments that you want to show the back button from appBarConfiguration

In your snippet, the back button won't show up in these fragemnts ( R.id.myTasksFragment, R.id.meetingsFragment, R.id.freeTimeFragment, R.id.dashboardFilterFragment ), remove any of them to show the back button instead.

AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.myTeamFragment,
        R.id.myTasksFragment, R.id.meetingsFragment, R.id.freeTimeFragment, R.id.dashboardFilterFragment)
        .setOpenableLayout(binding.getRoot())
        .build();

I don't know, why? But for me it works with postDelay function. Even if I setup the delay equals zero

 navController.addOnDestinationChangedListener { controller, destination, arguments ->
        if (destination.id == R.id.fragment_orders) {
            view.postDelayed(
                {
                    supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_amount)
                },
                0
            )
        }
    }

Adding a handler with post delay 0 mili worked for me.

  @Override
    public void onDestinationChanged(@NonNull NavController controller,
                                     @NonNull NavDestination destination,
                                     @Nullable Bundle arguments) {
        Log.d(TAG, "onDestinationChanged: current destination: "+navController.getCurrentDestination().getLabel());
        this.currentFragmentId = destination.getId();
        if(currentFragmentId == R.id.addItemFragment){
            new Handler().postDelayed(()->{
                mToolbar.setNavigationIcon(R.drawable.ic_baseline_arrow_back_ios_24);
            },0);
        }
        super.setTitle(destination.getLabel());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM