Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background color change in BottomNavigationView

I have implemented BottomNavigationView which is available from the new support library 25.0.0. Here is my code for that

<android.support.design.widget.BottomNavigationView     android:id="@+id/bottom_navigation"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_alignParentBottom="true"     app:itemBackground="@color/colorPrimary"     app:itemIconTint="@drawable/text"     app:itemTextColor="@drawable/text"     app:menu="@menu/bottom_navigation_main" /> 

And text.xml drawable

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">     <item android:color="@android:color/white" android:state_enabled="true" />     <item android:color="@color/colorPrimaryDark" android:state_enabled="false" /> </selector> 

With this code I am able to change text color when menu item is clicked, but when I apply same thing to app:itemBackground it is showing error <item> tag requires a 'drawable' attribute or child tag defining a drawable.

This is what I have tried for app:itemBackground

app:itemBackground="@drawable/text" 

So my question is how can I change the background color of the selected menu item?

like image 826
Ravi Avatar asked Jan 02 '17 04:01

Ravi


People also ask

How do I change my background on Kotlin?

To set Android Button background color, we can assign android:backgroundTint XML attribute for Button in layout file with the required Color Value. To programmatically set or change Android Button background color, we may call pass the method Button.

How do you change the color of your badge on Android?

Just use the method setBackgroundColor() of the badge.

What is Bottom navigation in android?

Bottom navigation bars make it easy for users to explore and switch between top-level views in a single tap. They should be used when an application has three to five top-level destinations.


1 Answers

found an answer from this medium post

  1. We need to use android:state_checked instead of android:state_enabled
  2. within onNavigationItemSelected you need to use return true instead of return false.

and to set background, we cannot use android:color in <item>, we need to use android:drawable

So here how it looks xml file when you are setting it for app:itemTextColor and app:itemIconTint

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

and to set app:itemBackground selector

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">     <item android:drawable="@drawable/banner_white" android:state_checked="true"/>     <item android:drawable="@drawable/banner_green" android:state_checked="false"/> </selector> 

Here banner_white and banner_green are pngs.

like image 195
Ravi Avatar answered Oct 11 '22 06:10

Ravi