In my app i have an optionsmenu. It has 2 buttons. Depending on a boolean value i would like to show/hide one of the buttons. I've got the following code but it doesn't hide the button. How can i do this?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menushowmoredetails, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(displayRotaDetails.equalsIgnoreCase("false")){
if(item.getItemId() == R.id.moredetails)
item.setVisible(false);
}
switch (item.getItemId()) {
case R.id.back:
onBackPressed();
return true;
case R.id.moredetails:
You have to use the onPrepareOptionMenu method like this:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
// add your conditions here and change 0 with the R.id.moredetails item postion.
if(displayRotaDetails.equalsIgnoreCase("false")){
menu.getItem(1).setVisible(false);
}
}
Related
I have implemented a menu item to search a list view. I need to make a view invisible when the menu item is selected. This is easily done with this code in my fragment:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_search:
addButton.setVisibility(View.INVISIBLE);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I can't figure out how to set the visibility back when I am done with the search (I am using SearchView). I tried to use onOptionsMenuClosed (Menu menu) but that is not being called for some reason.
Thanks in advance
Try using setOnActionExpandListener():
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.your_menu, menu);
menu.findItem(R.id.action_search).setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
addButton.setVisibility(View.VISIBLE);
return false; // change to true if `false` wont work for your case
}
});
return super.onCreateOptionsMenu(menu);
}
onMenuItemActionCollapse() will be called when SearchView is collapsed or closed.
I am trying to hide the menu in the TableLayout with ViewPager I want the menu only in the solutions tab. I used the onPrepareOptionsmenu to hide the menu in tabs except the solutions tab. the thing is my onOptionsItemSelected is not working.
code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inf = getMenuInflater();
inf.inflate(R.menu.simpleadd,menu);
// +getMenuInflater().inflate(R.menu.simpleadd, menu);
onPrepareOptionsMenu(menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (viewpager.getCurrentItem()==0){
menu.findItem(R.id.simpleadd).setVisible(false);
} else if(viewpager.getCurrentItem()==1){
menu.findItem(R.id.simpleadd).setVisible(false);
} else if(viewpager.getCurrentItem()==2){
menu.findItem(R.id.simpleadd).setVisible(true);
} else if(viewpager.getCurrentItem()==3){
menu.findItem(R.id.simpleadd).setVisible(false);
}else if(viewpager.getCurrentItem()==4){
menu.findItem(R.id.simpleadd).setVisible(false);
}
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.simpleadd:
startActivity(new Intent(this,NewSolution.class));
}
return super.onOptionsItemSelected(item);
}
thanks in advance,
You have used the layout's name in the switch case of onOptionsItemSelected.
Use menu item's id instead.
First, you need to get item id and then set compare with layout ids,
this helps you to set the functionality. Please find the below sample
code for your reference.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.simpleadd) {
// execute your code here
}
return super.onOptionsItemSelected(item);
}
How can I remove the three dots to the right of my SearchView ?
set false in onPrepareOptionsMenu
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
return false;
}
Maybe this helps you
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_activity, menu);
MenuItem searchMI = (MenuItem) menu.findItem(R.id.menu_search);
searchMI.setOnActionExpandListener(new OnActionExpandListener(){
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
// Hide menu icon
return true;
}
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
// Show menu icon
return true;
}
});
return true;
}
You can show or hide icons like this:
menu.findItem(R.id.yourActionId).setVisible(true);
menu.findItem(R.id.yourActionId).setVisible(false);
You need to hide all others menu items one by one...
#Override
public boolean onMenuItemActionExpand (MenuItem item){
menu.getItem(MENU_MAIN_REFRESH).setVisible(false);
menu.getItem(MENU_MAIN_SETTINGS).setVisible(false);
menu.getItem(MENU_MAIN_EXIT).setVisible(false);
return true;
}
I have two option items in a Toolbar. When one item is clicked, that item will be enabled. Then another item must be disabled. But, once the item was disabled I can't fire click event anymore on that item. Is there anyway that I can click on the disable item?
Thank you
I did like this but its not working anymore
MenuItem tureMenuItem;
MenuItem dingMenuItem
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
getActivity().getMenuInflater().inflate(R.menu.lore_fragment, menu);
tureMenuItem = menu.findItem(R.id.menu_ture);
dingMenuItem = menu.findItem(R.id.menu_ding);
tureMenuItem.setEnabled(true);
dingMenuItem.setEnabled(false);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_ding:
tureMenuItem.setEnabled(true);
dingMenuItem.setEnabled(false);
break;
case R.id.menu_ture:
tureMenuItem.setEnabled(false);
dingMenuItem.setEnabled(true);
break;
}
return super.onOptionsItemSelected(item);
}
Using setEnabled() will work. manage with your conditions.
you must be doing some action on those menu items right. You can enable the other item at the end of action.since you have not posted I have only option. Using Handler.PostDelayed.
boolean isMenuEnabled;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.action_settings) {
this.isMenuEnabled = true;
tureMenuItem.setEnabled(true);
dingMenuItem.setEnabled(false);
handler.postDelayed(runnable,1000);
return true;
}
else if(item.getItemId()== R.id.action_settings1) {
this.isMenuEnabled = true;
tureMenuItem.setEnabled(false);
dingMenuItem.setEnabled(true);
handler.postDelayed(runnable,1000);
return true;
}
return super.onOptionsItemSelected(item);
}
remove the callbacks
#Override
protected void onDestroy() {
handler.removeCallbacks(runnable);
super.onDestroy();
}
runnable with delay
Runnable runnable = new Runnable() {
#Override
public void run() {
if(isMenuEnabled){
tureMenuItem.setEnabled(true);
dingMenuItem.setEnabled(true);
isMenuEnabled=false;
}
}
};
You need to check below example code to disable and enable menu item vice-versa.
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
if(menu.getItem(0))
{
menu.getItem(0).setEnable(false);
menu.getItem(1).setEnable(true);
}
else if(menu.getItem(1))
{
menu.getItem(1).setEnable(false);
menu.getItem(0).setEnable(true);
}
return super.onPrepareOptionsMenu(menu);
}
I have used a technique (http://united-coders.com/nico-heid/use-android-activitygroup-within-tabhost-to-show-different-activity)
to develop an app where I have 3 tabs and each tab has its own ActivityGroup. I have menus for each activity. But when I press menu button, the menu does not appear. After doing some random trails I found that If I implement onCreateOptionsMenu in ActivityGroup then only menu appears. I am not able to execute onCreateOptionsMenu of Activity.
Please suggest how to use menu of Activity as I have many activities in single ActivityGroup and by implementing onCreateOptionsMenu in ActivityGroup is not the right way to handle this problem.
Here is how you roll with it:
In your ActivityGroup class onCreateOptionMenu() call the current Activity 's onCreateOptionMenu() i.e
public boolean onPrepareOptionsMenu(Menu menu)
{
Activity activity = getLocalActivityManager().getCurrentActivity();
return activity.onPrepareOptionsMenu(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
Activity activity = getLocalActivityManager().getCurrentActivity();
return activity.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected (MenuItem item)
{
Activity activity = getLocalActivityManager().getCurrentActivity();
return activity.onOptionsItemSelected(item);
}
and in your individual Activity
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
return true;
}
#Override
public boolean onOptionsItemSelected (MenuItem item)
{
switch (item.getItemId())
{
case R.id.MENU_LOGOUT:
Dialog.showToast(this, "message");
return true;
case R.id.MENU_HELP:
break;
case R.id.MENU_ABOUT:
break;
}
return super.onOptionsItemSelected(item);
}
and if you want any Activity without having any Menu just override these methods
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
return true;
}