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);
Related
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);
}
hi frnds am creating an application which is a tab application.
in my Home which extends sherlockFragmentActivity, i am inflating menu.xml and includes code for on optionMenuitem click listener. The Fragmentactivity contains tabhost and on each tab it load fragments.
this is my menu.xml
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/setting_selector"
android:title=""
>
<menu >
<item
android:id="#+id/Profile"
android:showAsAction="ifRoom"
android:title="Profile"/>
<item
android:id="#+id/chngDoctor"
android:showAsAction="ifRoom"
android:title="Change doctor"
android:visible="false"/>
<item
android:id="#+id/changePword"
android:showAsAction="ifRoom"
android:title="Change password"/>
<item
android:id="#+id/logout"
android:showAsAction="ifRoom"
android:title="Logout"/>
</menu>
</item>
and this is my onCreateOptionMenu and onOptionItemSelected methods in class Home
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getSupportMenuInflater().inflate(R.menu.main, menu);
SubMenu subMenu = (SubMenu) menu.getItem(0).getSubMenu();
if(userType.equals("admin"))
subMenu.getItem(1).setVisible(true);
else
subMenu.getItem(1).setVisible(false);
return true;
}
and this is my onOptionItemSelected method
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) {
case R.id.Profile:
break;
case R.id.changePword :
break;
case R.id.chngDoctor :
break;
case R.id.logout:
Home.this.finish();
break;
}
return true;
}
i need to add some menus depending on tab change. that is on tab change i load different fragments and when fragment changes i need to add new items to the menu. my ListFrag which extends SherlockFragment and it will load when i click on the 3 rd tab. when this fragment load i need to add 1 menu item to the menu
Try the following way.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, 0, 0, "Option1").setShortcut('3', 'c');
menu.add(0, 1, 0, "Option2").setShortcut('3', 'c');
menu.add(0, 2, 0, "Option3").setShortcut('4', 's');
SubMenu sMenu = menu.addSubMenu(0, 3, 0, "SubMenu"); //If you want to add submenu
sMenu.add(0, 4, 0, "SubOption1").setShortcut('5', 'z');
sMenu.add(0, 5, 0, "SubOption2").setShortcut('5', 'z');
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
// code for option1
return true;
case 1:
// code for option2
return true;
case 2:
// code for option3
return true;
case 4:
// code for subOption1
return true;
case 5:
// code for subOption2
return true;
}
return super.onOptionsItemSelected(item);
}
This may help you.
In the menu.xml you should add all the menu items. Then you can hide items that you don't want to see in the initial loading.
<item
android:id="#+id/action_newItem"
android:icon="#drawable/action_newItem"
android:showAsAction="never"
android:visible="false"
android:title="#string/action_newItem"/>
Add setHasOptionsMenu(true) in the onCreate() method to invoke the menu items in your Fragment class.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
You don't need to override onCreateOptionsMenu in your Fragment class again. Menu items can be changed (Add/remoev) by overriding onPrepareOptionsMenumethod available in Fragment.
#Override
public void onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.action_newItem).setVisible(true);
super.onPrepareOptionsMenu(menu);
}
here is an example...it might help you...
private static final int MENU_ADD = Menu.FIRST;
private static final int MENU_LIST = MENU.FIRST + 1;
private static final int MENU_REFRESH = MENU.FIRST + 2;
private static final int MENU_LOGIN = MENU.FIRST + 3;
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
if(enableAdd)
menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon);
if(enableList)
menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon);
if(enableRefresh)
menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon);
if(enableLogin)
menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case MENU_ADD: doAddStuff(); break;
case MENU_LIST: doListStuff(); break;
case MENU_REFRESH: doRefreshStuff(); break;
case MENU_LOGIN: doLoginStuff(); break;
}
return false;
based on Gunaseelan answer
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
menu.removeGroup(1);
if (iotsnames.isEmpty()) return true;
for (int i=0; i<iotsnames.size(); i++ ){
menu.add(1, i, 0, iotsnames.get(i) );
}
return true;
}
use onPrepareOptionsMenu method and clear all menu first using
menu.clear();
then add menu.
Also refer here.. check its onPrepareOptionsMenu method
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;
}
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
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