It's my understanding that in a normal FragmentActivity / ViewPager / FragmentStatePagerAdapter situation onCreateOptionsMenu should be handled by the ListFragment...
...but what about in a scenario where ActionBarSherlock is extending the FragmentActivity and ListFragment classes?
I'm having a hard time getting onCreateOptionsMenu to work in the SherlockListFragment:
The ABS version of onCreateOptionsMenu is boolean, not void.
The ABS version of onCreateOptionsMenu omits MenuInflater as a parameter
Instantiating MenuInflater in SherlockListFragment.onCreateOptionsMenu is a problem ("Inflater cannot be resolved to a type").
I would be grateful if anyone could show me what I'm doing wrong...
Thanks!
You should definitely be able to override void onCreateOptionsMenu(Menu menu, MenuInflater inflater) in SherlockListFragment. I've had issues in the past where I imported the wrong MenuInflater. Could that be what's going wrong? You want to import com.actionbarsherlock.view.MenuInflater instead of android.view.MenuInflater.
Also, be sure to use getSupportMenuInflater over getMenuInflater when using ABS.
Related
I want replace current menu of action bar with another when change action bar.
To achieve this I wrote:
getMenuInflater().inflate(R.menu.menu2, menu_bar);
But it add new menu at the rest of current menu.
What i want is remove current and add the new one.
How can I do that?
You can call invalidateOptionsMenu(), which will cause the system to call onPrepareOptionsMenu(Menu menu) again. Inside this method, just check which one of your two menus you should inflate, after calling menu.clear().
Note that invalidateOptionsMenu() has been introduced in API 11 (Honeycomb). If you need to target a lower API level, you can store your Menu object from onPrepareOptionsMenu(Menu menu) as an attribute in your Activity, and directly call clear() on it, then inflate the needed menu as you are already doing.
If you're doing it when changing fragments then:
(1) Create a menu file for fragment
(2) onCreate() method of Fragment set setHasOptionsMenu(true);
(3) Override onCreateOptionsMenu, where you'll inflate the fragment's menu and attach it to your standard menu.
#Override
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu2, menu);
}
(4) Override onOptionItemSelected in your fragment for item handlers.
Check the link below for more details..
the answer below is correct just has a extra point that will said
You can call invalidateOptionsMenu(), which will cause the system to call onPrepareOptionsMenu(Menu menu) again. Inside this method, just check which one of your two menus you should inflate, after calling menu.clear().
Note that invalidateOptionsMenu() has been introduced in API 11 (Honeycomb). If you need to target a lower API level, you can store your Menu object from onPrepareOptionsMenu(Menu menu) as an attribute in your Activity, and directly call clear() on it, then inflate the needed menu as you are already doing.
point : at onPrepareOptionsMenu(Menu menu) should not call older menu item that lead to Error
public boolean onPrepareOptionsMenu(Menu menu) {
// Error -> menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
menu.clear();
getMenuInflater().inflate(current_menu, menu);
return super.onPrepareOptionsMenu(menu);
}
I'm having type issues when attempting to inflate a ContextMenu while making use of the ActionBarSherlock library, which as you know ultimately mimics/implements the Android support library.
There is a SherlockFragmentActivity which sets the layout content and within that content exists two fragments. One of those fragments is a SherlockListFragment. Within the onCreate of the SherlockListFragment I make a call to register for the ContextMenu.
registerForContextMenu(getListView());
The problem stems when attempting to inflate the menu.
listView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
#Override
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
MenuInflater inflater = getSherlockActivity().getSupportMenuInflater();
inflater.inflate(R.menu.lot_menu, contextMenu);
}
});
I am unable to call inflate due to the type specified within the method parameters, which it expects as com.actionbarsherlock.view.Menu however the passed in type is android.view.ContextMenu.
I seem to be missing something as inflating a Menu within the action bar was trivial however the ContextMenu seems to be posing issues when making use of the support framework.
How do I register appropriately to make use of the support framework as needed and subsequently inflate the ContextMenu?
Try using getMenuInflater() instead of getSupportMenuInflater() for inflating into a ContextMenu.
I followed the following link to create a test application to create menu:
http://developer.android.com/guide/topics/ui/menus.html
It worked for me. However, when I added it to my app which is a google map app, menu is not working. I suspect its because the in the sample I created I extended Activity class but in my app I have extended MapActivity class.
Can someone explain how can i do it?
Menu is working same for MapActivity as for Activity class. You just need to override onCreateOptionsMenu method. Doublecheck your resource name R.menu.mapmenu etc.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mapmenu, menu);
return true;
}
I create an app that supports both phone and tablet version so i use the android-support-v4.jar library. My activity extends the FragmentActivity and override the onCreateOptionsMenu(Menu menu). This works fine on tablet, the onCreateOptionsMenu being called correctly but it doesn't work on phone, onCreateOptionsMenu never get called. How to resolve this?
Note: i use <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="12"/> on Manifest file.
You should consider from your Fragment code:
1) Implementing onCreateOptionsMenu(Menu menu, MenuInflater inflater)
2) Calling setHasOptionsMenu
3) And also implementing onOptionsItemSelected(MenuItem item)
Then you will be ok on both the phone and tablet.
I've got an app using MapActivity.onCreate() to initialize the map and show it on screen. Now I would like to add a menu to my app. From what I've found out I can't add a menu from MapActivity and need to use Activity (correct me if I'm wrong).
Now I have no idea how to "initialize" the map from my Activity-class.
And how would I have to fix the views, will I wrap my activity-layout around my Map-layout?
MapActivity extends a regular Android Activity, so there's nothing irregular you should need to do to create a menu.
Just override the onCreateOptionsMenu method, as shown in the developers' guide.
MapActivity extends Activity, so you should be able to add a menu.
MapActivity is a subclass of Activity, and thus you do it the same way as in any normal Activity (instructions here). I've been able to successfully create menus the same way in MapActivity as in a normal Activity.
Make sure that it doesn't extend from FragmentActivity but from AppCompatActivity!
If that's the case, the onCreateOptionsMenu method will be called and you are able to overwrite it like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu); //"menu_main" is the XML-File in res
return super.onCreateOptionsMenu(menu);
}