Change options menu during runtime - invalidateOptionsMenu() - android

I am creating a menu where one of the items is used the lock an object. When this item is clicked, the menu should be recreated with a button to unlock the item. I created two menus for this. This is working fine. I read that in Android version >= 11 the onPrepareOptionsMenu is no longer called when displaying the menu and I have to call invalidateOptionsMenu(). So I changed the build target (both in the Manifest and in properties) to 11 and ran the app on an AVD of 4.0.3. The program is still working fine, but I thought it shouldn't anymore, and I should check
if (Build.VERSION.SDK_INT >= 11)
{
invalidateOptionsMenu();
}
This is my code:
public class MainActivity3 extends Activity{
boolean locked;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locked = false;
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.changing_menu1, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
MenuInflater inflater = getMenuInflater();
if (locked) {
inflater.inflate(R.menu.changing_menu2, menu);
}
else {
inflater.inflate(R.menu.changing_menu1, menu);
}
return super.onPrepareOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Menu1:
break;
case R.id.Menu2 :
break;
case R.id.Menu3 :
locked = !locked;
break;
}
return true;
}
}
So the Menu is still refreshed/recreated in 4.0.
Did I misunderstand something about the usage of invalidateOptionsMenu();?

invalidateOptionsMenu() was added to give us the ability to force onCreateOptionsMenu() to be called again. onPrepareOptionsMenu() is still called every time you call the menu.
What you are trying to achieve above is a good example of when to use invalidateOptionsMenu() but because of backwards compatibility you will need to do both:
if (Build.VERSION.SDK_INT >= 11) {
invalidateOptionsMenu();
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
if (Build.VERSION.SDK_INT >= 11) {
selectMenu(menu);
}
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (Build.VERSION.SDK_INT < 11) {
selectMenu(menu);
}
return true;
}
private void selectMenu(Menu menu) {
menu.clear();
MenuInflater inflater = getMenuInflater();
if (locked) {
inflater.inflate(R.menu.changing_menu2, menu);
}
else {
inflater.inflate(R.menu.changing_menu1, menu);
}
}

Related

building menus in fragments in android

I am building an application in which each fragments have different menus but the code I am using to add menu but the menu option doesn't actually exists.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu,menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemThatWasClickedId = item.getItemId();
if (itemThatWasClickedId == R.id.logoutitem) {
((tailorDashboard)getActivity()).logoutwork();
return true;
}
return super.onOptionsItemSelected(item);
}
sir basically i am using this code to make a option menu in fragments i have used android.support.v7.widget.toolbar option to make my custom toolbar. but the code i have already used is not actually making the option menu.
`#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu,menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemThatWasClickedId = item.getItemId();
if (itemThatWasClickedId == R.id.logoutitem) {
((tailorDashboard)getActivity()).logoutwork();
return true;
}
return super.onOptionsItemSelected(item);
}'

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

Rename menu item in Android

I would like to rename menu on click of an item. I tried the following but it didn't change.
I tried item.setTitle("LandScape"); or item.setTitle("Portrait");
Declare the menu:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
Do something on click on the menu item:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
boolean result = true;
switch(item.getItemId())
{
case R.id.oscillation_mode:
{
if(getScreenOrientation() == 1)
{
Log.e("Orientation","ABC"+getScreenOrientation());
item.setTitle("LandScape");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else
{
item.setTitle("Portrait");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
break;
}
The text is the same. The default it is Portrait.
when you call setRequestedOrientation() your Activity is destroyed and recreated, so any changes you make to in memory objects will be dropped. You should make these changes when the menu is shown instead
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem oscillation = menu.findItem(R.id.oscillation_mode);
if (getScreenOrientation() == 1){
item.setTitle("LandScape");
}else{
item.setTitle("Portrait");
}
return true;
}

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

Categories

Resources