refresh onCreateOptionMenu android - android

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);
}

Related

How to update menu item in android

I have an activity with menu items. Every time when user come to this activity, i want to update the value of textView with some Utility value. This is my 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.menu_my2, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu){
RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.badge).getActionView();
TextView tv = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textview);
tv.setText(String.valueOf(Utility.ShoppingCartItemCount()));
return true;
}
This is updating the value only once (when launching activity). But when user moves from this activity and come on this activity again, this is not getting updated even value for Utility.ShoppingCartItemCount() is updated.
How to solve this?
Try invalidateOptionsMenu()
This will be call onCreateOptionsMenu(Menu) again.
edit)
I have some code like below
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (currentPage == 8) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_p, menu);
return true;
} else {
return false;
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.purchaseRestore:
// TODO: Restore purchase
return true;
case R.id.purchaseTerm:
// TODO: Show Term by WebView
return true;
default:
return super.onOptionsItemSelected(item);
}
}
When my user click other button, put invalidateOptionsMenu() after currentPage = 8 in Button's OnClickListener. In result, User can show Option Menu.
edit))
invalidateOptionsMenu() will force reload onCreateOptionsMenu and onPrepareOptionsMenu.
If this methods doesn't working for you, try to debug Utility.ShoppingCartItemCount().

How to click disabled option menu item in Toolbar android?

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);
}

How to hide an optionsmenu button at runtime

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);
}
}

Android-Problem in Menu of Activity in Activity Group

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;
}

How do I inflate an Android options menu and set an item to Enabled=false?

My XML menu definition sets the item R.id.menu_refresh's enabled state to false. When the app runs the menu item is greyed and disabled. Why is this code in the app not enabling the item?
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem refresh = menu.getItem(R.id.menu_refresh);
refresh.setEnabled(true);
return true;
}
What am I missing?
Try menu.findItem() instead of getItem(). getItem() takes an index from [0, size) while findItem() takes an id.
this is what I do in my activity for the menu handling ...
//Android Activity Lifecycle Method
// This is only called once, the first time the options menu is displayed.
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
return true;
}
//Android Activity Lifecycle Method
// Called when a panel's menu is opened by the user.
#Override
public boolean onMenuOpened(int featureId, Menu menu)
{
MenuItem mnuLogOut = menu.findItem(R.id.main_menu_log_out_id);
MenuItem mnuLogIn = menu.findItem(R.id.main_menu_log_in_id);
MenuItem mnuOptions = menu.findItem(R.id.main_menu_options_id);
MenuItem mnuProfile = menu.findItem(R.id.main_menu_profile_id);
//set the menu options depending on login status
if (mBoolLoggedIn == true)
{
//show the log out option
mnuLogOut.setVisible(true);
mnuLogIn.setVisible(false);
//show the options selection
mnuOptions.setVisible(true);
//show the edit profile selection
mnuProfile.setVisible(true);
}
else
{
//show the log in option
mnuLogOut.setVisible(false);
mnuLogIn.setVisible(true);
//hide the options selection
mnuOptions.setVisible(false);
//hide the edit profile selection
mnuProfile.setVisible(false);
}
return true;
}
//Android Activity Lifecycle Method
// called whenever an item in your options menu is selected
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId())
{
case R.id.main_menu_log_in_id:
{
ShowLoginUI();
return true;
}
case R.id.main_menu_log_out_id:
{
ShowGoodbyeUI();
return true;
}
case R.id.main_menu_options_id:
{
ShowOptionsUI();
return true;
}
case R.id.main_menu_profile_id:
{
ShowProfileUI();
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
I like this approach because it makes the code nice and modular
Use menu.findItem() instead of getItem().
because findItem is used to find item by id.

Categories

Resources