I need to add accessibility to the menu button (physical menu button or non) How can I add accessibility to this component?
Something in here?
public boolean onMenuOpened(int featureId, Menu menu) {
onCreateOptionsMenu(menu);
return super.onMenuOpened(featureId, menu);
}
Thanks!
As stated by CommonsWare in the comments you just have to setTitle on your MenuItem entries. For example during inflation like:
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
menu.getItem(0).setTitle(R.string.first_label_for_menu_item);
menu.getItem(1).setTitle(R.string.second_label_for_menu_item);
}
Related
I am trying to add menu item and it's icon, but the design result does not show the item's title and icon, What should I do?
Use this in your class and see again :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
Hi this is my first question. i am learning android. here i am trying to setup menu icon top menubar.
I have added sets of item in menu. i want to manage icon from activity.
I am trying to show hide menu icon.
Menu return null in onCreate.
Is there any other way to manage menu icon dynamically ?
please help.
This is the activity class code snippet where i am trying to manage menu.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMenu = (Menu) findViewById(R.id.menuBar);//here Menu return null
mMenuItem = mMenu.getItem(2);
mMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
mMenuItem.setVisible(true);
}
will appreciate your help. thanks.
First inflate menu to get MenuItem in activity method onCreateOptionsMenu and then try to get menu.getItemlike this :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_activity, menu);
MenuItem item=menu.getItem(2);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
item.setVisible(true);
return true;
}
Dont put it on your onCreate because your menu is initialized in onCreateOptionsMenu
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.your_menu, menu);
mMenuItem = menu.getItem(2);
mMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
mMenuItem.setVisible(true);
return true;
}
Just follow the bellow instruction:
Step 1:
You need to inflate menu item from onCreateOptionsMenu(Menu menu)
Step 2:
You need a MenuInflater object that you can get using getMenuInflater()
API. Like: MenuInflater inflater = getMenuInflater();
Step 3:
Inflate you menu xml file like: inflater.inflate(R.menu.menu_bottom_nav,
menu);
Step 4:
You have to get menu object for specific item like : MenuItem menuItem =
menu.getItem(index). Here Index is the number depending on which menu item's
object you want to get.
Full code example:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_bottom_nav, menu);
MenuItem menuItem = menu.getItem(0);
return true;
}
I have implemented ActionBar Navigation using Fragment. In my App i have one Activity and rest is in Fragments. In my MainActivity i am implementing menu like this.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Two Fragments uses Navigation Drawer and in their respected fragments i am inflating menu buttons to sort items.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.sort_button_shops, menu);
}
Now the Problem is if i open the Fragment 1 it works perfectly. When i open fragment 2, it shows 2 button to sort, one from Fragment 1 and the second one from Fragment 2.
I have tried to hide the button but it didn't worked.
Any Help will be Appreciated.
Thanks
When you inflate a new menu you are adding new items to the old Menu object, which is probably not what you intended.
Try this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.removeItem(R.id.your_menu_item);
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Try using this in onResume() of fragments.
MenuItem item = (MenuItem) findViewById(R.menu.activity_main);
item.setVisible(false);
this.invalidateOptionsMenu();
I'm wondering how to do that : when the user press hardware search button, it's open a search view in the actionbar.
Basically I have an activity hosting fragments.
One fragment add to the ActionbarSherlock a searchview, it's working fine:
#Override
public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu,
MenuInflater inflater) {
if(searchView==null)
searchView = new SearchView(getSherlockActivity().getSupportActionBar().getThemedContext());
searchView.setQueryHint(getString(R.string.search));
searchView.setOnQueryTextListener(this);
menu.add(Menu.NONE, R.string.search, Menu.NONE, R.string.search)
.setIcon(R.drawable.abs__ic_search_api_holo_light)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
super.onCreateOptionsMenu(menu, inflater);
}
I think I can also catch the hardware key in the FragmentActivity with
#Override
public boolean onSearchRequested() {
//DO SOMETHING
return super.onSearchRequested();
}
But I do not see how to open the searchview when the hardware search button is pressed.
Any hint ?
Thanks :).
So, here how I managed to use the hardware search key to open the searchview in the sherlock actionbar :
In my SherlockFragmentActivity :
private Menu ABSMenu;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
ABSMenu = menu;
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onSearchRequested() {
MenuItem mi = ABSMenu.findItem(R.string.search); // R.string.search is the id of the searchview
if(mi!=null){
if(mi.isActionViewExpanded())
{
mi.collapseActionView();
}else
{
mi.expandActionView();
}
}
return super.onSearchRequested();
}
You can expand the search view and collapse it like
searchView.setIconified(false);
searchView.setIconified(true);
This way you can programatically invoke search view
I have the following in my activity (sorry new to Java/Android):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.selectItem:
// menu.add(...) --> how to get the menu instance?
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I am wondering, how can I access a menu object in onOptionsItemSelected? For example, how would I go about adding a new view to the options menu based on the selection of an existing menu item? Is the answer related to "onPrepareOptionsMenu"?
you should use SubMenu for such things ... remeber that you cant add submenu to another submenu ... so only Menu->Submenu is possible you can't do stuff like this Menu->Submenu->Submenu (while Submenu is Dialog with choices)