How to inflate two different menus for a Toolbar and a BottomAppBar Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to inflate two different menus for a Toolbar and a BottomAppBar without wasting too much if your time.

The question is published on by Tutorial Guruji team.

The following causes a post-install crash in emulator. No debug info shown.

package com.example.myapplication;

import android.os.Bundle;

import com.google.android.material.bottomappbar.BottomAppBar;
import com.google.android.material.tabs.TabLayout;

import androidx.appcompat.widget.Toolbar;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;

import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;

import com.example.myapplication.ui.main.SectionsPagerAdapter;
public class MainActivity extends AppCompatActivity {

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.top_bar, menu);
        return true;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar top_toolbar = findViewById(R.id.top_toolbar);
        setSupportActionBar(top_toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        BottomAppBar bottom_toolbar = findViewById(R.id.bottom_toolbar);
        bottom_toolbar.replaceMenu(R.menu.bottom_bar);
        SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
        ViewPager viewPager = findViewById(R.id.view_pager);
        viewPager.setAdapter(sectionsPagerAdapter);
        TabLayout tabs = findViewById(R.id.tabs);
        tabs.setupWithViewPager(viewPager);
        ImageButton my_posts_button = findViewById(R.id.my_posts_button);
        ImageButton chats_button = findViewById(R.id.chats_button);
        ImageButton post_button = findViewById(R.id.post_button);
        ImageButton settings_button = findViewById(R.id.settings_button);
        ImageButton profile_button = findViewById(R.id.profile_button);
        ImageButton sort_button = findViewById(R.id.search_button);
        ImageButton search_button = findViewById(R.id.sort_button);
    }
}

It looks like the website thinks my post is mostly code.It looks like the website thinks my post is mostly code.It looks like the website thinks my post is mostly code.

Answer

You are using both a Toolbar and a BottomBar. You can use the onCreateOptionsMenu only for the bar used in the setSupportActionBar.

For example:

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

and onCreateOptionsMenu for the Toolbar menu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.toolbar_menu, menu);
    return true;
}

Then for the BottomBar you can use the app:menu in the layout or the replaceMenu method:

    BottomAppBar bottomAppBar = findViewById(R.id.bottomappbar);
    bottomAppBar.replaceMenu(R.menu.bottom_bar);
    bottomAppBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            if (item == R.id...){
                //....
                return true;
            }
            return false;
        }
    });
We are here to answer your question about How to inflate two different menus for a Toolbar and a BottomAppBar - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji