I faced with the following problem:
I use BottomNavigationView in my program. I added elements to it by creating a menu and then added it. But now this menu is also shown on the screen (there are 3 dots on ActionBar (unfortunately I don't know how they are called)).
But there's no need in this menu. How can I remove it from ActionBar?
If you don't want this menu just delete from your code the onCreateOptionsMenu() method.
What you need to do is clear the menu before inflating the new menu XML.
Like this:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.your_menu, menu);
}
Related
How can I add Menu option in Blank Activity? I've watched lots of videos to add menu option, they keep on showing menu folder in res, while I don't see menu folder in my project. How can I add menu option?
After creating menu layout, You need to inflate your menu in the blank activity as well something like this
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
return true;
}
replace my_menu xml file with your ones
In our app, we have a few fragments that can be shown either as fullscreen fragments or as dialog fragments. These fragments inherit from DialogFragment and we make sure to instantiate them correctly depending the mode the app is executed in (either fullscreen or dialog).
We thought about adding some extra functionality to some of these dialog fragments after the latest changes in the Toolbar widget were introduced in the support library with Lollipop. The idea is to have the type of options menu we would normally have in an ordinary fragment (i.e. the options menu inflated after onCreateOptionsMenu is executed) present in our subclasses of DialogFragment ONLY when these are visualized as dialogs. In short: when the fragments are shown in fullscreen mode we inflate a traditional options menu, and when the fragments are shown as dialogs we would like to have the same options menu inflated but using the new toolbar widget in standalone mode.
I followed the steps from http://android-developers.blogspot.dk/2014/10/appcompat-v21-material-design-for-pre.html and I managed to "activate" the toolbar, but it seems the menu is not inflated - see attached screenshots (picture one fragment in fullscreen mode, and picture two in dialog mode).
Is it even possible to inflate an options menu with the new toolbar in a DialogFragment?
Is it even possible to inflate an options menu with the new toolbar in
a DialogFragment?
first of all your design is ok and toolbar is supposed to be used every where lets have a look at this from Chris Banes google engineer link:
// Set an OnMenuItemClickListener to handle menu item clicks
toolbar.setOnMenuItemClickListener(
new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// Handle the menu item
return true;
}
});
// Inflate a menu to be displayed in the toolbar
toolbar.inflateMenu(R.menu.your_toolbar_menu);
and also android developer toolbar standalone sample:
http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html
Yes, DialogFragment has setHasOptions() function. Define the toolbar in the layout of your dialog and use it as if it is in an activity. A toolbar doesnt mind being in an activity or a fragment or a dialog fragment.......
Be sure that you use
setHasOptionsMenu(true) in onActivityCreated method....
Then, as usual override
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.my_menu, menu);
}
and
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
How can I disable options menu item from my action bar?
I made my own custom bar, for display *.png logo, but i don't want to display three dots button of options menu.
I've tried to find some solution, but nothing works.
Code Monkey's answer will do what you want, but as a side effect it will also not allow you to add ANY action items to the Action Bar at all. I believe the correct answer is
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
return false;
}
This is what you want.
Simply remove the public boolean onCreateOptionsMenu(Menu menu) method from your Activity.
In the java class containing the code for the app you are developing, all you need to do is remove the following method:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.main, menu);
}
This is automatically generated in your project by default. That's why you need to remove it.
Hope this helps :)
If you want this, not necessary Override onCreateOptionsMenu method in your activity.
Leave it with empty block.
I have a fragment activity.
If the screen is two panel menu is menu1 else menu2.
If a rotate screen it becames one panel with menu1+menu2 when i rotate the action menu.
If is it possible to delete previous menu1.
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu1, menu);
You can use
menu.clear();
... to clear and remove the menu items.
I understand that after Android 3 menu buttons are not supported. So, I simply want to use a buton on the screen to inflate the menu. I currently use an overridden
public boolean oncreateoptionsmenu(Menu menu) { MenuInflater inflater=getMenuInflater();
inflater.inflate( R.menu.menu, menu); return true}.
This script runs on menu button press, but I also want it to run on my button press. How can this be done? What data is sent to the Menu parameter ( the menu to inflate to)?
Thanks,
On android 3+ the menu will be added to the actionbar automatically when the activity starts. There is no need to change anything.