Skip to content

Instantly share code, notes, and snippets.

@stefanrusek
Created November 8, 2014 03:37
Show Gist options
  • Save stefanrusek/edc51afbf5a041893c18 to your computer and use it in GitHub Desktop.
Save stefanrusek/edc51afbf5a041893c18 to your computer and use it in GitHub Desktop.
This is a work around for the appcompat v21 on android 4.2.2.
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
menu = new MenuWrapper(menu) {
private MenuItem fix(MenuItem item) {
try {
Field f = item.getClass().getDeclaredField("mEmulateProviderVisibilityOverride");
f.setAccessible(true);
f.set(item, Boolean.FALSE);
} catch (Throwable e) {
e.printStackTrace();
}
return item;
}
@Override
public MenuItem add(CharSequence title) {
return fix(super.add(title));
}
@Override
public MenuItem add(int titleRes) {
return fix(super.add(titleRes));
}
@Override
public MenuItem add(int groupId, int itemId, int order, CharSequence title) {
return fix(super.add(groupId, itemId, order, title));
}
@Override
public MenuItem add(int groupId, int itemId, int order, int titleRes) {
return fix(super.add(groupId, itemId, order, titleRes));
}
};
}
inflater.inflate(R.menu.text_selection, menu);
import android.content.ComponentName;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
public abstract class MenuWrapper implements Menu {
private final Menu menu;
public MenuWrapper(Menu menu) {
this.menu = menu;
}
@Override
public MenuItem add(CharSequence title) {
return menu.add(title);
}
@Override
public MenuItem add(int titleRes) {
return menu.add(titleRes);
}
@Override
public MenuItem add(int groupId, int itemId, int order, CharSequence title) {
return menu.add(groupId, itemId, order, title);
}
@Override
public MenuItem add(int groupId, int itemId, int order, int titleRes) {
return menu.add(groupId, itemId, order, titleRes);
}
@Override
public SubMenu addSubMenu(CharSequence title) {
return menu.addSubMenu(title);
}
@Override
public SubMenu addSubMenu(int titleRes) {
return menu.addSubMenu(titleRes);
}
@Override
public SubMenu addSubMenu(int groupId, int itemId, int order, CharSequence title) {
return menu.addSubMenu(groupId, itemId, order, title);
}
@Override
public SubMenu addSubMenu(int groupId, int itemId, int order, int titleRes) {
return menu.addSubMenu(groupId, itemId, order, titleRes);
}
@Override
public int addIntentOptions(int groupId, int itemId, int order, ComponentName caller, Intent[] specifics, Intent intent, int flags, MenuItem[] outSpecificItems) {
return menu.addIntentOptions(groupId, itemId, order, caller, specifics, intent, flags, outSpecificItems);
}
@Override
public void removeItem(int id) {
menu.removeItem(id);
}
@Override
public void removeGroup(int groupId) {
menu.removeGroup(groupId);
}
@Override
public void clear() {
menu.clear();
}
@Override
public void setGroupCheckable(int group, boolean checkable, boolean exclusive) {
menu.setGroupCheckable(group, checkable, exclusive);
}
@Override
public void setGroupVisible(int group, boolean visible) {
menu.setGroupVisible(group, visible);
}
@Override
public void setGroupEnabled(int group, boolean enabled) {
menu.setGroupEnabled(group, enabled);
}
@Override
public boolean hasVisibleItems() {
return menu.hasVisibleItems();
}
@Override
public MenuItem findItem(int id) {
return menu.findItem(id);
}
@Override
public int size() {
return menu.size();
}
@Override
public MenuItem getItem(int index) {
return menu.getItem(index);
}
@Override
public void close() {
menu.close();
}
@Override
public boolean performShortcut(int keyCode, KeyEvent event, int flags) {
return menu.performShortcut(keyCode, event, flags);
}
@Override
public boolean isShortcutKey(int keyCode, KeyEvent event) {
return menu.isShortcutKey(keyCode, event);
}
@Override
public boolean performIdentifierAction(int id, int flags) {
return menu.performIdentifierAction(id, flags);
}
@Override
public void setQwertyMode(boolean isQwerty) {
menu.setQwertyMode(isQwerty);
}
}
@mypplication
Copy link

For android 4.2.2 ?? With Build.VERSION.SDK_INT == Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1 ?

@christoff
Copy link

My thoughts exactly... Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment