OptionsMenu doesn`t appear - android

I have ListFragment and i want manage ActionBar. However, it does not appear on the screen.
Here is code for ListFragment:
#Override
public void onCreate(Bundle saved) {
super.onCreate(saved);
setHasOptionsMenu(true);
...
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_crime_list, menu);
}
fragment_crime_list:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_item_new_crime"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/new_crime"
android:showAsAction="ifRoom|withText" />
</menu>
In my manifest.xml i have set only minSdkVersion but it doesnt help
<uses-sdk
android:minSdkVersion="8"
/>
Any suggestions?

The action bar is not added by default in your activity and it's also added in API level 11 not 8. So to add it, you have two options that I'll address here
On API level 11 or higher
The action bar is included in all activities that use the Theme.Holo theme (or one of its descendants), which is the default theme when either the targetSdkVersion or minSdkVersion attribute is set to "11" or higher. If you don't want the action bar for an activity, set the activity theme to Theme.Holo.NoActionBar.
API level 7 or higher
You have to use the appcompat v7 library that you can find exactly how to add it here.

For fragments you need to setsetHasOptionsMenu(true); in your oncreatView()

just call super constructor of onCreateOptionMenu it will work
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_sample, menu);
super.onCreateOptionsMenu(menu,inflater);
}

Related

Do I need an empty onCreateOptionsMenu for empty menus?

In my app I have some activities without menu items, that use the following override:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.appbar_menu_empty, menu);
return true;
}
This works good. If I remove the override, I get the same effect on Android 5.1, i.e. an action bar with no icons.
So the question is: can I drop the override?
The documentation of Activity.onCreateOptionsMenu states:
The default implementation populates the menu with standard system menu items.
What does that mean? Do I need to expect that Android comes up with some buttons I did not explicitly add?
You can remove OncreateOptionsMenu() if you dont want to have menu items.
If you want to add menu items, edit the menu.xml file in resources/menu directory.
From the docs the method is defined in Activity class as below
Initialize the contents of the Activity's standard options menu. You
should place your menu items in to menu.
This is only called once, the first time the options menu is
displayed. To update the menu every time it is displayed, see
onPrepareOptionsMenu(android.view.Menu).
The default implementation populates the menu with standard system
menu items. These are placed in the android.view.Menu.CATEGORY_SYSTEM
group so that they will be correctly ordered with application-defined
menu items. Deriving classes should always call through to the base
implementation.
You can safely hold on to menu (and any items created from it), making
modifications to it as desired, until the next time
onCreateOptionsMenu() is called.
When you add items to the menu, you can implement the Activity's
onOptionsItemSelected(android.view.MenuItem) method to handle them
there.
Parameters:
menu The options menu in which you place your items. Returns:
You must return true for the menu to be displayed; if you return false it will not be shown.
public boolean onCreateOptionsMenu(Menu menu) {
if (mParent != null) {
return mParent.onCreateOptionsMenu(menu);
}
return true;
}
also check this SO thread out onCreateOptionsMenu() calling super
check the code for Activity class here http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/app/Activity.java#Activity.onCreateOptionsMenu%28android.view.Menu%29
see some sample code here where you need to show option in action bar menu
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_act_add_recipe, menu);
super.onCreateOptionsMenu(menu, inflater);
}
/res/menu/menu_act_add_recipe.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_add_image"
android:icon="#drawable/ic_tab_add_image_white"
android:orderInCategory="100"
android:title="#string/action_preview"
app:showAsAction="always" />
<item
android:id="#+id/action_recipe_preview"
android:icon="#drawable/ic_tab_check_white"
android:orderInCategory="100"
android:title="#string/action_preview"
app:showAsAction="always" />

How do I use the Action Bar in Android 2.1

Various websites about this topic explained. But I did not. Please explain step by step use action bar in android 2.1.thanks
You need to use AppCompat from the support library.Download the Support library and import AppCompat to workspace. Its a Library project and you must reference the same in your Android project.
https://developer.android.com/tools/support-library/setup.html
Check Adding libraries with resources in the above link
Your Activity needs to ActionBarActivity. Need to use Theme.AppCompat
Check Adding ActionBar #
http://developer.android.com/guide/topics/ui/actionbar.html
or You can use ActionBarSherlock.
http://actionbarsherlock.com/
in res/menu you will add xml file
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_refresh"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/ic_action_refresh"
android:title="Refresh"/>
<item
android:id="#+id/action_settings"
android:title="Settings">
</item>
and then in Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.action_refresh){
}
return true;
}

Legacy menu is not showing in android

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.

Menu button is not show in 3.0 OS

I have created menu button which have two functions bookmark and home button.
This is working well in all the android version without android 3.0
do here any ways ? so my menu button will be display also in android 3.0 with all the version.
my code:-
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_bookmark:
db.updateContact(new Contact(itemN,imageStatus));
return true;
case R.id.home_page:
Intent i = new Intent(imageTouchs.this, Comics.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
and my androidmanifest.xml :-
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="15" />
menu.xml :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_bookmark"
android:title="Bookmark"
android:showAsAction="ifRoom|withText" />
<item android:id="#+id/home_page"
android:title="Home"
android:showAsAction="ifRoom|withText" />
</menu>
any menu is not display in action bar
The "overflow' button will only show up when there isn't a physical menu button on the device. This is the standard behavior for the ActionBar.
You can actually set the value of your AVD to specifically not have a menu button when you are configuring the image.
Here is a SO question addressing this same thing: Action bar overflow not displayed
If you are talking about the OverFlowMenu icon, to the best of my knowledge, it cannot be achieved (forced) using the standard Android Support Library.
If you must have an OverFlowMenu (forced), you will need to use the ActionBarSherlock library. Go through a couple of my answers where I have a few fairly detailed suggestions on how to achieve that:
https://stackoverflow.com/a/13307583/450534
https://stackoverflow.com/a/13180285/450534
NOTE: As already mentioned in both my answers linked above, this is not recommended to ensure users get a seamless UI on their devices. And also, if you must absolutely force the OverFlowMenu, you will need to use an older version of ABS, which is again, not recommended.

How to add my menu button in system bar in Android 3.1?

My pad is SAMSUNG GT-P7510.I want to add a new menu in the system bar.But the menu shows in action bar.Like this:
But now it is like this:
It's in the right of top.
public class TestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,0,1,"OK");
return true;
}
}
my guess is you started off with a pre-honeycomb app and ran it on 3.*+ emulator.
it happens automatically if you use the right layout style/theme.
on eclipse just go to your layout and choose "android 3.0" on the top right corner.
hope it helps.
EDIT:
after you edited your question I understand i got your question wrong.
If I understand correctly you are just trying to show the menu item as a button outside the menu list and that's simple -
on the xml use the "showAsAction" option like so-
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_share"
android:icon="#drawable/ic_menu_share"
android:title="#string/menu_share"
android:alphabeticShortcut='o'
android:showAsAction="ifRoom|withText" />
</menu>
inflating it with
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.list_options_menu, menu);
or by code:
MenuItem item = menu.add("OK");
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
Please remove targetSdkVersion from manifest.xml file..
like from this format:
make it like:
Thanks,
Ram

Categories

Resources