How to place bottomAppBar in layout

I'm currently working on a project that uses the bottom app bar as its main navigation. but I always get an error even when I'm copy-pasting from an example source code

Here is my layout file

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.bottomappbar.BottomAppBar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"></com.google.android.material.bottomappbar.BottomAppBar>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</LinearLayout>

Here is my gradle dependencies

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    implementation 'com.google.android.material:material:1.1.0-alpha05'
    implementation 'com.squareup.okhttp3:okhttp:3.10.0'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

Here an error that I got

Binary XML file line #14: Binary XML file line #14: Error inflating class com.google.android.material.bottomappbar.BottomAppBar

Upvotes: 2

Views: 1379

Answers (3)

I found the solution to my problem last night. My problem is that I use

implementation 'com.google.android.material:material:1.1.0-alpha05'

and that version is still an alpha and not stable. So I downgraded it to

implementation 'com.google.android.material:material:1.0.0'

and everything runs as smooth as butter

Upvotes: 1

Scrobot
Scrobot

Reputation: 1981

I solved this problem recently. You need to add dependency of

implementation 'com.google.android.material:material:1.0.0

And then add <com.google.android.material.bottomnavigation.BottomNavigationView to your XML layout file.

It's worked for me

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <com.google.android.material.bottomnavigation.BottomNavigationView
            android:layout_width="0dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_height="wrap_content" android:id="@+id/vBottomBar"
            app:layout_constraintHorizontal_bias="0.0"/>
    <FrameLayout
            android:layout_width="0dp"
            android:layout_height="0dp" app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/vBottomBar" android:id="@+id/vContainer">

    </FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 1

sfdkiaei
sfdkiaei

Reputation: 31

According to this website: Set your targetSdkVersion to the latest API version targetting Android P which is 28 and make sure your app inherits Theme.MaterialComponents theme in order to make BottomAppBar use the latest style. Alternatively, you can declare the style for BottomAppBar in widget declaration within layout xml file as follows: style="@style/Widget.MaterialComponents.BottomAppBar"

Upvotes: 1

Related Questions