Explain Codes LogoExplain Codes Logo

Navigation drawer item icon not showing original colour

android
navigation-drawer
icon-colours
state-lists
Nikita BarsukovbyNikita Barsukov·Nov 21, 2024
TLDR

No more grey! Keep the original icon colours in your navigation drawer by setting the icon tint to null. Implement this in the XML or directly in your Java code:

XML:

app:itemIconTint="@null"

Java:

navigationView.setItemIconTintList(null);

With this change, your app's icons retain their original splendour, dodging the Android system's typical grey influencer look.

Taking control: Custom icon colours and states

Unleashing the power of colour state lists

To orchestrate specific colours for different item states, deploy a colour state list. You can do this by generating an XML file in the res/color/ directory. Here's a small demo:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:color="#FFCC00"/> <!-- When checked, go bling bling gold --> <item android:color="#FFFFFF"/> <!-- Otherwise, play the innocent white --> </selector>

You can apply this colour state list to your drawer items either through code execution or via XML styling.

Dynamic icon colour changes: Meet setColorFilter

For swift and saucy icon tinting during runtime, get acquainted with setColorFilter:

drawerItem.getIcon().mutate().setColorFilter(ContextCompat.getColor(context, R.color.your_awesome_color), PorterDuff.Mode.SRC_ATOP);

Remember, mutating the icon is a must-do when you alter drawable properties - it's like making sure your changes don't wake up the sleeping clone army of the drawable.

Don't lose your style: Aesthetics matter

A cohesive and visually appealing navigation drawer works wonders for your app's charm. Maintaining icon's original hues is like dressing them in your brand's uniform - it creates a consistent, aesthetically pleasing user experience.

XML Icon uniformity: Matching the suit and tie

Plan for all of your icons to align with a uniform hue for that sleek, minimalistic look? Achieve this bliss in your navigation drawer menu XML file:

<item android:id="@+id/nav_camera" android:icon="@drawable/ic_camera" app:itemIconTint="@android:color/white" android:title="@string/camera" />

This snazzy tweak sets all icons to a dashing white when they're chilling (unselected). When coupled with a colour state list, you can differentiate between them based on their 'busy' or 'idle' states.