Lewis Black
Lewis Black

Reputation: 987

How to implement a bottom navigation bar in android?

I'm very new to Android development so i'm sure i'm making beginners mistakes here. I'm trying to implement a bottom navigation bar by following this guide here: https://segunfamisa.com/posts/bottom-navigation-view-android

I've downloaded the demo project and it works. I'm now trying to copy the code for the bottom navigation and make it work in another android studio project and am getting this error.

enter image description here

I think the cause of this error is this piece of xml in my main activity where it's referencing the bottom_nav_items i am trying to have show:

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="start"
    design:menu="@menu/bottom_nav_items" />

I think this is to do with the folder structure I have. The demo project has a different folder structure to my project.

Demo Project

Demo Project

Where as the project I have created has this folder structure.

My Project

My Project

I think the problem is when I add folders in my project the folder doesn't appear in the package folder whereas in the demo project this is the case.

EDIT ---------------

This is my buildgradle (Project Celebreak) file : apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
    applicationId "com.lewisblack.celebreak"
    minSdkVersion 16
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

sourceSets {
    main {
        java.srcDirs = ['src/main/java', 'src/main/java/com/lewisblack/celebreak/model']
        res.srcDirs = ['src/main/res', 'src/main/res/menu']
    }
}
}

repositories {
    mavenCentral()
}

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:design:25.0.0'
compile 'com.android.support:appcompat-v7:25.3.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.google.android.gms:play-services:10.2.1'
compile 'com.google.firebase:firebase-core:10.2.0'
compile 'com.facebook.android:facebook-android-sdk:[4,5)'
testCompile 'junit:junit:4.12'
}

apply plugin: 'com.google.gms.google-services'

Upvotes: 0

Views: 2383

Answers (5)

Aniket Jadhav
Aniket Jadhav

Reputation: 166

In Android Studio 2.3 they have provided this at the start of new project. Create new project and choose the bottom navigation for the start activity of your project and study their code as they provide the perfect code.

You will get other codes but, google will provide the best.

Good Luck!

Upvotes: 0

Canberk &#199;akmak
Canberk &#199;akmak

Reputation: 197

Firstly add this in layout :

   <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/white"
        app:itemIconTint="@color/bottomdrawer"
        app:itemTextColor="@color/bottomdrawer"
        app:menu="@menu/bottom_navigation_main" />

then add menu like this :

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_life"
        android:enabled="true"
        android:icon="@drawable/ic_favorite_white_24dp"
        android:title="@string/bottom_main"
        app:showAsAction="always" />
    <item
        android:id="@+id/action_contact"
        android:enabled="true"
        android:icon="@drawable/ic_favorite_white_24dp"
        android:title="@string/bottom_contact"
        app:showAsAction="always" />
    <item
        android:id="@+id/action_social"
        android:enabled="true"
        android:icon="@drawable/ic_favorite_white_24dp"
        android:title="@string/bottom_social"
        app:showAsAction="always" />
    <item
        android:id="@+id/action_application"
        android:enabled="true"
        android:icon="@drawable/ic_favorite_white_24dp"
        android:title="@string/bottom_app"
        app:showAsAction="always" />
    <item
        android:id="@+id/action_menu"
        android:enabled="true"
        android:icon="@drawable/ic_favorite_white_24dp"
        android:title="@string/bottom_menu"
        app:showAsAction="always" />
</menu>

Then in your Activity implement this :

BottomNavigationView bottomNavigationView;

Upvotes: 1

rafsanahmad007
rafsanahmad007

Reputation: 23881

It should be:

xmlns:app="http://schemas.android.com/apk/res-auto"
app:menu="@menu/bottom_nav_items"

also make sure you had this in Gradle:

compile 'com.android.support:design:25.0.0'

For more attributes of BottomNavigationView see the Documentation

Upvotes: 0

Rajan Kali
Rajan Kali

Reputation: 12953

You need to create menu as Directory not as Android Resource Directory and then paste your bottom_nav_items.xml in it. enter image description here

Upvotes: 0

Darish
Darish

Reputation: 11481

It is not design:menu="@menu/bottom_nav_items", it should be app:menu="@menu/bottom_nav_items"

Upvotes: 0

Related Questions