MenuItem doesn't appear on actionbar - android

I can not make the menu items appear on the actionbar. They appears on the menu only.
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, Menu.FIRST, Menu.NONE, "Favorites").setIcon(
android.R.drawable.btn_star_big_on);
menu.add(Menu.NONE, Menu.FIRST, Menu.NONE, "Favorites").setIcon(
android.R.drawable.btn_star_big_on);
return true;
}

Related

Show menu options on Toolbar for Activity contains Tabs as fragment

I have an activity contains 3 tabs using fragment. I want to load/change the toolbar menu, when tab move.
public class SpendsFragment extends Fragment {
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
MenuItem menuInicio = menu.add(0, 0, 0, "InĂ­cio");
menuInicio.setIcon(android.R.drawable.ic_menu_edit);
MenuItem menuBusca = menu.add(1, 1, 0, "Buscar");
menuBusca.setIcon(android.R.drawable.ic_menu_search);
setHasOptionsMenu(true);
super.onCreateOptionsMenu(menu, inflater);
}
}
public class MoneyFragment extends Fragment {
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
MenuItem menuInicio = menu.add(0, 0, 0, "Edit");
menuInicio.setIcon(R.drawable.xyz);
MenuItem menuBusca = menu.add(1, 1, 0, "Delete");
menuBusca.setIcon(R.drawable.abc);
setHasOptionsMenu(true);
super.onCreateOptionsMenu(menu, inflater);
}
}

Error in using menu item to switch between activities in Android

I am trying to use a menu item to switch between activities in my application. Unfortunately, when I tap on the menu item, it does nothing.
Here is the activity.java code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// Called when the user selects a contextual menu item
#Override
public boolean onContextItemSelected(MenuItem item) {
//Handles Item Selection.
switch (item.getItemId()) {
case R.id.action_switch_natural:
Intent a = new Intent(this, Natural_Display.class);
startActivity(a);
return true;
default:
return super.onContextItemSelected(item);
}
}
Here is the code for main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_switch_natural"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_switch_natural"/>
</menu>
What am I doing wrong?
onContextItemSelected(MenuItem item) should be onOptionsItemSelected(MenuItem item), as seen here. Don't forget to change super.onContextItemSelected(item) to super.onOptionsItemSelected(item).
I think you have to override onCreatecontextMenu before onContextItemSelected.
try this.
#Override
public void onCreateContextMenu(ContextMenu menu,
View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}

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);

Adding an onclick to a submenu?

I have an onclick function for my menu but I can't figure out what the ID is for my submenu so that I can tell the submenu what to do when the user click on it. I created my submenu programmatically using the code below. So if someone could please explain to me how I know what the id is for each item of the submenu I'd greatly appreciate it.
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
SubMenu submenu = menu.addSubMenu(0, Menu.FIRST, Menu.NONE, "Preferences");
submenu.add(0, Menu.FIRST, Menu.NONE, "Get Last 5 Packets");
submenu.add(0, Menu.FIRST, Menu.NONE, "Get Last 10 Packets");
submenu.add(0, Menu.FIRST, Menu.NONE, "Get Last 20 Packets");
inflater.inflate(R.menu.mainmenu, submenu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId())
{
case R.id.viewKML:
viewKML();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
When you are adding
submenu.add(0, Menu.FIRST, Menu.NONE, "Get Last 5 Packets");
The parameter arrangement for add() method is Android Menu Add Method
public abstract MenuItem add (int groupId, int itemId, int order, CharSequence title)
itemId Unique item ID. Use NONE if you do not need a unique ID.
Is the id of your menu item. It should be unique. Like you say 15,20,21. This id will act like android:id="#+id/15". Is will be used when you are going to check which item is clicked
e.g
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_menu, menu);
SubMenu submenu = menu.addSubMenu(0, Menu.FIRST, Menu.NONE, "Preferences");
submenu.add(0, 10, Menu.NONE, "Get Last 5 Packets");
submenu.add(0, 15, Menu.NONE, "Get Last 10 Packets");
submenu.add(0, 20, Menu.NONE, "Get Last 20 Packets");
inflater.inflate(R.menu.main_activity_menu, submenu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case 10:
Toast.makeText(LoginPageActivity.this, "Now "+item.getItemId(), Toast.LENGTH_SHORT).show();
return true;
case 15:
Toast.makeText(LoginPageActivity.this, "Now = "+item.getItemId(), Toast.LENGTH_SHORT).show();
return true;
case 20:
Toast.makeText(LoginPageActivity.this, "Now == "+item.getItemId(), Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

android menu items do not display

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

Categories

Resources