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);
}
}
Related
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 am working on Sherlock ActionBar and using an Option button at right corner, near search icon. I am successful to generate the onClick event for SEARCH button but I am getting problems in onClick event of the option Button.
Using the below code to add the buttons :
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
menu.add(0, 1, 1, "Search").setIcon(R.drawable.search_icon).setActionView(R.layout.action_search).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
menu.add(0, 2, 2, "Button").setIcon(R.drawable.menu_overflow_icon).setActionView(R.layout.action_button).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return super.onCreateOptionsMenu(menu);
}
And for click events :
#Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
switch (item.getItemId()) {
case 1:
search = (EditText) item.getActionView();
search.addTextChangedListener(filterTextWatcher);
search.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
break;
case 2:
Toast.makeText(ActivityScreen.this, "hit it", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
I have shown my requirement in the image attached below.
Replace your break; statements in the Switch of onOptionsItemSelected to return true;
Addition to the answer
final static int BUTTON_ID = 2;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.add(Menu.NONE, BUTTON_ID, Menu.NONE, R.string.action_option_name);
item.setIcon(R.drawable.action_option);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case BUTTON_ID:
Toast.makeText(this, "clicked on 2", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
Note: I haven't added MenuItem.setActionView(R.layout.action_search). I tried to use the solution at https://stackoverflow.com/a/11292843/1567588. But its not working. And i have set GroupId and OrderId to Menu.None
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 want to refresh the option menu each time it is called,
I have a functionality in which when the user clicks the option(add/remove favourite) in option menu, it checks whether it is favourite or not and do the functionality,
Problem : Once it creates the menu , it does not refresh the onCreateOptionMenu on 2nd time user presses the option button. I want to refresh the optionMenu each time it is pressed. Here is my code for this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (is_favorite.equals("1")) {
menu.add(1, 22 ,0,"Remove from Favourites").setIcon(R.drawable.favorites_unselected);
}
else{
menu.add(1, 11 ,0,"Add to Favourites").setIcon(R.drawable.favorites_selected);
}
return true;
}
#Override
public boolean onPrepareOptionsMenu (Menu menu)
{
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case 11:
//addtofavouritestask
is_favorite = "1";
return true;
case 22:
//removeFromFavouritestask
is_favorite = "0";
return true;
default:
return super.onOptionsItemSelected(item);
}
}
According to the doc, onCreateOptionMenu (Menu menu) is called only once time, so it does not refresh your menu. You must call the method onPrepareOptionsMenu(Menu) to refresh it (" To update the menu every time it is displayed, see onPrepareOptionsMenu(Menu)").
So this should work :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (is_favorite.equals("1")) {
menu.add(1, 22 ,0,"Remove from Favourites").setIcon(R.drawable.favorites_unselected);
}
else{
menu.add(1, 11 ,0,"Add to Favourites").setIcon(R.drawable.favorites_selected);
}
return true;
}
#Override
public boolean onPrepareOptionsMenu (Menu menu)
{
menu.removeGroup(1);
if (is_favorite.equals("1")) {
menu.add(1, 22 ,0,"Remove from Favourites").setIcon(R.drawable.favorites_unselected);
}
else{
menu.add(1, 11 ,0,"Add to Favourites").setIcon(R.drawable.favorites_selected);
}
return super.onPrepareOptionsMenu(menu);
}
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);