In my Android application I have a Button which opens up a context menu when clicked. The issue is that if a user clicks quickly, they can open multiple instances of the menu.
b.setOnClickListener( new OnClickListener() {
#Override
public void onClick( View view ) {
// popup options
view.showContextMenu();
}
} );
How can I prevent the user from opening up more than one copy? I am looking for a 'boolean' like checking the Visible status, but can't seem to find anything. My hope was that there was a function somehow that would result in code similar to:
if (context menu is not open)
open context menu
else
don't do anything
I really don't like this UI pattern. It's this sort of thing that cause iOS developers (and users) to think that Android developers lack discipline. Context menus are for long-presses, period. Use something else, like an AlertDialog or PopupMenu, elsewhere.
That being said, set a boolean flag when you show the context menu, checking it first to prevent duplicate menus. Clear the flag in onContextMenuClosed().
Related
I have this function where I need to return a list depending of what user pressed in Alert Dialog (cancel or save).
But I have an issue, let's imagine we have a list with a size of 10. Then on the iteration of that list it will build 10 alert dialogs at the same time plus a dark black shadow at the background caused by these.
So I'd like to "pause" until user pressed or find a way to don't pop up all these alert dialog at the same time and just appear one by one once pressed a button.
A quick reminder: I need to return a list after all dialogs have been pressed.
Question: How could I do that?
It would be better if you provided some code with this. Anyway, even though this is not something I would do and create 10 dialogs in the for loop, this can be done.
Just create a Boolean inside your for loop which will be used to check if the dialog is dismissed.
for(int i = 0; i < list.size(); ++i) {
Boolean isDismissed = false;
AlertDialog d = new AlertDialog(getBaseContext());
d.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
isDismissed = true;
}
});
//start your dialog
while(!isDismissed) {
//do nothing
}
}
As I said, I wouldn't do this.
Because I evaluate first a list of items, then I set on a new list of items that will require user to confirm about what to do with that, so I loop that with alert dialogs waiting for user to tell me what to do with those items
There is a much better way to do this. Why not starting one CustomDialog which will ask the user what to do with those items. He could choose options for each item with a spinner or if options are KEEP or DELETE just use checkbox or something.
So as people said, creating Alert Dialogs in a loop is a bad practice so my solution into this is just setting a view on Fragment that acts like a Dialog but I just turn it visible and gone whenever I need. This seems a proper solution for my case.
When user accept or cancel the view (clicking on button) just send it to the viewmodel and the viewmodel will evaluate if there are still items on the list. If there are items then show again this "view" on Fragment asking to user what to do :)
I don't have code to show because I haven't done it yet but I have thought for a while and this is the best I can think about. Hope it helps for someone who is in the same situation!
Overview
I'm developing a card game. Sometimes though the user may want to start over and create a new game, that's why I thought having a button in a corner that pops up a 'floating' menu where the user has the option to start a new game and quit the current one.
I know how to start over a new game, but I don't know how to make a button appear in every fragment (I have a FragmentManager) capable of making pop up a Menu, possibly a floating one so the user can still see what's behind.
I took a look at this API Guide for Menus but I'm not sure how to use this for my purpose, what do you suggest?
Example
Here's an example of the Menu I'd like to achieve:
All you need is to declare your custom dialog class:
public class CustomDialog extends Dialog {
public CustomDialog(final Context context) {
super(context);
setContentView(R.layout.your_custom_layout);
(...)
}
}
you can start another activities using context passed through constructor etc.
It's not a menu, it's called Dialog in Android. You need to google how to create custom Dialog in Android. You will see lots of tutorials that do this.
I have a button that when pressed, will call the company. Now, I was doing some research and found that there is a way to include a context menu. I really like the context menu because it gives you so many options.
Do you think it would be a waste of code to set a context menu for a click of the button that when pressed will open up the options to add contact, call contact, sms contact, etc.? Is it necessary?
I did come across these:
Android opening context menu after button click
http://developer.android.com/guide/practices/ui_guidelines/menu_design.html#tour_of_the_menus
I think it would be a good feature to include. Thats what context menu is there for, to give more options. I think it would be good to give the user more options when the button is clicked. Well it makes more since anyway.
Heres how you would get the long click
Button downSelected = (Button) findViewById(R.id.downSelected);
downSelected.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return true;
}
});
EDIT:
If you just want one click on the button just register its click listener like this..
downSelected.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}
It would be nice to provide a big main button to call the number, and some additional mechanism, let's say a smaller + button to do more stuff with as you sugggested. Also a long click could be considered a right user interaction to provide with more features.
Just a user feeling...
Halo,
My ideia is to show a Context Menu by clicking on one of the options menu.
I don't think it will be quite difficult but I'm not being able to get the View in the OptionsMenu class.
So actually it is just call the showContextMenu() on the OptionsMenu class.
Can somebody give me a hint?
ty!
It's possible to call a Context Menu by means other than a long press. If you implement your menu options within your activity, using an inner class for instance, you should be able to call your context menu:
yourView.showContextMenu();
Remember to register your context menu at onCreate:
registerForContextMenu(yourView);
Finally made it, and it was a quite simple solution.
Basically I pass the View, to the OptionsMenu constructor.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_event);
View v = findViewById(R.id.activity_event);
registerForContextMenu(v);
options = new Options(getApplicationContext(), getMenuInflater(), v);
fillData();
}
...
private class Options extends ActivityOptionsMenu {
public Options(Context c, MenuInflater mi, View v) {
super(c, mi, v);
}
}
...
private abstract class ActivityOptionsMenu {
...
public void onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case R.id.directions:
v.showContextMenu();
}
}
}
Thanks for all the help.
My ideia is to show a Context Menu by clicking on one of the options menu.
AFAIK, you cannot show a context menu from a long-tap on an option menu choice.
I don't think it will be quite difficult but I'm not being able to get the View in the OptionsMenu class.
The View is not exposed to you.
The only way to literally do what you seek is to not use the standard Android option menu. You can watch for the MENU key via onKeyDown() in your activity, at which point you can display whatever you want, including something that supports context menus.
However, context menus are not particularly popular among users, simply because they are not very discoverable. Users never read the documentation, even if it is supplied, and they tend not to randomly stab the screen to see if a menu will pop up. They will be even less likely to decide to hold down a fake option menu choice to see if a context menu will pop up.
Hence, I really recommend you consider some other approach, for improved usability.
This might be a simple question, but I've been looking around and can't find the answer. Is there any code to show the context menu on Android from a code, instead of pressing the menu button? E.g. when I touch the screen then it'll call the context menu?
Call openContextMenu() on your Activity whenever you want to open it. Note that this is a rather unusual UI pattern, one that your users may not expect.
OnClickListener onClick_Show_Contextmenu = new OnClickListener() {
#Override
public void onClick(View v) {
((Activity) context).openContextMenu(v);
}
};
findViewById(R.id.xxx).setOnClickListener(onClick_Show_Contextmenu);
registerForContextMenu(findViewById(R.id.xxx));
findViewById(R.id.xxx).setLongClickable(false);
you can use any of the following:
openContextMenu as shown here:
registerForContextMenu(view);
openContextMenu(view);
unregisterForContextMenu(view);
setOnCreateContextMenuListener
showContextMenuForChild
You can use
view.showContextMenu();
on your view.