Search code examples
androidnavigationandroid-toolbar

Change back icon color in toolbar


I'm using the Android component navigation for a DrawerLayout with NavigationView.

public class MainActivity extends AppCompatActivity {

    private DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        LoginActivity.toClose.finish();
        LoginViewModel viewModel = ViewModelProviders.of(this).get(LoginViewModel.class);
        Toolbar toolbar = findViewById(R.id.toolbar_menu);
        setSupportActionBar(toolbar);
        ActionBar actionbar = getSupportActionBar();
        assert actionbar != null;

        drawerLayout = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view_menu);
        navigationView.inflateMenu(viewModel.setUserInterface());
        NavController navController = Navigation.findNavController(this, R.id.fragment_main);
        NavigationUI.setupWithNavController(navigationView, navController);
        NavigationUI.setupActionBarWithNavController(this,navController,drawerLayout);
     }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                drawerLayout.openDrawer(GravityCompat.START);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Everything's ok, except that the "Up button" are in a different color that my title in The action bar.

The XML for the Toolbar is:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_menu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:textColor="#FFF"
    android:textColorPrimary="#FFF"
    android:theme="@style/Toolbar"
    app:layout_constraintTop_toTopOf="parent"
    app:titleTextColor="#FFF"
    app:title="@string/app_name"/>

The title is white, but the icon is black.

My question is: how can I change the color of this icon?

Even if I change the primary color to white and the theme editor show me the icon in white, when the app is running, the color is still black.

The app that I'm building has minSdkVersion 15 and I run it in a phone with API 7 SDK 24. I didn't run in the emulator with SDK 15 yet.


Solution

  • use this style

    <style name="ToolbarTheme" parent="@style/ThemeOverlay.AppCompat.ActionBar">
            <!-- Customize color of Toolbar -->
            <item name="colorControlNormal">@color/WhiteColor</item>
        </style>
    

    and then use it in app:theme="@style/ToolbarTheme" in your Toolbar XML :

     <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_menu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#000000"
            android:textColor="#FFF"
            android:textColorPrimary="#FFF"
            app:theme="@style/ToolbarTheme"
            app:layout_constraintTop_toTopOf="parent"
            app:titleTextColor="#FFF"
            app:title="@string/app_name"/>