How to change color of Toolbar back button in Android?
AndroidAndroid Problem Overview
I could not change the color of back button. I am using toolbar material design. In my app I am applying black background of tool bar but the back design is being black by default that's why I just want to change the color of this back button. Please give me solutions.
Thank You
Android Solutions
Solution 1 - Android
You can add a style to your styles.xml,
<style name="ToolbarTheme" parent="@style/ThemeOverlay.AppCompat.ActionBar">
<!-- Customize color of navigation drawer icon and back arrow -->
<item name="colorControlNormal">@color/toolbar_color_control_normal</item>
</style>
and add this as theme to your toolbar in toolbar layout.xml using app:theme, check below
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="@style/ToolbarTheme" >
</android.support.v7.widget.Toolbar>
Solution 2 - Android
use this style
<style name="Theme.MyFancyTheme" parent="android:Theme.Holo">
<item name="android:homeAsUpIndicator">@drawable/back_button_image</item>
</style>
Solution 3 - Android
Here is the simplest way of achieving Light and Dark Theme for Toolbar.You have to change the value of app:theme
of the Toolbar tag
- For Black Toolbar Title and Black Up arrow, your toolbar should implement following theme:
>app:theme="@style/ThemeOverlay.AppCompat.Light"
-
For White Toolbar Title and White Up arrow, your toolbar should implement following theme:
> app:theme="@style/ThemeOverlay.AppCompat"
Solution 4 - Android
Use this:
<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorControlNormal">@color/white</item>
</style>
Solution 5 - Android
For white Toolbar Title and White Up arrow, use following theme:
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
Solution 6 - Android
Try this,
final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(getResources().getColor(R.color.grey), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);
Solution 7 - Android
Just use the MaterialToolbar
and override the default colors:
<com.google.android.material.appbar.MaterialToolbar
style="@style/Widget.MaterialComponents.Toolbar.Primary"
android:theme="@style/MyThemeOverlay_Toolbar"
..>
with:
<style name="MyThemeOverlay_Toolbar" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
<!-- color used by navigation icon and overflow icon -->
<item name="colorOnPrimary">@color/...</item>
</style>
If you are using the androidx.appcompat.widget.Toolbar
or the MaterialToolbar
with the default style (Widget.MaterialComponents.Toolbar
) you can use:
<androidx.appcompat.widget.Toolbar
android:theme="@style/MyThemeOverlay_Toolbar2"
with:
<style name="MyThemeOverlay_Toolbar2" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
<!-- This attributes is used by title -->
<item name="android:textColorPrimary">@color/white</item>
<!-- This attributes is used by navigation icon and overflow icon -->
<item name="colorOnPrimary">@color/secondaryColor</item>
</style>
Solution 8 - Android
You don't have to change style for it. After setting up your toolbar as actionbar, You can code like this
android.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
android.getSupportActionBar().setHomeAsUpIndicator(R.drawable.back);
//here back is your drawable image
But You cannot change color of back arrow by this method
Solution 9 - Android
You can use your own icon by using app:navigationIcon
and for the title color app:titleTextColor
Example:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="@color/colorPrimary"
app:navigationIcon="@drawable/ic_arrow_back_white_24dp"
app:titleTextColor="@color/white" />
Solution 10 - Android
I think if theme should be generated some problem but its dynamically set black arrow.So i Suggested try this one.
Drawable backArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
backArrow.setColorFilter(getResources().getColor(R.color.md_grey_900), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(backArrow);
Solution 11 - Android
If you want white back button (←) and white toolbar title, follow this:
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
Change theme from Dark
to Light
if you want black back button (←) and black toolbar title.
Solution 12 - Android
To style the Toolbar on Android 21+ it's a bit different.
<style name="DarkTheme.v21" parent="DarkTheme.v19">
<!-- toolbar background color -->
<item name="android:navigationBarColor">@color/color_primary_blue_dark</item>
<!-- toolbar back button color -->
<item name="toolbarNavigationButtonStyle">@style/Toolbar.Button.Navigation.Tinted</item>
</style>
<style name="Toolbar.Button.Navigation.Tinted" parent="Widget.AppCompat.Toolbar.Button.Navigation">
<item name="tint">@color/color_white</item>
</style>
Solution 13 - Android
I did it this way using the Material Components library:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="drawerArrowStyle">@style/AppTheme.DrawerArrowToggle</item>
</style>
<style name="AppTheme.DrawerArrowToggle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="color">@android:color/white</item>
</style>
Solution 14 - Android
I am using <style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar>
theme in my android application and in NoActionBar theme, colorControlNormal
property is used to change the color of navigation icon default Back button arrow in my toolbar
styles.xml
<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorControlNormal">@color/your_color</item>
</style>
Solution 15 - Android
You can add this code into your java class. But you must create a vector asset before, so you can customize your arrow back.
actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_back_black_24dp);
Solution 16 - Android
Simple and no Worries
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:titleTextColor="@color/white"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/testing" />
</android.support.design.widget.CoordinatorLayout>
Solution 17 - Android
Programmatically we can achieve using below code. No need to add our own back icon as suggested by many others.
private void initToolBar(String title) {
toolbar = findViewById(R.id.toolbar);
toolbar.setTitle(title);
toolbar.setTitleTextColor(getResources().getColor(R.color.gold));
setSupportActionBar(toolbar);
// add back arrow to toolbar
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
Drawable nav = toolbar.getNavigationIcon();
if(nav != null) {
nav.setTint(getResources().getColor(R.color.gold));
}
}
}
Please note toolbar.getNavigationIcon()
if requested before getSupportActionBar().setDisplayHomeAsUpEnabled(true);
it won't work.
Solution 18 - Android
For white Toolbar Title and White Up arrow, use following theme:
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
Solution 19 - Android
If you want system back color in white then you can use this code
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_notification"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp">
<include layout="@layout/action_bar_notification" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
Note:- android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
is key
The paste is in your activity where you want to show back button on action bar
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_notification);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(false);