Set active/inactive drawables for bottomavigationview - android

I am trying to put active/inactive drawables for bottomnavigationview menu items. Going through several post in SO and browsing in Google I could find only setting colour for active/inactive state using app:itembackgroundColor what I want to achieve is set drawable when selected.
I tried this for menuitems
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/navigation_home"
android:icon="#drawable/home_navigation_view_home_selector"
android:checked="false"
android:title="#string/title_home" />
<item
android:id="#+id/navigation_dashboard"
android:icon="#drawable/home_navigation_view_oppurtunity_selector"
android:checked="false"
android:title="#string/title_oppurtunity" />
<item
android:id="#+id/navigation_notifications"
android:icon="#drawable/home_navigation_view_leads_selector"
android:checked="false"
android:title="#string/title_leads" />
<item
android:id="#+id/navigation_settings"
android:icon="#drawable/home_navigation_view_settings_selector"
android:checked="false"
android:title="#string/title_settings" />
</menu>
where
home_navigation_view_home_selector/
home_navigation_view_oppurtunity_selector/
home_navigation_view_leads_selector/
home_navigation_view_settings_selector
are selector for each item.
home_navigation_view_home_selector.xml
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_home_selected"
android:state_checked="true" />
<item android:drawable="#drawable/ic_home_black_24dp" />
</selector>
the bottomnavigationview code is as follows
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
android:foreground="?attr/selectableItemBackground"
app:menu="#menu/navigation" />
but doesn't seem to work.
Any help will be appreciated.

Try:
navigation.setItemIconTintList(null);
in your code.
Bottom Navigation View uses color tinting by default, so you have to disable it.

Related

Set a particular group item at end in Navigation Drawer menu

okay, Google Keeps-Notes and Lists App have a navigation drawer like this,
Google Keeps-Notes and Lists App Navigation Drawer(Original)
I want to do same exact as I mention in this image. I want this
'EDIT'
I want that EDIT (the TextView) at right side of navigation drawer.
I achieved this far,My Navigation Drawer
<group>
<item android:title="LABALS"
android:actionLayout="#layout/main_nav_drawer_edit">
<menu>
<item
android:id="#+id/navigation_drawer_lable"
android:title="Label 1" />
<item
android:id="#+id/navigation_drawer_create_new_label"
android:title="Create new label" />
</menu>
</item>
what should I do to achieve this, Is it possible?
Create a custom View and apply to the app:actionLayout attribute of your <item>.
<item
android:id="#+id/grp_labeled"
android:title="LABALS"
app:actionLayout="#layout/main_nav_drawer_edit">
<menu>
<item
android:id="#+id/navigation_drawer_lable"
android:title="Label 1" />
<item
android:id="#+id/navigation_drawer_create_new_label"
android:title="Create new label" />
</menu>
</item>
layout:
<androidx.constraintlayout.widget.ConstraintLayout 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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EDIT"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
like this?

Item showing only on click on BottomNavigationView

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"

BottomNavigationView not show icon

I set an icon for BottomNavigationView but it not show .
My code:
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="?android:attr/windowBackground"
app:menu="#menu/navigation" />
Menu :
<item
android:id="#+id/navigation_home"
android:icon="#drawable/ic_tab_bhxh"
android:title="#string/main_tab_social_bhxh"
app:showAsAction="ifRoom" />
<item
android:id="#+id/navigation_dashboard"
android:icon="#drawable/ic_tab_bhxh"
android:title="#string/main_tab_social_bhyt"
app:showAsAction="ifRoom" />
<item
android:id="#+id/navigation_notifications"
android:icon="#drawable/ic_tab_bhxh"
android:title="#string/main_tab_social_news"
app:showAsAction="ifRoom" />
and my program :
If you have any suggest , please tell my know .
After tried some image with different background , I resolved my issue with icon background color is white .

Android BottomNavigationView disable icon title

I am trying to create Bottom navigation view in android without a title, but I'm unable to hide the title. Also, I need to active and inactive icon colour for Bottom navigation view.
Expected design
Current design
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_alignParentBottom="true"
android:background="#color/bottom_nav_colour"
app:itemTextColor="#color/black"
app:itemIconTint="#color/black"
design:menu="#menu/bottom_navigation" />
bottom_navigation.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_home"
android:title="#string/menu_home"
android:icon="#drawable/home" />
<item
android:id="#+id/menu_search"
android:title="#string/menu_search"
android:icon="#drawable/search"/>
<item
android:id="#+id/menu_add"
android:title="#string/menu_add"
android:icon="#drawable/add"/>
<item
android:id="#+id/menu_wishlist"
android:title="#string/menu_wishlist"
android:icon="#drawable/wishlist"/>
<item
android:id="#+id/menu_account"
android:title="#string/menu_account"
android:icon="#drawable/account" />
</menu>
In the latest support library for BottomNavigationView you can use labelVisibility
You can create selector for icons.
selector_home_icon.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="drawable/home" android:state_selected="true"/>
<item android:drawable="drawable/home_disabled"/>
</selector>
Set this selector as icon drawable
bottom_navigation.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_home"
android:title="#string/menu_home"
android:icon="#drawable/selector_home_icon" />
.......
</menu>

BottomNavigation Icon Tint

I just added the BottomNavigation into my android app, but two of my four icons in there don't have the right color.
This is how it looks like in Android Studio Preview:
And this is how it looks like on the device:
All the icons are in my drawable folder.
This is the xml for the BottomNavigation:
<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="#color/colorAccent"
app:itemTextColor="#color/colorAccent"
app:menu="#menu/bottom_navigation_main" />
And this is the xml for the items:
<item
android:id="#+id/action_home"
android:enabled="true"
android:icon="#drawable/ic_home_white_48dp"
android:title="#string/home"
app:showAsAction="always" />
<item
android:id="#+id/action_requests"
android:enabled="true"
android:icon="#drawable/ic_collections_white_48dp"
android:title="#string/requests"
app:showAsAction="always" />
<item
android:id="#+id/action_profile"
android:enabled="true"
android:icon="#drawable/ic_account_circle_white_48dp"
android:title="#string/profile"
app:showAsAction="always" />
<item
android:id="#+id/action_recents"
android:enabled="true"
android:icon="#drawable/ic_schedule_white_48dp"
android:title="#string/recents"
app:showAsAction="always" />
Hope you can help me.
While making a selector, always keep the default state at the end, otherwise only default state would be used. You need to reorder the items in your selector as:
And the state to be used with BottomNavigationBar is state_checked not state_selected.

Categories

Resources