Set drop down menu of Action bar invisible - android

I am using Android native action bar in my app. I created the drop down menu of action bar by:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbar_menu, menu);
return true;
}
For certain page, I would like to hide the action bar drop down menu. How to achieve this? Seems there is no direct method to set visibility to false for this drop down menu...

I managed to resolve this problem myself by set each menu item in drop down menu of action bar to be invisible:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbar_menu, menu);
menu.getItem(2).setVisible(false);
menu.getItem(3).setVisible(false);
return true;
}

you should try to change the navigation mode of action bar when you create the activity...
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);

Related

Android SearchView not removed on menu clear

I am using app compat activity. I have a fragment where I added a search view in the MenuItemCompat. Now when I am opening another Fragment within the same container I can still see the Search View when clicking on the action bar Menu item. I have added the following
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_main, menu);
// MenuItemCompat.setActionView(menu.getItem(1), null);
//SearchView searchView =View.findViewById(R.id.searchViewId);
// MenuItemCompat.collapseActionView(menu.getItem(0));
// MenuItem item = menu.findItem(R.id.action_add_recipient);
//
// item.setVisible(false);
super.onCreateOptionsMenu(menu, inflater);
}
in the new fragment but still when clicking on the action bar item the search view is displayed. Any help please.

Menu return null in android

Hi this is my first question. i am learning android. here i am trying to setup menu icon top menubar.
I have added sets of item in menu. i want to manage icon from activity.
I am trying to show hide menu icon.
Menu return null in onCreate.
Is there any other way to manage menu icon dynamically ?
please help.
This is the activity class code snippet where i am trying to manage menu.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMenu = (Menu) findViewById(R.id.menuBar);//here Menu return null
mMenuItem = mMenu.getItem(2);
mMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
mMenuItem.setVisible(true);
}
will appreciate your help. thanks.
First inflate menu to get MenuItem in activity method onCreateOptionsMenu and then try to get menu.getItemlike this :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_activity, menu);
MenuItem item=menu.getItem(2);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
item.setVisible(true);
return true;
}
Dont put it on your onCreate because your menu is initialized in onCreateOptionsMenu
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.your_menu, menu);
mMenuItem = menu.getItem(2);
mMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
mMenuItem.setVisible(true);
return true;
}
Just follow the bellow instruction:
Step 1:
You need to inflate menu item from onCreateOptionsMenu(Menu menu)
Step 2:
You need a MenuInflater object that you can get using getMenuInflater()
API. Like: MenuInflater inflater = getMenuInflater();
Step 3:
Inflate you menu xml file like: inflater.inflate(R.menu.menu_bottom_nav,
menu);
Step 4:
You have to get menu object for specific item like : MenuItem menuItem =
menu.getItem(index). Here Index is the number depending on which menu item's
object you want to get.
Full code example:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_bottom_nav, menu);
MenuItem menuItem = menu.getItem(0);
return true;
}

Remove Action Button From Action Bar

I have implemented ActionBar Navigation using Fragment. In my App i have one Activity and rest is in Fragments. In my MainActivity i am implementing menu like this.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Two Fragments uses Navigation Drawer and in their respected fragments i am inflating menu buttons to sort items.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.sort_button_shops, menu);
}
Now the Problem is if i open the Fragment 1 it works perfectly. When i open fragment 2, it shows 2 button to sort, one from Fragment 1 and the second one from Fragment 2.
I have tried to hide the button but it didn't worked.
Any Help will be Appreciated.
Thanks
When you inflate a new menu you are adding new items to the old Menu object, which is probably not what you intended.
Try this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.removeItem(R.id.your_menu_item);
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Try using this in onResume() of fragments.
MenuItem item = (MenuItem) findViewById(R.menu.activity_main);
item.setVisible(false);
this.invalidateOptionsMenu();

How can I add a menu item to the action bar with actionbarsherlock?

I have started my android application with the standard holo theme. Then I added the tabs functionality and now I would like to add a small settings button in the action bar.
How can I do that. I already set this method:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.activity_main, (com.actionbarsherlock.view.Menu) menu);
return super.onCreateOptionsMenu(menu);
}
Try this:
menu.add("some title").setIcon(R.drawable.[your image]).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

Menu Ordering differences due to Fragment and Activity hierarchy. (onCreateOptionsMenu)

Background
Developing for Android 3.0, I have a HostActivity that is the superclass of NotebooksActivity and NoteActivity. NotebooksActivity includes a fragment, NotebooksFragment.
In HostActivity, I include a menu that I want to appear at the rightmost end of the options menu in the ActionBar, i.e. all menu items in subclasses of HostActivity should appear to the left of the menu items added in HostActivity.
Menu inflation in HostActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
final MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.host_menu, menu);
return super.onCreateOptionsMenu(menu);
}
The Problem
When I add menu items in NoteActivity, I achieve the desired order as expected:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.notebook_menu, menu);
return super.onCreateOptionsMenu(menu);
}
However, when I add menu items in NotebooksFragment, because of how Fragments work, onCreateOptionsMenu is called after the same method in HostActivity, resulting in HostActivity's menu items appearing before NotebooksFragment's.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.notebooks_menu, menu);
SearchView sv = new SearchView(getActivity());
sv.setOnQueryTextListener(this);
menu.findItem(R.id.search_notebooks).setActionView(sv);
super.onCreateOptionsMenu(menu, inflater);
}
How can I achieve my desired menu ordering?
You can try using android:menuCategory and android:orderInCategory to more manually specify your ordering.
Or, use onPrepareOptionsMenu() in HostActivity, as that is called after onCreateOptionsMenu(). Downside is that it is called on every MENU button press, not just the first time.

Categories

Resources