How to Set Menu to Toolbar in Android

How to set menu to Toolbar in Android

just override onCreateOptionsMenu like this in your MainPage.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}

How can we set menu to toolbar

Replace this setToolbar in activity :

public void setToolBar(String title,int resourceMenu) {

mToolbar = findViewById(R.id.toolbar_everywhere_toolbar);
mTextViewToolbarTitle = findViewById(R.id.toolbar_title);
mTextViewToolbarTitle.setText(title);
mToolbar.inflateMenu(resourceMenu);

}

With this :

public void setToolBar(String title) {

mToolbar = findViewById(R.id.toolbar_everywhere_toolbar);
mTextViewToolbarTitle = findViewById(R.id.toolbar_title);
mTextViewToolbarTitle.setText(title);

}

And add this line to your onCreateView fragment or any fragment that you want add menu toolbar to it :

((MainActivity)getActivity()).setToolBar(getString(R.string.authenticationpassword_titletoolbar),R.menu.authenticationpassword_menutoolbar);

Set Menu for Additional Toolbar

You can inflate the menu on toolbar using this.

mFragmentToolbar = (Toolbar) layout.findViewById(R.id.toolbar2);
mFragmentToolbar.inflateMenu(/*your menu resource file*/);
mFragmentToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
return false;
}
});

How to add overflow menu to Toolbar?

Define a Menu for your Toolbar in the res/menu resource folder, for example:

toolbar_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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".activity.MainActivity">

<item
android:id="@+id/action_sign_out"
android:title="@string/toolbar_sign_out"
app:showAsAction="never"/>
</menu>

Setting app:showAsAction="never" ensures that this MenuItem will not be shown in the Toolbar, but placed in the overflow menu instead.

The theme of your Activity should be (or derive from) one of the NoActionBar themes (Theme.AppCompat.NoActionBar for example, or Theme.MaterialComponents.NoActionBar if you're using Material Components).

In your Activity, set up your Toolbar:

Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);

And override onCreateOptionsMenu() to inflate your previously defined menu resource:

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

You can override onOptionsItemSelected() to define the onClick behaviour of your MenuItem(s):

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_sign_out: {
// do your sign-out stuff
break;
}
// case blocks for other MenuItems (if any)
}
return true;
}

How to make custom toolbar menu visible for my android app?

Inside onCreate

..
setSupportActionBar(findViewById(R.id.my_toolbar1))
..

Inside onCreateOptionsMenu

override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_toolbar, menu)
return super.onCreateOptionsMenu(menu)
}

How use setOnClickListner on custom toolbar's menu items?

It is the same as with standard ActionBar.

1) Replace ActionBar with your own material toolbar like so:

    @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
...

2) Override OnCreateOptions menu as usual:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_layout,menu);
super.onCreateOptionsMenu(menu, inflater);
}

3) Handle clicks:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.register: {
...
return true;
}
default:
return super.onOptionsItemSelected(item);
}

}


Related Topics



Leave a reply



Submit