0

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" in a color selector resource file doesn't seem to work.I also tried to entered more things like android:state_focused="true" or android:state_enabled="true", no effect unfortunately.

(BottomnavigationView)

 <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:itemIconTint="@color/menu_txt_color"
        app:itemTextColor="@color/menu_txt_color"
        android:background="@drawable/bnv_tab_item_foreground"
        app:layout_constraintBottom_toTopOf="@+id/guideline14"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/navigation" />

(Color Selector)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/add_child" android:state_checked="true" android:state_pressed="true"/>
    <item android:drawable="@color/light_blue" android:state_checked="false"/>

</selector>

(My Menu Resource)

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <item
        android:id="@+id/tab1"
        android:layout_height="wrap_content"
        android:icon="@drawable/baby3"
        android:checked="true"
        android:title="hello"
        app:showAsAction="always|withText" />

    <item
        android:id="@+id/tab2"
        android:icon="@drawable/baby2"
        android:title="heelo2"
        app:showAsAction="always|withText|collapseActionView" />


    <item
        android:id="@+id/tab3"
        android:checkable="false"
        android:icon="@drawable/baby2"
        android:title="hello 3"
        app:showAsAction="always|withText" />

    <item
        android:id="@+id/tab4"
        android:checkable="false"
        android:icon="@drawable/baby3"
        android:title="heelo 4"
        app:showAsAction="always|withText" />

</menu>

Tabs are highlighted when i selected ,I want BottomNavigationBar tabs like Instagram, i also tried (How to create bottom navigation bar similar to instagram) but it no help..

Has anyone encountered this situation?

Thanks

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Rahul Bh
  • 123
  • 2
  • 15

1 Answers1

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"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android: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{
    View view = navigationView.getMenu().findItem(R.id.nav_1).getActionView();
            Button button = (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"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="@color/Primary" />
    <item android:state_focused="false" android:color="@color/IconsColor" />
    <item android:state_selected="false" android:color="@color/IconsColor" />
    <item android:state_focused="true" android:color="@color/Primary" />
    <item android:state_pressed="true" android:color="@color/white" />
    <item android:color="@color/tabsScrollColor" />
</selector>
Aniruddh Parihar
  • 3,072
  • 3
  • 21
  • 39
  • Thanks @Aniruddh Parihar for your answer but the problem is still not solve i tried to change the button background color but nothing is happen it all show blank... – Rahul Bh Sep 12 '18 at 07:46
  • have you tried without app:itemIconTint="@color/menu_txt_color" in BottomNavigationView.? – Aniruddh Parihar Sep 12 '18 at 09:29
  • I need one more help if you don't mind .......I want my tab icon stay still like instagram bottom tab....do you know how it would be work...?? – Rahul Bh Sep 12 '18 at 10:46
  • https://www.google.com/search?q=instagram+bottom+bar+android&source=lnms&tbm=isch&sa=X&ved=0ahUKEwjEmPSmsLXdAhXEzbwKHc_YDacQ_AUICigB#imgrc=KEwZ3o2Z_jlDMM: this type of navigation bar ....the icon remains still only background changes... icons not moviing – Rahul Bh Sep 12 '18 at 12:28
  • have you tried this, in selector ? – Aniruddh Parihar Sep 12 '18 at 12:48