9

I have added Bottom Navigation View to my activity XMl. I am unable to set the default checked item from the menu items. It always sets the first menu item as default. Also when I tap on other menu items the selected menu item is not hightlighted. If I programmatically set the item.setChecked(true) then the menu option gets highlighted but the first menu item is also highlighted. I am using

 compile 'com.android.support:design:25.0.1'

Here is the bottom Navigation view

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:itemBackground="@color/white"
    app:itemTextColor="@color/black"
    app:menu="@menu/navigation_menu">
</android.support.design.widget.BottomNavigationView>

Here is the 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_wallet"
    android:enabled="true"
    android:icon="@drawable/ic_account_balance_wallet_black_24dp"
    android:title="wallet"
    app:showAsAction="ifRoom"
    />
<item
    android:id="@+id/action_card"
    android:enabled="true"
    android:title="allowance"
    android:icon="@drawable/ic_credit_card_black_24dp"
    app:showAsAction="ifRoom"
    />

<item
    android:id="@+id/action_transaction"
    android:enabled="true"
    android:icon="@drawable/ic_description_black_24dp"
    android:title="transaction"
    app:showAsAction="ifRoom" />

</menu>

Bottom Navigation View Has anyone encountered this situation? Any Suggestions on how to handle this?

Thanks, Priya

user2622786
  • 779
  • 3
  • 9
  • 21
  • 3
    Did you tried to add android:checkable="true" in each menu item? Did you return true in OnNavigationItemSelectedListener? – Oleh Nov 18 '16 at 09:09
  • returning true on OnNavigationItemSelectedListener solved the problem. Thanks :) – user2622786 Nov 18 '16 at 20:18

2 Answers2

15

Add a selector.xml in drawable folder for example my xml is

navbar_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true"
         android:color="highlight color" />
   <item android:color="normal color"  />
</selector>

Then add following in lines BottomNavigationView

app:itemIconTint="@drawable/selector"
app:itemTextColor="@drawable/selector"
Abhishek c
  • 539
  • 6
  • 16
  • 3
    Nice clean solution. As a side note, I also had to set the checked status in the #onNavigationItemSelected with menuItem.setChecked(true) for this to work. – Eurospoofer Aug 14 '19 at 08:34
  • 3
    BottomNavigationView not suppoert app:itemIconTint="@drawable/selector" means. @drawable not found – Bhavesh Vadalia Sep 06 '19 at 14:05
11

I was not returning true from OnNavigationItemSelectedListener thats why I was facing the issue. After returning true from OnNavigationItemSelectedListener the issue got resolved. Thanks user2650128 for pointing in that direction.

user2622786
  • 779
  • 3
  • 9
  • 21
  • 3
    Weird, returning true did not fix it for me. In fact, item.setChecked(true) makes the item always selected, even when I tab another tab. – Xi Wei Feb 10 '17 at 20:49