1

I'm trying to implement the BottomAppBar menu widget in android, however on implementation I can see only the menu icon and cannot see all the other menu items(settings, search).

  1. I changed the parent style theme to: Theme.MaterialComponents.Light.NoActionBar
  2. Tried using inflater to inject the menu items in the bottom position and BottomAppbar widget.

Repo link of android project: https://github.com/tracebackerror/StuddyBuddy/

Expected Output using BottomAppMenu :

Expected Image

Current Menu Output:

Current Output

bottomappbar_menu.xml

 <?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/app_bar_fav"
        android:icon="@drawable/ic_favorite_black_24dp"
        android:title="@string/action_favorite"
        app:showAsAction="always"/>

    <item
        android:id="@+id/app_bar_search"
        android:icon="@drawable/ic_search_black_24dp"
        android:title="@string/action_search"
        app:showAsAction="always"/>
    <item
        android:id="@+id/app_bar_settings"
        android:title="@string/action_settings"
        app:showAsAction="never"/>

</menu>

HomeActivity.java

package com.aspireedutech.study_buddy;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class HomeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.bottomappbar_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.app_bar_settings:
                Toast.makeText(this, "Settings Clicked", Toast.LENGTH_SHORT).show();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

}

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>


    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="BottomMenuColor" parent="Theme.AppCompat.DayNight.DarkActionBar">
        <item name="android:textColorSecondary">@android:color/white</item>
    </style>
</resources>

AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".HomeActivity"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_home.xml

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bottom_app_bar"
            style="@style/Widget.MaterialComponents.BottomAppBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:backgroundTint="@color/colorPrimary"

            app:fabAlignmentMode="center"
            app:navigationIcon="@drawable/ic_menu_white_24dp" />


        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_add_white_24dp"
            app:layout_anchor="@id/bottom_app_bar" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
Shivam Chaurasia
  • 474
  • 5
  • 21
  • 1
    You need to set your `BottomAppBar` as the support `ActionBar` for it to inflate the options menu. – Mike M. Nov 24 '19 at 20:05
  • can you point the line number where to change the code for ActionBar. Because i changed the theme but it doesn't worked out. – Shivam Chaurasia Nov 24 '19 at 20:51
  • 1
    Sorry, I meant the change is in your code, not in the theme. In `onCreate()`, after `setContentView()`, it'd be `BottomAppBar bottomAppBar = findViewById(R.id.bottom_app_bar);`, `setSupportActionBar(bottomAppBar);`. – Mike M. Nov 24 '19 at 20:54
  • Thanks Mike, Its working now. Also, to note I had one unsupported attribute(app:navigationIcon="@drawable/ic_menu_white_24dp") in xml file home_activity. which was causing the crash of app and hence i was not able to see the output. – Shivam Chaurasia Dec 06 '19 at 13:45

1 Answers1

3

Just use:

   <com.google.android.material.bottomappbar.BottomAppBar
      app:menu="@menu/bottomappbar_menu"
      .../>

You can also use:

bottomAppBar.replaceMenu(R.menu.bottomappbar_menu);

enter image description here

In this case you have to handle menu options using:

bottomAppBar.setOnMenuItemClickListener(new OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        //....
        return true;
    }
});

Otherwise if you want to setup the menu callbacks in a similar way to Toolbar as described by Mike in the comments you have to:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    //...
    BottomAppBar bottomAppBar = findViewById(R.id.bottom_app_bar);
    setSupportActionBar(bottomAppBar);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.bottomappbar_menu, menu);
    return true;
}
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • tried both steps still all the menu are not visible. Repo link of complete code: https://github.com/tracebackerror/StuddyBuddy/ – Shivam Chaurasia Nov 25 '19 at 00:40
  • @shivam I've tried the code in the answer with 1.1.0-beta02 and 1.2.0-alpha02 and it works. In your code you are mixing the solutions. If you use `setSupportActionBar` you have to implement the methods in the `Activity` as `onCreateOptionsMenu`. If you use `app:menu` in the layout then use `setOnMenuItemClickListener` – Gabriele Mariotti Nov 25 '19 at 07:51
  • 1
    Thanks Gabriel, Its working now. Also, to note I had one unsupported attribute(app:navigationIcon="@drawable/ic_menu_white_24dp") in xml file home_activity. which was causing the crash of app and hence i was not able to see the output. – Shivam Chaurasia Dec 06 '19 at 13:46