Implementing Dynamic Icon Switching for Selected Items in Android BottomNavigationView

Dec 06, 2025 · Programming · 8 views · 7.8

Keywords: Android | BottomNavigationView | Icon Switching

Abstract: This paper comprehensively explores multiple technical approaches for implementing dynamic icon switching of selected items in Android BottomNavigationView. By analyzing two core methodologies—XML selectors and programmatic dynamic setting—it provides detailed explanations on avoiding icon tint interference, properly managing menu item states, and offers complete code examples with best practice recommendations. Special emphasis is placed on the importance of precise icon updates within the onNavigationItemSelected callback to ensure smooth user interaction and consistent interface states.

Introduction

In Android application development, BottomNavigationView serves as a crucial component of Material Design, providing users with intuitive bottom navigation experiences. However, developers frequently encounter the requirement to not only switch content areas when users click navigation items but also to change the corresponding icons for clearer visual feedback. This paper systematically explores multiple technical approaches to implement this functionality.

Core Implementation Mechanism

The key to implementing icon switching for selected items in BottomNavigationView lies in understanding Android's menu system state management. Each MenuItem maintains its own state properties, including checked and enabled states. When users interact with BottomNavigationView, the system triggers the OnNavigationItemSelectedListener callback, which represents the optimal timing for executing icon updates.

Programmatic Dynamic Setting Approach

Based on the best answer (Answer 4), we can implement precise icon switching control through programmatic means within the onNavigationItemSelected callback. This approach offers maximum flexibility, allowing developers to dynamically determine which icon resources to use based on application logic.

First, initialize BottomNavigationView and set up the listener in the Activity or Fragment:

BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(
    new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            // Reset all icons to default state
            resetAllIcons();
            
            // Set highlighted icon for the selected item
            switch (item.getItemId()) {
                case R.id.action_favorites:
                    item.setIcon(R.drawable.ic_favorite_selected);
                    break;
                case R.id.action_schedules:
                    item.setIcon(R.drawable.ic_schedule_selected);
                    break;
                case R.id.action_music:
                    item.setIcon(R.drawable.ic_music_selected);
                    break;
            }
            
            // Execute corresponding navigation logic
            performNavigation(item.getItemId());
            return true;
        }
    });

The implementation of the resetAllIcons() method is as follows:

private void resetAllIcons() {
    Menu menu = bottomNavigationView.getMenu();
    menu.findItem(R.id.action_favorites).setIcon(R.drawable.ic_favorite_default);
    menu.findItem(R.id.action_schedules).setIcon(R.drawable.ic_schedule_default);
    menu.findItem(R.id.action_music).setIcon(R.drawable.ic_music_default);
}

XML Selector Method

As a supplementary approach, XML selectors provide a declarative method for icon state management. As described in Answer 1 and Answer 2, selector drawable resource files can be created:

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

Then reference it in the menu resource:

<item
    android:id="@+id/action_favorites"
    android:icon="@drawable/favorite_selector"
    android:title="@string/text_favorites" />

Key Considerations

Regardless of the chosen method, attention must be paid to icon tinting issues. As noted in Answer 2 and Answer 3, BottomNavigationView applies tint to icons by default, which may interfere with the display of custom icon colors. Calling bottomNavigationView.setItemIconTintList(null) disables this behavior, ensuring icons display according to their original design.

Furthermore, state management consistency is crucial. The programmatic approach requires correctly updating all relevant states with each selection change, while the XML selector method relies on the system's automatic state management. Developers should select the most appropriate solution based on their application's specific requirements.

Performance and Best Practices

For performance-sensitive applications, it is recommended to appropriately optimize icon resources and avoid excessively large image files. Additionally, consider using vector icons (VectorDrawable) for better scalability and memory efficiency. When implementing dynamic icon switching, ensure UI updates execute on the main thread to prevent interface flickering or state inconsistencies caused by asynchronous operations.

Conclusion

Implementing icon switching functionality for selected items in BottomNavigationView can be achieved either through programmatic dynamic setting within the onNavigationItemSelected callback or via declarative configuration using XML selectors. Both methods have distinct advantages: the programmatic approach offers greater flexibility and precise control, suitable for complex state logic; while the XML selector method is more concise, appropriate for simple state switching scenarios. Developers should choose the appropriate method based on specific requirements, while carefully addressing icon tinting and state consistency issues to deliver smooth user experiences.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.