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.
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 am developing an app with actionbarsherlock. Because i need to add compatibility for device under 3.0. In main(first) activity i need to hide actionbar (for design issue). Also that activity has an option menu. Question is how can i add legacy menu for recent version of device which doen't have any hardware menu button. I have checked all the options but not working. I am adding some options here
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.coredialer_menu, menu);
return true;
}
#manifest
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="10" />
I have solved my problem by removing actionbarsherlock.
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.
I have a TabActivity with four tabs. When I set android:targetSdkVersion="15" the onCreateOptionsMenu method is not called on any of the tab activities when testing on a Nexus 7.
It works correctly with android:targetSdkVersion="10".
With android:targetSdkVersion="15" it works correctly when the activities are not in a TabActivity and when tested on a phone (Evo).
Here is the code for onCreateOptionsMenu.
public boolean onCreateOptionsMenu(Menu menu) {
Log.i("Test","Base In create option menu");
if( menuId != null ) {
new MenuInflater(this).inflate(menuId,menu);
}
return super.onCreateOptionsMenu(menu);
}
Sorry but this is an easy one. According to Google:
http://developer.android.com/guide/topics/ui/menus.html
So, if you set the target SDK lower, you can show the deprecated options bar. For the newer SDKs you need to use an action bar or some other form of navigation. Basically Google decided that not all devices would have a "menu" button.
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;
}