Android Toolbar Change Navigation/Back Icon/homeAsUpIndicator (Maintain Click Effect)

Sep 29, 2019
Restore original back arrow icon on toolbar

Change Navigation Icon

Change Toolbar navigation icon

toolbar.setNavigationIcon(R.drawable.ic_close_black_24dp)

Change Icon color

val passiveColor = ContextCompat.getColor(context, R.color.selection_icon_passive)// val closeIcon =  ContextCompat.getDrawable(context, R.drawable.ic_close_black_24dp)val closeIcon =  ContextCompat.getDrawable(context, R.drawable.selector_ic_close)DrawableCompat.setTint(closeIcon!!, passiveColor)toolbar.navigationIcon = closeIcon

res/drawable/selector_ic_close

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_close_black_24dp" android:state_pressed="true" />
    <item android:drawable="@drawable/ic_close_black_24dp" />
</selector>

Restore Original Navigation Icon

Assuming you changed the navigation icon as per the above instruction, how do you restore the original back icon?

Option 1: R.attr.homeAsUpIndicator

toolbar.navigationIcon = context.getDrawableFromAttribute(R.attr.homeAsUpIndicator)

NOTE: Refer Context.getDrawableFromAttribute

Option 2: R.drawable.abc_ic_ab_back_material

toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_material)

NOTE: R.drawable.abc_ic_ab_back_material - resource is marked as private

Option 3: Save original drawable before switching

val originalNavigationIcon = toolbar.navigationIcon

NOTE: Becareful of UI state as sometimes toolbar.navigationIcon will be null

Option 4: Material Icon

Import back icon: Android Studio -> File -> New -> Vector Asset -> Clip Art -> arrow back

Use the method in Change Navigation Icon section.

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.