Toolbar Navigation Icon Never Set

Toolbar navigation icon never set

Currently you can use it, changing the order: (it seems to be a bug)

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);

toolbar.setNavigationIcon(R.drawable.ic_good);
toolbar.setTitle("Title");
toolbar.setSubtitle("Sub");
toolbar.setLogo(R.drawable.ic_launcher);

Toolbar custom icon reset after switching between navigation drawer items

It is an expected result.

When using NavigationUI, the top app bar helpers automatically transition between the drawer icon and the Up icon as the current destination changes

You can add the OnDestinationChangedListener to set your own icons after your setup method.

navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
if (destination.getId() == R.id.nav_xxx){
getSupportActionBar().setHomeAsUpIndicator(R.drawable....);
}
}
});

Can't change toolbar back icon while using navhost

This feature should be added in the future, currently you can do it by implementing 'OnDestinationChangedListener', and changing the toolbar icon inside the callback (just be sure to add the 'OnDestinationChangedListener' after your setup method), something like this:

navController.addOnDestinationChangedListener { controller, destination, arguments ->
toolbar.setNavigationIcon(R.drawable.ic_keyboard_arrow_left_black_24dp) //set it here for all the destinations, or inside the switch statement if you want to change it based on destination
when(destination.id) {
R.id.mainFragment -> {
toolbar.setNavigationIcon(R.drawable.ic_keyboard_arrow_left_black_24dp)
}
R.id.detailsFragment -> {
toolbar.setNavigationIcon(R.drawable.ic_keyboard_arrow_left_black_24dp)
}
}
}

Based on this issue:

https://issuetracker.google.com/issues/121078028

Android toolbar setNavigationIcon not working

I think you can set like this

    menuDrawerToggle = new ActionBarDrawerToggle(this, menuDrawer, toolbar, R.string.drawer_open, R.string.drawer_close){...}

menuDrawerToggle.syncState();

toolbar.setNavigationIcon(getResources().getDrawable(yourDrawable));

put setNavigationIcon after syncState()

How to set navigationIcon color white in toolbar?

To change the Overflow Menu Icon color via xml define a custom style in styles.xml file and change the android:tint property like below:

<style name="MyToolBarTheme" parent="ThemeOverlay.AppCompat.ActionBar">
<item name="android:tint">@color/white</item>
</style>

and you can use this style in Toolbar using the android:theme attribute like below:

<androidx.appcompat.widget.Toolbar
android:theme="@style/MyToolBarTheme"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">

or in case you want to change it programmatically you can do it like below:

if(toolbar.getOverflowIcon()!=null)
toolbar.getOverflowIcon().setTint(Color.WHITE);

Call navigation icon click in toolbar (android)

Please try this by replacing 'number' with the index of the icon. (It could be 1-2-3-4 etc.)

    toolbar.getChildAt(number).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//do whatever you want here
}
});


Related Topics



Leave a reply



Submit