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.
Related
I am developing an Android app and I am trying to put a menu item into the ActionBar.
It has enough space, so it shouldn't be on the overflow or anything.
In my menu.xml I have added that item + android:showAsAction="ifRoom|withText"
However, no matter how large the screen is, that damn menu will not show up on the ActionBar (although it is present in the menu, if the user presses a key).
Unfortunately I cannot post any full-code since I am under a non-disclosure agreement, but I will answer all questions.
The section where I inflate the menu:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.drinks, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.done:
//stuff
}
return super.onOptionsItemSelected(item);
}
Since you have only a menu item, use this attribute instead: android:showAsAction="always".
EDIT Above works all the time if you're running the code on post Honeycomb. But, in order to run on pre-Honeycomb, according to developer article, you need to extend from ActionBarActivity, that means adding compatibility support v4 & v7 and set the following theme for your activity:
<activity android:theme="#style/Theme.AppCompat.Light" ... >
... or a Them.AppCompat theme. Or use one of your own that extends from these.
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.
I just started using Juno, and I was wondering what this method is really about. And since I
dont use it, I dont want it to create every time i create a project. How can I disable her?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
All this does is create your menu that shows up when you press the menu key. It may also show up in the action bar depending on how your app is set up and the API version it is running on. It is not needed unless you have extra options that you want to show to the user in this manner.
This method is by default created from the ADT Activity templates.
I am new to work with android tablet api level 12. I have created 7 inches avd with 1024*600 screen resolution. I have implemented sample application for get the option menus on my screen and back button. I am unable to see option button and back button on my emulator.
I have implemented for option menu code as follows:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.icon: Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG).show();
break;
case R.id.text: Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show();
break;
case R.id.icontext: Toast.makeText(this, "You pressed the icon and text!", Toast.LENGTH_LONG).show();
break;
}
return true;
}
You can use keyboard shortcuts to simulate the hardware buttons. There's a table of the shortcuts here. The Back button is simulated by pressing escape, and the menu button is simulated by the Page-Up or F2 keys.
Ordinary options menus will appear regardless of Android version. If you have:
android:targetSdkVersion="11"
then the options menu will appear in the action bar on Android 3.0+ devices. All items in the options menu will be available when clicking the "overflow" button in the upper-right corner. Your options menu will appear normally on Android 1.x and 2.x devices, even if you have android:targetSdkVersion="11" in your manifest element.
If you overrode the MENU button to have other behavior, you will need to create some other trigger for that behavior for API Level 11 and higher. Overriding the MENU button was never a good idea to begin with, and as you can see, it is even less of a good idea now. I know of no way to get a MENU button on an Android 3.0 device if you have
android:targetSdkVersion="11".
As you said "I am unable to see option button and back button on my emulator"
SO its does not make a sense that you have posted your code because you can't changes the hardware as programmatically.
and
for menu u can press F2
for back u can press Esc
Short update: a current list of commands is found at https://developer.android.com/studio/run/emulator.html#tasks.
For the menu, you are supposed to use Ctrl-M (on a Mac: Command-M).
For windows 10 you have to open your emulator, then press Ctrl + M
and the menu will show.
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.