Reputation: 165
I want to implement background color of selected item like below in the image
but unfortunately everyone are trying to answer this using some third party library.
I want to achieve this with material design component itself
this is my code
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/id_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom|start"
app:itemBackground="@android:color/white"
app:itemIconTint="@drawable/bottom_navigation_style"
app:itemTextColor="@drawable/bottom_navigation_style"
app:menu="@menu/bottom_navigation_menu_items" />
style.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorPrimary" android:state_selected="true" />
<item android:color="@android:color/darker_gray" android:state_selected="false" />
</selector>
Whether it can be achieved or not with material design component only, just tell me in answer.
thanks in advance
Upvotes: 3
Views: 2266
Reputation: 4152
Ok, Some curious thing i found is you cannot have background color and text/icon color style on same file.
so u need two files
bottom_navigation_style.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorPrimary" android:state_selected="true" />
<item android:color="@android:color/darker_gray" android:state_selected="false" />
</selector>
bottom_navigation_style_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorPrimaryDark" android:state_checked="true" />
<item android:drawable="@android:color/white" android:state_checked="false" />
</selector>
Then on activity xml you need to add to itemBackground like this
app:itemBackground="@drawable/bottom_navigation_style_background"
app:itemIconTint="@drawable/bottom_navigation_style"
app:itemTextColor="@drawable/bottom_navigation_style"
It will work for sure.
Upvotes: 2
Reputation: 897
Please try one this, In bottom navigation view give a property
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="bottom"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/id_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@drawable/drwable_value_selected"
app:itemBackground="@drawable/drwable_value_selected"
app:menu="@menu/menu" />
</LinearLayout>
app:itemBackground
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/dark" android:state_checked="true"/>
<item android:drawable="@color/white"/>
Check your menu items
<item
android:id="@+id/action_message"
android:enabled="true"
android:checkable="true"
android:title="search"
app:showAsAction="ifRoom" />
checkable true is must for select and unselect background color.
Upvotes: 3