Bottom NavigationView Example

June 20, 2017
Download Complete Project Here :BottomNavigationView.zip

screenshot:


build.gradle:

apply plugin: 'com.android.application'
android {
    compileSdkVersion
25
   
buildToolsVersion "25.0.2"
   
defaultConfig {
        applicationId
"com.ramsandroid.bottomnavigationviewexample"
       
minSdkVersion 15
       
targetSdkVersion 25
       
versionCode 1
       
versionName "1.0"
       
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
       
vectorDrawables.useSupportLibrary = true
   
}
    buildTypes {
        release {
            minifyEnabled
false
           
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
       
}
    }
}

dependencies {
    compile fileTree(
dir: 'libs', include: ['*.jar'])
    androidTestCompile(
'com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude
group: 'com.android.support', module: 'support-annotations'
   
})
    compile
'com.android.support:appcompat-v7:25.1.1'
   
compile 'com.android.support:design:25.1.1'
   
compile 'com.android.support:support-vector-drawable:25.1.1'
   
testCompile 'junit:junit:4.12'}

strings.xml:

<resources>
    <string name="app_name">Bottom NavigationView Example</string>
    <string name="title_home">Home</string>
    <string name="title_dashboard">Search</string>
    <string name="title_notifications">Notifications</string>
</resources>


create navigation.xml file under menu folder and keep following code and you can icons from downloaded project
  

<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">

    <
item
       
android:id="@+id/navigation_home"
       
android:icon="@drawable/home"
       
android:title="@string/title_home" />

    <
item
       
android:id="@+id/navigation_dashboard"
       
android:icon="@drawable/search"
       
android:title="@string/title_dashboard" />

    <
item
       
android:id="@+id/navigation_notifications"
       
android:icon="@drawable/notifications"
       
android:title="@string/title_notifications" />

</
menu>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout 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:id="@+id/container"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical"
   
tools:context="com.ramsandroid.bottomnavigationviewexample.MainActivity">

    <
FrameLayout
       
android:id="@+id/content"
       
android:layout_width="match_parent"
       
android:layout_height="0dp"
       
android:layout_weight="1">

        <
TextView
           
android:id="@+id/message"
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content"
           
android:layout_marginBottom="@dimen/activity_vertical_margin"
           
android:layout_marginLeft="@dimen/activity_horizontal_margin"
           
android:layout_marginRight="@dimen/activity_horizontal_margin"
           
android:layout_marginTop="@dimen/activity_vertical_margin"
           
android:text="@string/title_home" />

    </
FrameLayout>

    <
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"
       
app:menu="@menu/navigation" />

</
LinearLayout>

MainActivity.java:
package com.ramsandroid.bottomnavigationviewexample;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView mTextMessage;
    @Override
   
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
           
= new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
       
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    return true;
                case R.id.navigation_dashboard:
                    mTextMessage.setText(R.string.title_dashboard);
                    return true;
                case R.id.navigation_notifications:

                    mTextMessage.setText(R.string.title_notifications);
                    return true;
            }
            return false;
        }

    };



}


Read Also : Dialog With Multiplechoice ListView


Artikel Terkait

Previous
Next Post »