Created
October 3, 2013 16:28
-
-
Save jgilfelt/6812732 to your computer and use it in GitHub Desktop.
A base Activity class that allows the compatibility menu button (AKA "the menu button of shame") to be displayed for applications whose targetSdkVersion >= 11. On devices with a hard menu key there is no effect. Useful if you want to launch some sort of in-app debug UI from an on-screen affordance without altering your application's user interfa…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.shame; | |
import android.annotation.TargetApi; | |
import android.app.Activity; | |
import android.os.Build; | |
import android.view.KeyEvent; | |
import android.view.ViewConfiguration; | |
public abstract class ShameActivity extends Activity { | |
/** | |
* Flag for a window belonging to an activity that responds to {@link KeyEvent#KEYCODE_MENU} | |
* and therefore needs a Menu key. For devices where Menu is a physical button this flag is | |
* ignored, but on devices where the Menu key is drawn in software it may be hidden unless | |
* this flag is set. | |
* | |
* (Note that Action Bars, when available, are the preferred way to offer additional | |
* functions otherwise accessed via an options menu.) | |
* | |
* {@hide} | |
*/ | |
private static final int FLAG_NEEDS_MENU_KEY = 0x08000000; | |
private boolean mCompatMenuKeyEnabled = false; | |
/** | |
* Enable the compatibility menu button on devices that don't have | |
* a permanent menu key. | |
* | |
* @param enabled true to enable the compatibility menu button, false to disable it. | |
*/ | |
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) | |
protected void setCompatMenuKeyEnabled(boolean enabled) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { | |
ViewConfiguration vc = ViewConfiguration.get(this); | |
if (!vc.hasPermanentMenuKey()) { | |
if (enabled) { | |
getWindow().addFlags(FLAG_NEEDS_MENU_KEY); | |
} else { | |
getWindow().clearFlags(FLAG_NEEDS_MENU_KEY); | |
} | |
mCompatMenuKeyEnabled = enabled; | |
} | |
} | |
} | |
@Override | |
public boolean dispatchKeyEvent(KeyEvent event) { | |
if (mCompatMenuKeyEnabled) { | |
if (event.getKeyCode() == KeyEvent.KEYCODE_MENU && event.getAction() == KeyEvent.ACTION_UP) { | |
onCompatMenuKeyPressed(); | |
// returning true creates an unfortunate side effect with the | |
// overflow menu, so it will still open here (if present) | |
return false; | |
} | |
} | |
return super.dispatchKeyEvent(event); | |
} | |
/** | |
* Override to implement a compatibility menu key press action. | |
*/ | |
protected abstract void onCompatMenuKeyPressed(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unfortunately that does not work on Android 5+.
So I added this: