Skip to content Skip to sidebar Skip to footer

Using Different Background Color Of Bottom Navigationbar Tab

I'm adding a BottomNavigationView to a project, and I would like to have a different background color for each bottom tab.Using a different color with android:state_selected='true'

Solution 1:

try this way

use Custom Action layout as menu

<item
    android:id="@+id/nav_1"
    android:title=""
    app:actionLayout="@layout/custom_layout"/>

<item
    android:id="@+id/nav_2"
    android:title=""
    app:actionLayout="@layout/custom_layout"/>

<item
    android:id="@+id/nav_3"
    android:title=""
    app:actionLayout="@layout/custom_layout"/>

<item
    android:id="@+id/nav_4"
    android:title=""
    app:actionLayout="@layout/custom_layout"/>

<item
    android:id="@+id/nav_5"
    android:title=""
    app:actionLayout="@layout/custom_layout"/>

custom_layout will be like this in layout folder

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="0dp"android:layout_gravity="center_vertical"android:background="@drawable/selector"android:layout_weight="1"android:gravity="center_vertical"/></LinearLayout>

at Activity where you using BottomNavigationView you can do like this

try{
    Viewview= navigationView.getMenu().findItem(R.id.nav_1).getActionView();
            Buttonbutton= (Button) view.findViewById(R.id.button);
             //button.setOnClickListener(this);// similarly for others menu in BottomNavigationView
        } catch (Exception e) {
            AppLogger.getInstance().writeException(e);
        }

Update

selector.xml

<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_selected="true"android:color="@color/Primary" /><itemandroid:state_focused="false"android:color="@color/IconsColor" /><itemandroid:state_selected="false"android:color="@color/IconsColor" /><itemandroid:state_focused="true"android:color="@color/Primary" /><itemandroid:state_pressed="true"android:color="@color/white" /><itemandroid:color="@color/tabsScrollColor" /></selector>

Post a Comment for "Using Different Background Color Of Bottom Navigationbar Tab"