In my BottomNavigationView, I want to add ripple effect and then change the background color of my selected navigation item. Here is a screenshot of what i've done so far
My Drawable File
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorAccent" android:state_checked="true"/>
<item android:drawable="#color/colorPrimaryDark" android:state_checked="false"/>
</selector>
My Layout File
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:itemBackground="#drawable/navigaton_item_bg"
app:itemIconTint="#android:color/white"
app:itemTextColor="#android:color/white"
app:menu="#menu/navigation" />
Related
How to change menu item text color in xml file
in drawable file the code looks like this file name: bottom_nav_menu_item_text_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color= "#color/bottom_nav_menu_item_selected_color"
android:state_checked="true"
android:state_selected="true"/>
<item
android:color= "#color/bottom_nav_menu_item_unselected_color"
/>
</selector>
Bottom Navigation Menu Bar looks like this in activity_main.xml
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/bottom_nav_menu_bar"
app:menu="#menu/bottom_nav_menu_items"
app:itemTextColor="#drawable/bottom_nav_menu_item_text_color"
android:background="#color/white"
/>
Can add more info with SS or gif about current behaviour so exact solution can be provided
But as you asked question about changing color of BottomNavigationView text change then you can use as mentioned below
tab_text_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false" android:color="#color/unselectedTab" />
<item android:state_selected="true" android:color="#color/selectedTab" />
<item android:color="#color/unselectedTab" />
</selector>
In Activity xml
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/bottom_nav_menu_bar"
app:menu="#menu/bottom_nav_menu_items"
app:itemTextColor="#drawable/tab_text_selector"
android:background="#color/white"
/>
I have used images as icon but I dont want them to look grey when they are not selected. It should look same as picture whether its active or not. What should I do?
com.google.android.material.bottomnavigation.BottomNavigationView
Thanks
Try using this in your XML file in the BottomNavigationView part
app:itemIconTint="null"
Or add this to your java file after defining your BottomNavigationView (with the findViewById)
//Replace BottomNavView with your nav bar id
BottomNavView.setItemIconTintList(null);
A long way to achieve it
mBottomNav.setItemIconTintList(null);
Then do the designs yourself. Don't forget to separate the buttons as clicked and not clicked.
Example Button XML
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--Clicked-->
<item android:drawable="#drawable/homeclicked" android:state_checked="true" />
<!--Not Clicked-->
<item android:drawable="#drawable/homenotclicked" android:state_checked="false" />
</selector>
And add them to the view: Example bottom_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/homebuttons"
android:icon="#drawable/homebuttonxml />
<!--Other Buttons...-->
</menu>
And finally, Link view to bottomnavigationview
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:labelVisibilityMode="unlabeled"
app:elevation="0dp"
app:menu="#menu/bottom_navigation">
</com.google.android.material.bottomnavigation.BottomNavigationView>
The default color used by not selected items is based on the colorOnSurface color.
Just use:
<com.google.android.material.bottomnavigation.BottomNavigationView
android:theme="#style/BottomNavigationViewThemeOverlay"
.... />
with:
<style name="BottomNavigationViewThemeOverlay">
<item name="colorOnSurface">#color/...</item>
</style>
Otherwise you can define your custom selector:
<com.google.android.material.bottomnavigation.BottomNavigationView
app:itemIconTint="#color/bottomnavicontint"
../>
with:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="1.0" android:color="?attr/colorPrimary" android:state_checked="true"/>
<item android:alpha="0.6" android:color="#color/..."/>. <!-- not selected -->
</selector>
Is it possible to change bottom navigation view color tint to a custom color created using xml? If so how do I do this? I am aware that you can change the icon color tint with the property "app:itemIconTint="#color/colorPrimaryDark", but I am not sure how to go about referencing the color in my drawable for the iconTint color.
Bottom navigation view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.examp.smartshop.Controller.MainActivity">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/bottom_navigation">
</FrameLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemIconTint="#color/colorPrimaryDark"
app:itemTextColor="#color/colorPrimaryDark"
app:menu="#menu/bottom_navigation"
app:itemIconSize="25sp"
android:background="?android:attr/windowBackground"/>
</RelativeLayout>
**Custom color**
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="#EC6EAD"
android:startColor="#3494E6"
android:type="linear" />
<corners
android:radius="0dp"/>
</shape>
Android BottomNavigationView
To disable this behaviour
bottomNavigationView.itemIconTintList = null
**
Change Icon and Selected Icon
**
Navigation Item (including text and icon) is specified in a menu file.
<com.google.android.material.bottomnavigation.BottomNavigationView
...
app:menu="#menu/navigation"
/>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/navigation_dailyquote"
android:icon="#drawable/yy_dailyquote"
android:title="#string/navigation_dailyquote"/>
...
</menu>
To support both icon and selected icon, create res/drawable/yy_dailyquote.xml.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/yy_dailyquote_sun" android:state_checked="false"/>
<item android:drawable="#drawable/yy_dailyquote_sun_active" android:state_checked="true"/>
</selector>
TextColor and selected TextColor
Change TextColor
<com.google.android.material.bottomnavigation.BottomNavigationView
...
app:itemTextColor="#998971"
/>
Support TextColor and Selected TextColor
<com.google.android.material.bottomnavigation.BottomNavigationView
...
app:itemTextColor="#color/navigation_text_color"
/>
Create res/color/navigation_text_color.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#9A8971" />
<item android:state_checked="false" android:color="#AD9F88"/>
</selector>
Change Background Color
<com.google.android.material.bottomnavigation.BottomNavigationView
...
android:background="#FDFAE6"
/>
UPDATE:
The other question uses a white background and since it uses white background there is no ripple on the BottomNavigationBar (because the ripple itself is white). But in my case the ButtomNavigationBar is blue, and it reflects the ripple. In fact all of the colors except the white(that the other question uses) reflect the incomplete ripple.
ORIGINAL:
I am using ButtomNavigationView, and the problem is that it doesn't show ripple effect in the background of it's Items.
Or it doesn't care about app:itemBackground it just uses android:background value without any ripple effect.
I've tried using a ripple instead of selector in drawable-21 and changing the app:itemBackground color values but it is not working.
ButtomNavigationView:
<android.support.design.widget.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="0dp"
android:layout_height="56dp"
android:theme="#style/BottomNavigationStyle"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/bottom_nav_menu" />
ButtomNavigationStyle:
<style name="BottomNavigationStyle">
<item name="itemBackground">#drawable/navigation_bar_item_bg</item>
<item name="itemIconTint">?attr/bottom_nav_colors</item>
<item name="itemTextColor">?attr/bottom_nav_colors</item>
<item name="android:background">?attr/colorPrimary</item>
<item name="android:fontFamily">#font/iransans_mobile</item>
</style>
bottom_nav_menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/navigation_mylibrary"
android:icon="#drawable/ic_library_books_black_24dp"
android:title="#string/title_mylibrary" />
<item
android:id="#+id/navigation_search"
android:icon="#drawable/ic_search_black_24dp"
android:title="#string/title_search" />
.
.
.
</menu>
navigation_bar_item_bg:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/black_color"
android:state_checked="true"/>
<item android:drawable="#drawable/black_color"/>
</selector>
ScreenShot:
https://firebasestorage.googleapis.com/v0/b/library-1696f.appspot.com/o/Screenshot%20from%202019-06-17%2005-39-23.png?alt=media&token=20efbe56-3234-47ab-b6cc-898124b77762
I've searched and I've found that it is a library bug that is not fixed yet.
But I've found a solution for this.
1) Since the android:background and android:itemBackground don't work correctly delete both of them from BottomNavigationView.
2) Create a new FrameLayout and put your BottomNavigationView inside FrameLayout.
3) change these properties of the FrameLayout:
android:layout_width="match_parent"
android:layout_height="wrap_content"
4)Finally Add your desired color for ButtomNavigationView into FrameLayout as android:background.
Example:
<FrameLayout
android:id="#+id/buttomnavigation_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/blue"><!--Background color for BNV-->
<android.support.design.widget.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="56dp"
app:itemIconTint="#color/bottom_navigation_colors"
app:itemTextColor="#color/bottom_navigation_colors"
app:labelVisibilityMode="labeled"
app:menu="#menu/bottom_nav_menu"/>
</FrameLayout>
bottom_navigation_colors.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="#FFFFFF" />
<item
android:state_checked="false"
android:color="#C7FFFFFF" />
</selector>
So I want a navigation bar at the bottom of the screen with 4 items but for some reason only the first one is showing and the others appear only when i click them.
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/white"
app:itemTextColor="#color/black"
app:menu="#menu/navigation_bar_bottom_menu" />
My menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/nav_home"
android:title="#string/navigation_bar_home_text" />
<item
android:id="#+id/nav_inbox"
android:title="#string/navigation_bar_inbox_text" />
<item
android:id="#+id/nav_notes"
android:title="#string/navigation_bar_notes_text" />
<item
android:id="#+id/nav_profile"
android:title="#string/navigation_bar_profile_text" />
</menu>
The bar is displayed like this just with the first item showing:
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#color/gray_background"
app:itemIconTint="#color/nav_item_state_list"
app:itemTextColor="#color/nav_item_state_list"
app:labelVisibilityMode="labeled"
android:theme="#style/Widget.BottomNavigationView"
app:menu="#menu/bottom_navigation_items"/>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/colorPrimaryLite"
android:state_pressed="true"/>
<item android:color="#color/colorPrimaryLite"
android:state_checked="true"/>
<item android:color="#color/grayDark"
android:state_checked="false"/>
<item android:color="#color/white"/>
</selector>
Here is the example
Create a color file
navigation_item_text_color.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#color/black" />
<item android:color="#android:color/darker_gray" />
</selector>
then put to color folder (if you don't have color folder -> just create it manually)
Then
<com.google.android.material.bottomnavigation.BottomNavigationView
...
app:itemTextColor="#color/navigation_item_text_color" />
More
If you bottom menu item have icon, you can change icon color like
app:itemIconTint="#color/navigation_item_text_color"