Im using action bar compability library. Im trying to open the options menu from a button with openOptionsMenu() function but it does nothing.
Menu shows as usual when pressing the menu key on my phone. What is wrong here?
public class ReadActivity extends ActionBarActivity {
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
boolean value;
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.read, menu);
value = super.onCreateOptionsMenu(menu);
if (Helper.SupportsNewApi()) {
getActionBar().hide();
} else {
((View) ((LinearLayout) findViewById(R.id.actionbar_compat))
.getParent()).setVisibility(View.GONE);
}
return value;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
case R.id.menu_search:
// Toast.makeText(this, "Tapped search", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_bookmark:
// selectText();
// setFullScreen(false);
break;
case R.id.menu_day_night_mode:
break;
case R.id.menu_settings:
break;
case R.id.menu_zoom_in:
showOverlay(false);
break;
case R.id.menu_zoom_out:
showOverlay(false);
break;
case R.id.menu_table_of_contents:
Intent tocIntent = new Intent(this, TocActivity.class);
int GET_SECTION_REFERENCE = 1;
startActivityForResult(tocIntent, GET_SECTION_REFERENCE);
break;
case R.id.menu_overflow:
Toast.makeText(this, "Tapped overflow", Toast.LENGTH_SHORT).show();
//closeOptionsMenu();
openOptionsMenu(); //tried the below aswell, no results
//getWindow().openPanel(Window.FEATURE_OPTIONS_PANEL, null);
break;
}
return super.onOptionsItemSelected(item);
}
#Override //disable volume buttons
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (!menuShown && (keyCode == 25 || keyCode == 24)) {
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
Log.d(tag, "Keycode is = "+keyCode);
if (keyCode == 82) {
if (!menuShown) {
//openOptionsMenu();
showOverlay(true);
} else {
showOverlay(false);
}
//don't want it to open when pressing menu
return true;
} else if (keyCode == 4 && menuShown) {
showOverlay(false);
return true;
} else if (keyCode == 25 && !menuShown) {
prevPage();
return true;
} else if (keyCode == 24 && !menuShown) {
nextPage();
return true;
}
return super.onKeyUp(keyCode, event);
}
}
I was having the same problem trying to go around this openOptionsMenu thing on an app
that I was doing that should run on Android 1.6 and up. Following the answer from Werner Van Belle I reached the conclusion that we could achieve a workaround to solve the problem.
So I came up with the following code, it's always beatiful when people don't mark a method as final, so we can always override it. It's perfect if you don't want to give up on targeting your app to the latest api (android:targetSdkVersion="17"). I hope you guys like it. :)
#Override
public void openOptionsMenu() {
Configuration config = getResources().getConfiguration();
if((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK)
> Configuration.SCREENLAYOUT_SIZE_LARGE) {
int originalScreenLayout = config.screenLayout;
config.screenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
super.openOptionsMenu();
config.screenLayout = originalScreenLayout;
} else {
super.openOptionsMenu();
}
}
To shed some light on this sad development by google. Google obviously wishes everybody to embrace the new ActionBar. They could have achieved that by making the ActionBar easier to use than the old menu system. That is however not how they planned the transition. No, they thought it would make sense to harras programmers by making the old menus impossible to use but without providing proper backward compatibility.
Below is the code taken from com.android.internal.policy.impl, which is supposed to create the optionsMenu panel. As you see, the code simply refuses to create an options Panel. Allthough, the ability is obviously there. So, to answer your question: forget it, Google doesn't want you to use that optionsPanel anymore.
// Don't open an options panel for honeycomb apps on xlarge devices.
// (The app should be using an action bar for menu items.)
if (st.featureId == FEATURE_OPTIONS_PANEL) {
Context context = getContext();
Configuration config = context.getResources().getConfiguration();
boolean isXLarge = (config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) ==
Configuration.SCREENLAYOUT_SIZE_XLARGE;
boolean isHoneycombApp = context.getApplicationInfo().targetSdkVersion >=
android.os.Build.VERSION_CODES.HONEYCOMB;
if (isXLarge && isHoneycombApp) {
return;
}
}
I cannot understand the forcing blocking Menu button usage as well. However, the following trick helped me with showing a menu on "restricted" types of devices.
First of all we need to define, whether we require the following hack or not.
boolean requireDirtyMenuButtonHack = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && (activity.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_XLARGE) > 0;
Then:
protected final OnClickListener mMenuButtonClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
if (requireDirtyMenuButtonHack) {
Configuration config = getContext().getResources().getConfiguration();
config.screenLayout &= ~Configuration.SCREENLAYOUT_SIZE_XLARGE;
config.screenLayout |= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
getActivity().openOptionsMenu();
}
};
Don't forget to cleanup! (don't know if it's neccessary, but better to play kind)
public void onPrepareOptionsMenu(Menu menu) {
if (requireDirtyMenuButtonHack) {
Configuration config = getContext().getResources().getConfiguration();
config.screenLayout &= ~Configuration.SCREENLAYOUT_SIZE_LARGE;
config.screenLayout |= Configuration.SCREENLAYOUT_SIZE_XLARGE;
}
//do the preparing...
}
I'm not sure how relevant this is, but this forum post I found seems to be answerring the same question you have.
I hope this post is relevant enough to solve your problem.
Good luck!
android:targetSdkVersion="10" in manifest helped me.
openOptionsMenu() works as expected now on ICS+. In addition, there is "overflow" menu button appears at the bottom of screen (on device buttons panel).
ps: I use NoTitleBar theme (NoActionBar for sdk 11 and higher) +ViewPagerIndicator by Jake Wharton.
This worked for me, my code is pretty similar to yours, and what I want to do is from a button in the action bar, open the overflow menu:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_home_about:
dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.customdialog);
dialog.show();
break;
default:
case R.id.menu_home_refresh:
updateLists(true);
break;
case R.id.menu_home_menu:
new Handler().postDelayed(new Runnable() {
public void run() {
openOptionsMenu();
}
}, 0);
return true;
}
return false;
}
Do you mean you want to show a button on the right side of the action bar?
Here is how I did mine:
res/menu/main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_share"
android:title="Logout"
android:icon="#android:drawable/ic_lock_power_off"
android:orderInCategory="1"
android:showAsAction="always" />
</menu>
Activity
1) take note of the ActionBarActivity; 2) MenuInflater in onCreateOptionsMenu 3) onOptionsItemsSelected (I think you need to return super.onOptionsItemSelected(item) )
public class BaseActivity extends ActionBarActivity {
....
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_share:
//Do something
break;
}
return super.onOptionsItemSelected(item);
}
If you are using your custom toolbar, you can try:
toolBar.showOverflowMenu();
Related
I have used this guide from Google and this tutorial to produce my own contextual action bar.
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) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.annotation_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
}
// Called when the user selects a contextual menu item
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.custom_button:
// do some stuff
break;
case R.id.custom_button2:
// do some other stuff
break;
default:
// This essentially acts as a catch statement
// If none of the other cases are true, return false
// because the action was not handled
return false;
}
finish(); // An action was handled, so close the CAB
return true;
}
// Called when the user exits the action mode
#Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
This menu is designed to appear when the user selects text, so it overrides the native copy/paste menu. Now I get to my issue.
Because I am overriding functions for text selection, I also added a LongClickListener to a WebView and implemented the onLongClick(View v) method so I can detect when users make the selection.
myWebView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
if (mActionMode != null) {
return false;
}
mActionMode = startActionMode(mActionModeCallback);
v.setSelected(true);
return true;
}
});
When I long click, I see my custom menu appear, but no text is highlighted.I need to have the text selection functionality; without it, my menu is pointless.
How do I override onLongClick(View v), but maintain the text selection provided by Android?If that is not possible, can I make the call to startActionMode(mActionModeCallback) somewhere else so that text will be selected as normal, but my custom menu will also appear?If neither of those are possible... help.
THERE IS AN EASIER WAY! See update below :D
For the sake of completeness, here is how I fixed the problem:
I followed the suggestion according to this answer, with a little more tweaking to more closely match the overridden code:
public class MyWebView extends WebView {
private ActionMode mActionMode;
private mActionMode.Callback mActionModeCallback;
#Override
public ActionMode startActionMode(Callback callback) {
ViewParent parent = getParent();
if (parent == null) {
return null;
}
mActionModeCallback = new CustomActionModeCallback();
return parent.startActionModeForChild(this, mActionModeCallback);
}
}
Essentially, this forces your customized CAB to appear instead of the Android CAB. Now you have to modify your callback so that the text highlight will go away along with the CAB:
public class MyWebView extends WebView {
...
private class CustomActionModeCallback implements ActionMode.Callback {
...
// Everything up to this point is the same as in the question
// Called when the user exits the action mode
#Override
public void onDestroyActionMode(ActionMode mode) {
clearFocus(); // This is the new code to remove the text highlight
mActionMode = null;
}
}
}
That's all there is to it. Be aware that as long as you are using MyWebView with the overridden startActionMode there is NO WAY to get the native CAB (the copy/paste menu, in the case of a WebView). It may be possible to implement that sort of behavior, but that is not the way this code works.
UPDATE: There is a much easier way to do this! The above solution works well, but here is an alternative, easier way.
This solution provides less control over the ActionMode, but it requires far less code than the above solution.
public class MyActivity extends Activity {
private ActionMode mActionMode = null;
#Override
public void onActionModeStarted(ActionMode mode) {
if (mActionMode == null) {
mActionMode = mode;
Menu menu = mode.getMenu();
// Remove the default menu items (select all, copy, paste, search)
menu.clear();
// If you want to keep any of the defaults,
// remove the items you don't want individually:
// menu.removeItem(android.R.id.[id_of_item_to_remove])
// Inflate your own menu items
mode.getMenuInflater().inflate(R.menu.my_custom_menu, menu);
}
super.onActionModeStarted(mode);
}
// This method is what you should set as your item's onClick
// <item android:onClick="onContextualMenuItemClicked" />
public void onContextualMenuItemClicked(MenuItem item) {
switch (item.getItemId()) {
case R.id.example_item_1:
// do some stuff
break;
case R.id.example_item_2:
// do some different stuff
break;
default:
// ...
break;
}
// This will likely always be true, but check it anyway, just in case
if (mActionMode != null) {
mActionMode.finish();
}
}
#Override
public void onActionModeFinished(ActionMode mode) {
mActionMode = null;
super.onActionModeFinished(mode);
}
}
Here is an example Menu to get you started:
<!-- my_custom_menu.xml -->
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/example_item_1"
android:icon="#drawable/ic_menu_example_1"
android:showAsAction="always"
android:onClick="onContextualMenuItemClicked"
android:title="#string/example_1">
</item>
<item
android:id="#+id/example_item_2"
android:icon="#drawable/ic_menu_example_2"
android:showAsAction="ifRoom"
android:onClick="onContextualMenuItemClicked"
android:title="#string/example_2">
</item>
</menu>
That's it! You're done! Now your custom menu will show up, you don't have to worry about the selection, and you barely have to concern yourself with the ActionMode lifecycle.
This works nearly flawlessly with a WebView that occupies its entire parent Activity. I am not sure how well it will work if there are multiple Views within your Activity at one time. It will likely require some tweaking in that case.
The way I did something similar was to only override the onTouchListener and to invoke a GestureDetector to detect when the WebView was long-pressed and do what I wanted from there. Here's some sample code that allows you to catch long-press events without sacrificing text-selection in the WebView. Hopefully this helps.
#Override
protected void onCreate(Bundle savedInstanceState) {
WebView mWebView = (WebView) findViewById(R.id.myWebView);
GestureDetector mGestureDetector = new GestureDetector(this, new CustomGestureListener());
mWebView.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View view, MotionEvent arg1) {
//Suggestion #1 - this just lets the touch to be handled by the system but allows you to detect long presses
mGestureDetector.onTouchEvent(arg1);
return false;
//Suggestion #2 - this code will only let the touch be handled by the system if you don't detect a long press
return mGestureDetector.onTouchEvent(arg1);
}
});
}
private class CustomGestureListener extends SimpleOnGestureListener {
#Override
public void onLongPress(MotionEvent e) {
//do stuff
}
}
I want to add a new button to the ActionBar. When I click it, it does a specific Action. So I don't want a button that, after being pressed, opens a sub-menu (like the classic 3-dot menu).
I created a new button, this:
<item android:id="#+id/action_refresh"
android:icon="#drawable/refresh"
android:title="#string/refresh_string"
android:showAsAction="always"/>
and it's shown on the ActionBar, but if I click it, naturally, it doesn't do anything.
How can I manage to get an Action just pressing it?
Thanks!
You Need to override onOptionsItemSelected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_refresh:
//do your stuff
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Same as Homo sapiens's answer, but with an if-structure.
Add this method to your Activity class:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == R.id.action_refresh)
{
// do your stuff
return true;
}
else if (item.getItemId() == R.id.otherItem)
{
// do other stuff
return true;
}
// ...
else
{
return super.onOptionsItemSelected(item);
}
}
I'm developing a weather app, but I have a problem when displaying an optionsMenu with some smartphones. I want to display a menu with 3 options, but on the HTC one X, only 2 options are displayed.
The problem is that the black bar at the bottom of the screen hides the third option of my menu.
Here is a screenshot of the problem : (we should see "Recharger", "Voir cette image" and the last option : "Autres cartes")
The code :
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,100,0,m_res.getStringArray(R.array.menu_reload)[Commons.currentLanguage]).setIcon(R.drawable.reload);
menu.add(0,101,0,m_res.getStringArray(R.array.menu_view_single)[Commons.currentLanguage]).setIcon(R.drawable.see);
if(m_model.getUrl2().size() !=0)
menu.add(0,102,0,m_res.getStringArray(R.array.menu_more)[Commons.currentLanguage]).setIcon(R.drawable.more);
SubMenu moreMaps = menu.addSubMenu(0,103,0,m_res.getStringArray(R.array.menu_others_maps)[Commons.currentLanguage]).setIcon(R.drawable.france);
if(m_time == 1)
{
for(int iBoucle = 0 ; iBoucle < m_model.getNames1().size() ; iBoucle++)
moreMaps.add(1,iBoucle,1,m_model.getNames1().get(iBoucle));
}
else
{
for(int iBoucle = 0 ; iBoucle < m_model.getNames2().size() ; iBoucle++)
moreMaps.add(1,iBoucle,1,m_model.getNames2().get(iBoucle));
}
onContextItemSelected(moreMaps.getItem());
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 100:
if(Commons.getNetworkState() || Commons.getWifiState())
{
eraseData();
loadingData();
}
else
Commons.getConnectivityErrorMessage(m_Context);
return true;
case 101:
Intent sia = new Intent(m_Context, SingleImageActivity.class);
sia.putExtra("url", correctURL(m_urlImages.get(m_step)));
sia.putExtra("choiceNumber", -1);
if(m_textIsHour)
{
if(m_time == 1)
sia.putExtra("title", m_model.getNames1().get(m_mode)+" - "+m_model.getHours1().get(m_step)+"h");
else
sia.putExtra("title", m_model.getNames2().get(m_mode)+" - "+m_model.getHours2().get(m_step)+"h");
}
startActivity(sia);
break;
case 102:
if(Commons.getNetworkState() || Commons.getWifiState())
{
Intent modelSubList = new Intent(m_Context, ModelViewActivity.class);
modelSubList.putExtra("model",m_modelNumber);
modelSubList.putExtra("mode",m_mode);
if(m_time == 1)
modelSubList.putExtra("time",2);
else
modelSubList.putExtra("time",1);
startActivity(modelSubList);
finish();
}
else
{
Commons.getConnectivityErrorMessage(m_Context);
}
break;
case 103:
break;
default:
if(Commons.getNetworkState() || Commons.getWifiState())
{
Intent otherMapsIntent = new Intent(m_Context, ModelViewActivity.class);
otherMapsIntent.putExtra("model",m_modelNumber);
otherMapsIntent.putExtra("mode",item.getItemId());
otherMapsIntent.putExtra("time",m_time);
startActivity(otherMapsIntent);
finish();
return true;
}
else
{
Commons.getConnectivityErrorMessage(m_Context);
return false;
}
}
return false;
}
This code is showing three menu options on the US and European One X phones I have here:
https://github.com/lnanek/Misc/tree/master/OneXFullscreenMenuTest
Can you try it on your device? If it doesn't work, then we know it is the software on your device. If it does work, then it could be something in your code.
Can you post more about your situation? I'm particularly interested in the code for how you turn on fullscreen and your AndroidManifest.xml (for things like the uses-sdk line and theme settings).
I don't see anything wrong offhand in the code you posted, but could you comment out this line just for a quick check?
if(m_model.getUrl2().size() !=0)
If that check isn't passing, then there should be only two menu items, not three. So commenting it out would be a quick check (or watching in a debugger to see if all three adds are called, of course).
Please note that onCreateOptionsMenu is only called the very first time the Activity instance shows the options menu, as opposed to onPrepareOptionsMenu which is called every time. So if adding that third menu option is not done the very first time, then it never will be.
Note: PopupMenu is available with API level 11 and higher.
http://developer.android.com/guide/topics/ui/menus.html#PopupMenu
With that in mind, how can I link a menu to a View on screen with ABS and the compatibility library? I have a menu imageview on one of my layouts that I would like to provide a universal menu option. Basically by having an imageview that pops up the menu, the same system is in use regardless of device or android version. But if popup only exists on Gingerbread and later, is there another way to link an ImageView (+ click listener) to open up my menu?
Here's how I ended up solving this:
ImageView menuImg = (ImageView) activity.findViewById(R.id.menuImageView);
menuImg.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
activity.openOptionsMenu(); //This is the key method!
}
});
public boolean onCreateOptionsMenu(Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.demographics:
return true;
case R.id.settings:
Log.v("v", "settings clicked");
return true;
default:
return false;
}
}
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!