But now, with the release of Design Support Library version 25.0.0, Google now provide an official widget to make this design. This is a good new for Android developers, from now on, we have not depended on any third-party library anymore, just only need to use
BottomNavigationView
in your layout design.
DEMO VIDEO:
Adding Design Support Library dependency
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:design:25.0.0'
Declaring in activity layout
BottomNavigationView
widget to our desired layout file. Remember that this should be aligned with the bottom of the screen with all content displaying above it. We can add this view like so:
activity_main.xml
As you can see, there are 4 important attributes of <?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="info.devexchanges.bottomnavigationview.MainActivity">
<FrameLayout
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="@color/colorPrimaryDark"
app:itemIconTint="@color/white"
app:itemTextColor="@color/white"
app:menu="@menu/menu_bottom_navigation" />
</RelativeLayout>
BottomNavigationView
:
itemBackground
: the background color orDrawable
of the items. Can be set from code with thesetItemBackgroundResource()
method.itemIconTint
: the icon tint for items. Can be set from code with thesetItemIconTintList()
method.itemTextColor
: the text color for the item labels. Can be set from code with thesetItemTextColor()
method.menu
: the menu resource to be used to display items in the bottom navigation menu. Can be set from code withinflateMenu()
method.
Create a menu for Bottom Navigation View
menu
attribute for our BottomNavigationView
. It looks exactly the same as any other menu that we’d use throughout our app, let put a menu resource file in res/menu
folder:
menu_bottom_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/songs"
android:icon="@drawable/song"
android:title="All songs" />
<item
android:id="@+id/genre"
android:icon="@drawable/genre"
android:title="Genres" />
<item
android:id="@+id/album"
android:icon="@drawable/album"
android:title="Albums" />
<item
android:id="@+id/artist"
android:icon="@drawable/artist"
android:title="Artists" />
</menu>
Important Note
: the maximum number of items we can display now is 5. You can check it through call getMaxItem()
method.
Handle selected/unselected states
BottomNavigationView
we can easily handle the display of both selected and unselected menu items. Firstly, create a selector file for our selected/unselected colors (this file is put in res/color
folder):
color_states.xml
Now, change the <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:color="#757575"
android:state_checked="false"/>
<item
android:color="@color/colorAccent"
android:state_checked="true"/>
</selector>
itemIconTint
attribute value of your BottomNavigationView
:
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="@color/colorPrimaryDark"
app:itemIconTint="@color/color_states"
app:itemTextColor="@color/white"
app:menu="@menu/menu_bottom_navigation" />
You will have this output:
Handle menu items click event
setOnNavigationItemSelectedListener()
method of BottomNavigationView
to set a listener for menu item events:
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.artist:
break;
case R.id.genre:
break;
case R.id.album:
break;
case R.id.songs:
break;
default:
break;
}
return true;
}
});
Conclusions
Of course, if you need more code of this project, please check it out on @Github, I will replace corresponding fragments when menu items clicked, output of my sample project like this:
Read more:
- Material design guidelines
- My previous post about Bottom Navigation View