android menu items do not display - android

there is an activity in my android application. I override the 'onCreateOptionsMenu' method, adding four menu items in the activity. But the menu items do not display. I can not figure out what is the problem. Could somebody give me an clue to fix that or explaination?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, FeaturedActivity.MENU_FEATURED, 0, R.string.menu_featured).setIcon(R.drawable.icon_tabbar_featured);
menu.add(0, FeaturedActivity.MENU_THE_DRINK, 1, R.string.menu_the_drink).setIcon(R.drawable.icon_tabbar_drinks);
menu.add(0, FeaturedActivity.MENU_PLAYER, 2, R.string.menu_player).setIcon(R.drawable.icon_tabbar_player);
menu.add(0, FeaturedActivity.MENU_SHARE, 3, R.string.menu_share).setIcon(R.drawable.icon_tabbar_share);
return true;
}

in your activity use
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.optionsmenu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.info:
startActivity(new Intent(this, AboutApp.class));
return true;
case R.id.exit:
finish();
return true;
}
return false;
}
and create a folder menu in res and now create an xml in res/menu like optionsmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/info" android:title="Info"
android:icon="#drawable/info_menubtn" />
<item android:id="#+id/exit" android:title="Exit" />
</menu>
Hope this will work for you

remove the line super.onCreateOptionsMenu(menu); and try .

#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add("this is menu");
menu.add("this is another");
return super.onCreateOptionsMenu(menu);
}
edit into above code and add return super.onCreateOptionsMenu(menu); at last and remove it from first line

Related

Where to add menu to work in all activities

I have a menu.xml and the code to show the menu in action bar. Do i have to add the code in every activity? Cause after this i use a switch case to get click, and more code, so i dont thing its the wright way to copy-paste the same code in every activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
Is there a better way to do it as to work in all app (all activities)?
Create a BaseActivity class.
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
}
Now all activities that extend your base activity will have the same menu.
this is a guide for Android Menu
this is an example menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game" />
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
And then inflating it within your activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
whe item clicked:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
good luck and read the doc official for android

MenuInflater not working - menu not showing

My menu is not showing in the ActionBar (onCreateOptionsMenu) is called properly. The icon and string are available. The code works fine in my other projects. I am using android.support.v7.app.ActionBarActivity for android:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
Code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
Log.i("onCreate", "menu");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.add, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add:
Intent addIntent = new Intent(this, RoomAddActivity.class);
startActivity(addIntent);
break;
}
return super.onOptionsItemSelected(item);
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_add"
android:icon="#drawable/ic_action_new"
android:showAsAction="always"
android:title="#string/action_add"/>
</menu>
Try to return true explicitly:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
Log.i("onCreate", "menu");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.add, menu);
super.onCreateOptionsMenu(menu);
return true;
}
Updated:
By the way, you are not obliged to call super method. As to my experience it works all right without it.

Menu setActionView(view) to xml

In onCreateOptionsMenu I'm setting an action view with:
MenuItem item = ..
item.setActionView(action_view)
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
and it works ok. But I want to move this to my xml menu and I can't seem to make it show. It only shows up the title. I tried many versions, like:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourApp="http://schemas.android.com/apk/lib/myPackage.myClass" >
<item
android:id="#+id/item"
yourApp:actionViewClass="action_view"
android:showAsAction="always" or youtApp:showAsAction="always"
android:title="title"
</item>
The problem must be it getting to the action_view variable, but I can't seem to figure it out
Did you inflate the menubar?
#Override
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_name, menu);
mMenu = menu;
if(mMenu != null){
MenuItem item = mMenu.findItem(R.id.item_id);
if(item != null){
item.setVisible(false);
}
}
super.onCreateOptionsMenu(menu, inflater);
}
Also do you have setHasOptionsMenu(true); #onCreate() function?
Try this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item:
// Write your action_view here
break;
case R.id.your_other_item_id:
//Your other Menu item logic
break;
}
return super.onOptionsItemSelected(item);
}

How to track the Share button clicked on the ActionBar?

I have added Share option to my ActionBarSherlock, this way:
public boolean onCreateOptionsMenu(final Menu menu) {
menu.add("Share")
.setIcon(R.drawable.ic_title_share_default)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
}
and on clicking this Icon i want to do something. how can i track the click on this ShareIcon??
You should create an XML file to define menu items.
For example mymenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/share"
android:icon="#drawable/ic_title_share_default"
android:title="#string/share"
android:showAsAction="ifRoom"/>
</menu>
Then in the onCreateOptionsMenu you will do:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}
To handle the item selection:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.share:
//do something for share
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You can see more information in here:
http://developer.android.com/guide/topics/ui/menus.html
http://actionbarsherlock.com/usage.html

Android ActionBarShelock onOptionsItemSelected

I using action bar by sherlock. I'm trying to implement it into my app. But seems like I am missing something to make it to work. Please check on my codes. My app doesnt do anything when i tap on the action buttons. Below are my codes and my xml.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
menu.add("Share")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add("Save")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add("Set")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.share:
new share(this).execute(image_url);
return true;
case R.id.save:
new save(this).execute(image_url);
return true;
case R.id.set:
new set(this).execute(image_url);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
My menu xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/share"
android:title="#string/share"/>
<item
android:id="#+id/save"
android:title="#string/save"/>
<item
android:id="#+id/set"
android:title="#string/set"/>
</menu>
You can set OnMenuItemClickListener on your menu items like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Share")
.setOnMenuItemClickListener(this.mShareButtonClickListener)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
// Other items...
return super.onCreateOptionsMenu(menu);
}
Then you create your OnMenuItemClickListener:
OnMenuItemClickListener mShareButtonClickListener = new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// Example of action following your code
new share(YouActivity.this).execute(YouActivity.this.image_url);
return false;
}
};
You are inflating menu from menu's xml and also add in onCreateOptionsMenu, either of this should be done not both
menu.add(Menu.NONE, PREF_MENU_ITEM, Menu.NONE, R.string.channel_preferences_menu_label).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
menu.add(Menu.NONE, FEEDBACK_MENU_ITEM, Menu.NONE, R.string.feedback_from_menu_label).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
menu.add(Menu.NONE, ABOUT_MENU_ITEM, Menu.NONE, R.string.about_app_menu_label).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
// commenting out this line because this func dosent have any use case
// for APP version 3.0.0
// menu.add(Menu.NONE, SOCIAL_MENU_ITEM, Menu.NONE, R.string.social_app_menu_label).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
menu.add(Menu.NONE, FAQ_MENU_ITEM, Menu.NONE, R.string.faq_app_menu_label).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
menu.add(Menu.NONE, DIAGNOSIS_MENU_ITEM, Menu.NONE, R.string.diagnosis_app_menu_label).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
menu.add(Menu.NONE, MY_ACCOUNT_MENU_ITEM, Menu.NONE, R.string.account_app_menu_label).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);

Categories

Resources