option item selection in Dialog menu - android

I have been running into some troubles recently and I think I need your help :).
I am currently trying to show a menu on top of a dialog, I know that it could be far easier to launch a new activity yet doing so would compell me to store/pass a lot of data.
I managed to show an optionmenu by writing a custom dialog and rewriting the oncreateOptionMenu method.
My problem is I can't get any listener to these button, I tried to rewrite the onoptionitemselectedmethod but nothing happens.
Ps: my dialog is nearly full screen so i can't see the activity dialog (i didn't find any put on top method)
I would be glad to try any solution you could provide.
Thanks a lot
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(Menu.NONE,0,Menu.NONE,c.getString(R.string.home));
menu.add(Menu.NONE,4,Menu.NONE,c.getString(R.string.report));
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if(item.getItemId()==0){
getOwnerActivity().startActivity(new Intent(c,Home.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
}
else
if(l>1)
getOwnerActivity().startActivity(new Intent(c,report.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
else
dismiss();
return true;
}

Maybe this little snippet out of my app helps you:
private static final int REFRESH_ID = Menu.FIRST + 1;
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, REFRESH_ID, 0, R.string.menu_refresh).setIcon(R.drawable.and_refresh);
return result;
}
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case REFRESH_ID: {
// Do whatever you want here!
return true;
}
}
return super.onMenuItemSelected(featureId, item);
}
As you can see, I've got constants for my menu-items. Those items get the Menu.First + n number as integer. For every item, I count it up. Easier, then change it everytime ;)
And in the onMenuItemSelected you can switch those constants easily.
Hope that helps!

Related

Overflow menu layout lag

When I'm opening the overflow menu, at first I see just an empty layout, and only after that the menu with text appears:
The same thing happens when the overflow menu is closing:
Creating this menu seems to be standard:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_cp:
return true;
case R.id.action_settings:
return true;
case R.id.action_used_libraries:
return true;
case R.id.action_help_and_feedback:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
why does it happen and how to get rid of this lag?
Try removing <item name="android:background">#color/yourgreencolor</item> from both your ThemeOverlay.AppTheme.ActionBar and ThemeOverlay.AppTheme.PopupMenu style, it's rendering a background first. I used parent="ThemeOverlay.AppCompat.Light" to have my desired white background, maybe that could help you find out how to have a green background despite this issue. It's definitively possible to have custom colors without this, I've seen some.

Android webview override startActionMode yet keep default dismiss of text selection

After searching for a similar question I gave up and decided to ask myself , if there is an existing answer please notify me.
I have a WebView in which I needed to override the default actionMode , I followed suggestions from many Q & A here and everything was working well until I have noticed that something very specific was missing in the behavior of the actionMode. The dismiss event for the text selection only happens when the user presses "Back button" or "Done" on the action mode. In the Default actionMode when the user presses outside the selected text the actionMode also closes , and this I cannot achieve and not sure how can I acheive it without complicating stuff and writing extra lines of code instead of using the "super" behavior for dissmissing text selection when the user presses outside the selected text.
Hope you can help , thanks ahead.
My WebView with the overriden startActionMode:
#Override
public ActionMode startActionMode(ActionMode.Callback callback) {
/*
* When running Ice Cream Sandwich (4.0) or Jelly Bean (4.1 - 4.3),
* there is a hidden class called 'WebViewClassic' that draws the
* selection. In order to clear the selection, save the callback from
* Classic so it can be destroyed later.
*/
// Check the class name because WebViewClassic.SelectActionModeCallback
// is not public API.
String name = callback.getClass().toString();
if (name.contains("SelectActionModeCallback")) {
webViewDefaultActionModeCallBack = callback;
}
if (mActionMode == null) {
mActionMode = super.startActionMode(mActionModeCallback);
}
return mActionMode;
}
My ActionModeCallback :
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
// Called when the action mode is created; startActionMode() was called
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
return true;
}
// Called each time the action mode is shown. Always called after
// onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false; // Return false if nothing is done
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
// do something
break;
case R.id.item2:
// do something
break;
}
mode.finish();
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
// Semi-hack in order to clear the selection
// when running Android earlier than KitKat.
if (webViewDefaultActionModeCallBack != null) {
webViewDefaultActionModeCallBack.onDestroyActionMode(mode);
}
}
};

How to add more options to Android default Contextual Action Bar

As we know, by default, after selecting some text on views, android displays Contextual Action Bar (CAB) with some default options, such as: copy, cut, select all...
Now, I want to have an application (that has only 2 options: ON/OFF), If I turn it ON, Some other options will be added to default CAB. If I turn it OFF, my custom options will be removed from Android default CAB.
My question is: Is it possible to Add/Remove some options to this default CAB? How can I make above application?
Thank you!
You'll have to use the setCustomSelectionActionModeCallback on each of your TextViews.
You can have a boolean:
boolean on = true;
Then create a method that actually edits the CAB like so:
private void editContextualActionBar(ActionMode actionMode, Menu menu) {
if (on) {
// adds a new menu item to the CAB
// add(int groupId, int itemId, int order, int titleRes)
menu.add(0, R.id.action_to_be_performed, 1, R.string.action_name);
} else {
// removes the new menu item
menu.removeItem(R.id.action_to_be_performed);
}
}
Finally, call the Callback on your TextView with the editContextualActionBar method in onCreateActionMode and perform the menu action in onActionItemClicked:
textView.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
editContextualActionBar(mode, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.action_to_be_performed:
// perform action
return true;
default:
break;
}
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
});

onCreateOptionsMenu not working properly

I have this recurring issue with the onCreateOptionsMenu method. I have it set up so it enables or disables options depending on the value of some SharedPreferences, but for some reason the first time you open the menu it doesn't work as it should, the options that should be disabled are enabled and the other way around. If I close it and reopen it, it works fine.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
menuConfig(menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
menuConfig(menu);
return true;
}
public void menuConfig(Menu menu){
menu.getItem(getResources().getInteger(R.integer.MENU_ABOUT)).setEnabled(true);
if (preferences.getBoolean(getString(R.string.PREFS_STARTED), false)){
menu.getItem(getResources().getInteger(R.integer.MENU_START)).setEnabled(false);
menu.getItem(getResources().getInteger(R.integer.MENU_STOP)).setEnabled(true);
}else{
menu.getItem(getResources().getInteger(R.integer.MENU_START)).setEnabled(true);
menu.getItem(getResources().getInteger(R.integer.MENU_STOP)).setEnabled(false);
}
if(!preferences.getBoolean(getString(R.string.PREFS_STARTED),false) && preferences.getBoolean(getString(R.string.PREFS_FILES_CREATED),false)){
menu.getItem(getResources().getInteger(R.integer.MENU_DELETE)).setEnabled(true);
menu.getItem(getResources().getInteger(R.integer.MENU_SET_ID)).setEnabled(true);
}
else{
menu.getItem(getResources().getInteger(R.integer.MENU_DELETE)).setEnabled(false);
menu.getItem(getResources().getInteger(R.integer.MENU_SET_ID)).setEnabled(false);
}
}
These two pics are taken subsequently with a few seconds between each menu key press.
The first one is wrong, it should look like the second one.
I fixed it by changing the default value returned by the queries to the SharedPreferences object but I have no idea of what is actually happening.

Android - checkable buttons on options menu

Can anyone point in the direction of any tutorials that show how to create an options menu with clicakble checks like in the picture below:
alt text http://img291.imageshack.us/img291/1221/deviceit.png
I have tried as follows:
/** Menu creation and setup **/
/* Creates the menu items */
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, 1, 0, "Speaker").setCheckable(true);
menu.add(0, 2, 0, "Mute").setCheckable(true);
return result;
}
/* Handles item selections */
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
if(audioManager.isSpeakerphoneOn()==false){
audioManager.setSpeakerphoneOn(true);
audioManager.setRouting(AudioManager.MODE_IN_CALL,
AudioManager.ROUTE_SPEAKER, AudioManager.ROUTE_ALL);
}else{
audioManager.setSpeakerphoneOn(false);
audioManager.setRouting(AudioManager.MODE_IN_CALL,
AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL);
}
return true;
case 2:
if(audioManager.isMicrophoneMute())
audioManager.setMicrophoneMute(false);
else
audioManager.setMicrophoneMute(true);
return true;
}
return false;
}
But this doesn't work it only gives me text on the buttons on the options menu
EDIT: I have added the following onPrepareOptionsMenu method:
public boolean onPrepareOptionsMenu(Menu menu){
boolean result = super.onPrepareOptionsMenu(menu);
if(audioManager.isSpeakerphoneOn())
menu.findItem(1).setChecked(true);
else
menu.findItem(1).setChecked(false);
if(audioManager.isMicrophoneMute())
menu.findItem(2).setChecked(true);
else
menu.findItem(2).setChecked(false);
return result;
}
However I get the same outcome just text and no check light as in the picture above
If you want to change dynamically the state of your Option Menu, you need to use onPrepareMenu(). In this method, you can do dynamic checks and update anything you want.
Good luck!!
documentation
After some digging, this look like a custom view. I think your picture comes from this code.
This is an old question, but I had the same problem and searched a lot to find such an optionsMenu shown above. I found a tutorial on http://www.codeproject.com and modified it a little bit. Maybe it is not a profi-programmers-code, but it works for me. See my modifications at my (poor arranged) web page on google sites (I got an little tutorial too on this site):
https://sites.google.com/site/opiatefuchs/android-code-examples
This code is original from wjfrancis on the Code Project web page (many props):
http://www.codeproject.com/Articles/173121/Android-Menus-My-Way
I just modified it and would be glad if somebody got any ideas to improve this code. But for now, it works.

Categories

Resources