Android OptionsMenu, add items on runtime - android

I've got an optionsmenu looking like this right now:
Lets say that if I click item 1, i want two new items added to the menu looking like this:
I'm having problems doing this at runtime(while it's open) since onCreateOptionsMenu is only called once and onPrepareOptionsMenu seems only to be called when the menubutton of the phone is clicked. I just want it to refresh with these new items added.
Thanks for any help!

When you select an option item it causes the system to close the options menu. This happens after onOptionsItemSelected() runs. I'm guessing what you need to have happen is for that entire process to complete then have the options menu programmatically opened again so onPrepareOptionsMenu() is called.
Try this:
In onOptionsItemSelected(), when the user selects your "add two more options" option, set a flag in your activity but don't do anything else.
Override onOptionsMenuClosed(). Check if the flag is set. If it is, then post to be executed by the UI thread on its next pass. It should do nothing but open the options menu again, e.g.
runOnUiThread(new Runnable() {
#Override
public void run() {
openOptionsMenu();
}
});
Override onPrepareOptionsMenu(). Check if the flag is set. If it is, then add your two additional menu items and un-set the flag. You'll need to do some additional work to prevent the "add more items" menu item from continuing to add new items every time its pressed, unless that's the behavior you're looking for.

Related

How to show "up" / "back" button in Xamarin.Forms?

I am trying to work out how to show the "up" arrow in Xamarin.Forms without a pushing a page onto the stack. I.E. I just want to perform an action when the back button is pressed. I am completely stuck on this so any help would be appreciated.
I have tried creating a custom renderer which handles a view property called DisplayHomeAsBack. Which in the renderer calls the following:
FormsAppCompatActivity context = ((FormsAppCompatActivity)Forms.Context);
Android.Support.V7.App.ActionBar actionBar = context.SupportActionBar;
if (actionBar != null)
{
actionBar.SetDisplayHomeAsUpEnabled(element.DisplayHomeAsBack);
}
Unfortunately it seems this does absolutely nothing, even though all online tutorials and stackoverflow question for android suggest this method.
The plan is that I can then use the "OnBackButtonPressed" override in MasterDetailPage, which should allow me to perform this action. Unfortunately displaying the back button has been the larger hurdle so far!
Any idea of a better way to do this or how I can get the current mechanism to work?
EDIT
I have created a project and uploaded it to this question on the Xamarin support forums, if it helps.
http://forums.xamarin.com/discussion/comment/186330#Comment_186330
Sorry to keep you waiting so long!
Warning that I did not actually run this code and changed it from my own so I would be surprised if it worked perfectly without some changes.
So below should add a back button where there was not one before (so like when there is not really a page to go back to) and then we will add a custom action to perform when it gets pressed.
I would suggest you push a new page onto the stack without using animation so it is transparent to the user and also makes all of this much simpler, but if you absolutely do not want to do that, the below method should work.
MainActivity:
//Use this to subscribe to the event which will create the back button
public override bool OnCreateOptionsMenu(IMenu menu) {
if(menu != null && App.AppMasterPage != null) { //You will need this to make sure you are on your MasterDetailPage, just store a global reference to it in the App class or where ever
Xamarin.Forms.MessagingCenter.Unsubscribe<string>(this, "CreateBackButton");
Xamarin.Forms.MessagingCenter.Subscribe<string>(this, "CreateBackButton", stringWeWillNotUse => { //Use this to subscribe to the event that creates the back button, then when you want the back button to show you just run Xamarin.Forms.MessagingCenter.Send<string>(this, "CreateBackButton")
ActionBar.DisplayOptions = ActionBarDisplayOptions.ShowTitle | ActionBarDisplayOptions.ShowHome | ActionBarDisplayOptions.UseLogo | ActionBarDisplayOptions.HomeAsUp; //You may need to play with these options to get it working but the important one is 'HomeAsUp' which should add the back button
});
} else {
Xamarin.Forms.MessagingCenter.Unsubscribe<string>(this, "CreateBackButton");
}
return base.OnCreateOptionsMenu(menu);
}
Now the next step is do do a custom action when it is pressed. I think you can either override OnBackPressed() or OnOptionsItemSelected() in MainActivity or maybe you can override the MasterDetailPage method. I am not sure.
Which ever one works for you, inside of that override, I would simply check to see if you are on your App.AppMasterPage like we did above, and if so, send a MessagingCenter message which your App.AppMasterPage has already subscribed to in order for it to handle the custom action.
If you get stuck let me know!
I know it sounds like a bit of a hack, but the best "solution" I have found so far is to add a page behind the current page (behind the root) so it is not visible. Then when the user presses the back button, handle it by removing that page.

Handle back action for multiple view

I have an activity which have multiple piece of UI panel(you can think them as view in android), these panels will be invisible by default.
Now when user trigger action1, PanelA will display, when trigger action2, PanelB will display(at different location of the screen).
Both PanelA and PanelB is visible at the moment, now when user hit the back menu, the PanelB should disappear, and PanelA should disappear when hit the back menu again.
At first, I use View to hold different panels, however I found it is difficult to keep the state consist, for example, the activity will be a little different between PanelA and PanelB.
Then I found the fragment, however after I tried and tested, I found that the addTobackStack() can not apply to my us-case, since the PanelA and PanelB are at different location, android can not save their state by default.
So I wonder if there is any other solution for my requirement?
You need to manually handle this scenario inside onBackPressed() method of an Activity.
For instance -
#Override
public void onBackPressed() {
if (panelB.isOpened()) {
panelB.close()
} else if (panelA.isOpened()) {
panelA.close()
} else {
super.onBackPressed();
}
}
When panelB is opened, it will close only panelB and wont do anything else. Same goes for panelA, if its opened and when both the panel are closed then it will exit the app like normal.
I highly recommend to use DialogFragments here as you can call show() and dismiss() any point of time on that and they can handle custom views pretty well.
Hope it helps.

CAB "check" button

How can we handle the check button in the CAB menu?
Is the item associated to some internal id like android.R.id.checkbox?
I've read that it could be handled in SherlockActionBar CAB, but could it be the same with the native ActionBar CAB?
Is there any way to detect the interaction of this item? onActionModeFinished() is not sufficient since I'm calling it multiple times since the CAB needs to be present due to previous changes that happened.
Thanks.
Ok, finally found a solution after trying some things.
Place the code you want for the checkbox or onbackpressed in the method below
public void onDestroyActionMode(ActionMode mode)
{
//place the code you want for the checkbox or back icon here. If you don't want
// this code run if other selections are used, then just create a boolean value that
//you earlier on and check the value in this section before implementing the code
}
};

Can anyone help me figure out why my OptionsMenu won't show up?

I am using tabs, with Activity groups in each. I want to add an options menu to certain activities but it won't show on any. I have options menus working in other projects with the exact same code, so I can't figure out why they won't show up. This is the code I am using:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Option 1");
menu.add("Option 2");
menu.add("Option 3");
return super.onCreateOptionsMenu(menu);
}
Is there anything wrong with this code or anywhere else I should be looking at that might be blocking this menu from showing when I hit the menu button?
The only thing that would be blocking the menu AFAIK would be if you are overriding onKeyDown. Make sure you aren't overriding that method and thus preventing the menu button from doing what it is supposed to.

how to rename existing menu item?

Do you know how to rename existing menu ?
I can rename when press menu item. But I don't know how to access to menu item when press the button.
Please advice.
It would be good if you can clarify the question a little, but each time the user presses the Menu on their Android device while inside one of your activities, the onPrepareOptionsMenu method is called. The first time the menu is shown (i.e. only once), the onCreateOptionsMenu method is called.
Basically, the onPrepareOptionsMenu method is where you should make any changes such as enabling/disabling certain menu items, or changing the menu item text depending on the circumstances.
As an example:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// Check current message count
boolean haveMessages = mMessageCount != 0;
// Set 'delete' menu item state depending on count
MenuItem deleteItem = menu.findItem(R.id.menu_delete);
deleteItem.setTitle(haveMessages ? R.string.delete : R.string.no_messages);
deleteItem.setEnabled(haveMessages);
return super.onPrepareOptionsMenu(menu);
}
Use MenuItem.setTitle(). If this isn't what you needed, you have to be more specific.
The onPrepareOptionsMenu is the proper place to make changes to menuitems.

Categories

Resources