2

I want to change the selected item whole icon not the tint color in bottom navigation view.I have the selector for each icon but i don't know where to add that image selector. Please any one tell me the solution.

Code:

private void handleBottomNavigationItemSelected(MenuItem menuItem) {
    menuItem.setChecked(true);
    switch (menuItem.getItemId()) {
        //Replacing the main content with ContentFragment Which is our Inbox View;
        case R.id.action_calendar:
            switchFragment(new PatientAppointmentStatusFragment(), "TODAY");
            break;
        case R.id.action_case_sheets:
            switchFragment(new CaseSheetFragment(), "Case Sheet");
            break;
        case R.id.action_history:
            switchFragment(new HistoryFragment(), "History");
            break;
        case R.id.action_reports:
            switchFragment(new ReportsFragment(), "Reports");
            break;
        case R.id.action_billing:
            switchFragment(new BillingFragment(), "Billing");
            break;

    }
}

Menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_calendar"
        android:enabled="true"
        android:icon="@drawable/menu_calendar_bg"
        android:title="@string/menu_calendar"
        app:showAsAction="always|withText" />
    <item
        android:id="@+id/action_case_sheets"
        android:enabled="true"
        android:icon="@drawable/menu_case_sheets_bg"
        android:title="@string/menu_case_sheets"
        app:showAsAction="always|withText" />
    <item
        android:id="@+id/action_history"
        android:enabled="true"
        android:icon="@drawable/menu_history_bg"
        android:title="@string/menu_history"
        app:showAsAction="always|withText" />

    <item
        android:id="@+id/action_reports"
        android:enabled="true"
        android:icon="@drawable/menu_reports_bg"
        android:title="@string/menu_reports"
        app:showAsAction="always|withText" />

    <item
        android:id="@+id/action_billing"
        android:enabled="true"
        android:icon="@drawable/menu_billing_bg"
        android:title="@string/menu_billing"
        app:showAsAction="always|withText" />

</menu>

Here is the code for bottom navigation view xml.

<BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="@dimen/margin_48dp"
        android:layout_alignParentBottom="true"
        app:itemBackground="@drawable/tab_background"
        app:itemIconTint="@color/navigation_item_color"
        app:itemTextColor="@color/navigation_item_color"
        app:menu="@menu/bottom_navigation_main" />

I tried the following link but it's not working for me.

Link Android: Bottom Navigation View - change icon of selected item

Shanmugapriyan M
  • 301
  • 5
  • 17

1 Answers1

1

You should create a folder color in res folder .Into res/color folder create a file with "navigation_item_color" name and use following code:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--state is enabled and checked-->
    <item android:color="@color/white"  android:state_enabled="true" android:state_checked="true" />
    <!--state is enabled and not checked-->
    <item android:color="@color/colorPrimaryDark" android:state_enabled="true" android:state_checked="false" />
    <!--state (menu item) is disabled -->
    <!--   <item android:state_enabled="false" android:color="@color/light_black"  />-->
</selector>
PRIYA PARASHAR
  • 777
  • 4
  • 15