I had an inflater menu created in a fragment.
This is the code.
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
It works, but I want that if I click on a button the inflater is disable. It means that also if I click on the inflater it doesn't show the menu. When I click on another button the inflater return to show its options. How can I do? Thanks!
Related
I have a fragment extends android.support.v4.app.Fragment. And I need a checkbox in my menu, but I know I can't use getSupportActionBar(), so How another choice I have?
Thanks a lot.
in your onCreateView add this line:
setHasOptionsMenu(true);
then override the following :
#Override
public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.menu_frag, menu);
}
How can I add action (icon) on Toolbar (AppConpat v21+)? Like 3-dot icon on right side.
I can create this action icons via onCreateOptionsMenu( Menu menu, MenuInflater inflater):
#Override
public void onCreateOptionsMenu( Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu( menu, inflater);
inflater.inflate( R.menu.menu, menu);
menu.clear();
MenuItem menuItemReload = menu.add( getResources().getString( R.string.sReload));
menuItemReload.setNumericShortcut( '0')
.setIcon( ResourcesCompat.getDrawable( getResources(), android.R.drawable.stat_notify_sync, getActivity().getTheme()));
if( Build.VERSION.SDK_INT >= 11) menuItemReload.setShowAsAction( MenuItem.SHOW_AS_ACTION_ALWAYS);
}
I not found any information inside getSupportActionBar() about add actions.
Is there exist different method add icons to Toolbar?
Did you set the AppCompatToolBar correctly in your AppCompatActivities onCreate()?
Toolbar toolBar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolBar);
After initializing it this way onCreateOptionsMenue() should work as intended.
I need to change the title of a menu item in my action bar at startup based on a few variables which get created at the startup.
But for some reason I cant simply do that since the menu items take time to inflate maybe?
how do I get around this issue.
below is my attempt but it throws java.lang.IndexOutOfBoundsException
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.map_fragment_action_menu, menu);
mOptionsMenu = menu;
mOptionsMenu.getItem(R.id.map_fragment_action_layers_0).setTitle("my title");
}
P.S. I am using a fragment, I also tried to set the title in onCreateView() method but still doesn't work.
You need to use the method Menu#findItem() instead.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.map_fragment_action_menu, menu);
mOptionsMenu = menu;
mOptionsMenu.findItem(R.id.map_fragment_action_layers_0).setTitle("my title");
}
Menu#getItem() expects an index and not the menu item's id. For e.g, if this menu item is the first item in the menu, you would use
mOptionsMenu.getItem(0).setTitle("my title");
So I have a Search Button in my Action Bar, But I only want that button in the first Activity, but when I open the seconde activity, the Action button is still there. How to get rid of that button in the seconde activity without removing the action bar, because I nee it from my Navigation Drawer.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom" />
...
</menu>
Activity1:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
Activity2:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//I thought removing the INFLATER part would help, but it didn't...
return super.onCreateOptionsMenu(menu);
}
You can try hiding the search menu item in your second activity.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.action_search).setVisible(false);
return true;
}
You can refer to this question with multiple answers.
You can make the Item in your 2nd Activity invisible like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.action_search);
item.setVisible(false);
return super.onCreateOptionsMenu(menu);
}
Another approach would be to load a second menu.xml with no item in it.
if you return false it will not be shown. as android docment saying http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu%28android.view.Menu%29
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}
I created a simple menu item with the help of ActionBarSherlock:
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, ITEMD, 0, "item").setIcon(R.drawable.topbar_btn_inbox).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.breadcrumb_bar_menu, menu);
return true;
}
But I can't change its width and height. How can I do this in XML or programatically?
As #Tanis.7x stated and according to the Action Bar Icons Documentation size and densities for different devices are already predefined for consistency, so you cant change any. However you can define your custom Action Bar view and then behave like a view and change sizes, etc:
actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
actionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.custom_view);