How to get all menu items id Android - android

I can get all the items from the actionbar with getItem() but I need to compare their id's with a particular id. How can I do this?

You can get actionBar menu items id by calling pMenu.getItem();
You can use changing methods of these menu items by overriding the method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action buttons
switch(item.getItemId()) {
case R.id.action_stats:
break;
}
}

Related

LayoutInflater Onclick Not working

I have 2 xml one main xml and one menu xml. I want to get the click event from menu xml to main xml activity. is there is any way to solve this problem.
Main Home Page
Menu Page
Now click on the menu page button event i want in my main home activity page.
I done Like this
View otherLayout = LayoutInflater.from(this).inflate(R.layout.menu_layout,null);
Button tstclick = (Button) otherLayout.findViewById(R.id.textclick);
tstclick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Your thing
System.exit(0);
}
});
The Activity class provides us two methods to inflate/show menu items and work with them or assign some tasks to them. They are:
onCreateOptionsMenu(Menu menu)
onOptionsItemSelected(MenuItem item)
The onCreateOptionsMenu() method is responsible for creating and inflating the menu by help of MenuInflater class.
The onOptionsItemSelected() method is responsible for assigning tasks to each menu item. Each menu item is identified by help of its unique ID.
In order to show and work with menu's in any Activity, both the methods needs to be overridden as shown below:
public class MainActivity extends AppCompatActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
// All your menu items will come here. Each menu item ID will become a separate case in the Switch case
default:
return super.onOptionsItemSelected(menuItem);
}
}
}
As soon as the user clicks or taps on any menu item, the onOptionsItemSelected() method is called by the Android OS and then the ID for that particular menu item will be matched in the method. The group of statements specified in the respective case will then be executed.
For more info visit the following links:
http://developer.android.com/guide/topics/ui/menus.html
http://developer.android.com/reference/android/view/MenuInflater.html

Hide/show MenuItem on click actionbar android

I've been trying to get my actionbar buttons to show on click but can't get it to work. I got 2 buttons and if i click on one i want the other to show and the other to get invinsible.
Here is my code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
MenuItem brandsMenu = (MenuItem)findViewById(R.id.action_brands);
MenuItem categoryMenu = (MenuItem)findViewById(R.id.action_category);
switch (item.getItemId()) {
case R.id.action_category:
brandsMenu.setVisible(true);
return true;
case R.id.action_brands:
categoryMenu.setVisible(true);
}
This only shows errors. Any Suggestions?
You need to call InvalidateOptionsMenu when you want to make changes to your menu.
You then use the onCreateOptionsMenu override to apply those changes.
Define MenuItems named brandsMenu and categoryMenu and initialize them onPrepareOptionsMenu
categoryMenu = menu.findItem(R.id.action_category);
brandsMenu = menu.findItem(R.id.action_brands);
You should be able to change visibility such as categoryMenu.setVisible(true);
You can't set visibility on menuitems.
You should invalidate the options menu and add only the menuItems you want to be visible

Hide item in Action Bar Sherlock

I want to hide an item in Action Bar Sherlock. I try it:
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
switch (item.getItemId()) {
case 2131165381:
item.setVisible(false);
supportInvalidateOptionsMenu();
CopyOfAsyncLoadTasks.run(this);
item.setVisible(true);
break;
What I am doing wrong?
I suggest you to use rather resource id of <item> instead of your "ambicious" number.
switch (item.getItemId()) {
case R.id.myItem:
menu.findItem(R.id.myItem).setVisible(false);
break;
...
}
From first look at your code i don't know exactly what number 2131165381 is? If you'll work in team it'll be not very human-readable especially for another person.
Note: To get more control over menu i recommend you to create second menu variable e.q:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
...
}

How to distinguish two menu item clicks in ActionBarSherlock?

I have been working with ActionBarSherlock recently, and follwing various tutorials, I wrote this code to add items to Action bar
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Refresh")
.setIcon(R.drawable.ic_action_refresh)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add("Search")// Search
.setIcon(R.drawable.ic_action_search)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
However, I dont know how to distinguish the two clicks.
Although I did find out that you have to Override onOptionsItemSelected to handle the clicks and also that a switch statement can be used to distinguish between clicks, but most tutorials use item ids from thier xml menus. Since I am not creating menus in xml how can i distinguish the clicks without ids.
private static final int REFRESH = 1;
private static final int SEARCH = 2;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, REFRESH, 0, "Refresh")
.setIcon(R.drawable.ic_action_refresh)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(0, SEARCH, 0, "Search")
.setIcon(R.drawable.ic_action_search)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case REFRESH:
// Do refresh
return true;
case SEARCH:
// Do search
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Just check following
http://developer.android.com/guide/topics/ui/actionbar.html
which contains
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { <--- here you can get it
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
you can do ti by there Id in onOptionsItemSelected ................which can be set here also
http://thedevelopersinfo.wordpress.com/2009/10/29/handling-options-menu-item-selections-in-android/
http://developer.android.com/reference/android/view/Menu.html#add(int, int, int, java.lang.CharSequence)
use
public abstract MenuItem add (int groupId, int itemId, int order, CharSequence title)
Since: API Level 1
Add a new item to the menu. This item displays the given title for its label.
Parameters
groupId The group identifier that this item should be part of. This can be used to define groups of items for batch state changes. Normally use NONE if an item should not be in a group.
itemId Unique item ID. Use NONE if you do not need a unique ID.
order The order for the item. Use NONE if you do not care about the order. See getOrder().
title The text to display for the item.
Returns
The newly added menu item.

Checking for which submenu user clicked in android

I have created a submenu using the manifest file. I have four text fields in my submenu. I want to know which submenu item the user clicked. I know about the switch case item.getItemId condition for the menu items. But i want to know how to check for which submenu the user clicked.
Thanks for any help
You could save the id of the submenu item to a variable. For example:
int item;
#Override
public void onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.something: {
this.item = something;
return true;
}
}
Now the variable item contains the id of the item selected.

Categories

Resources