I am a little confused. I want to show the remaining items of the action bar inside the three dot item. But i always lose it when the number of items increases ! after googling a little, i found this "useful" method : ASMUIRTI ANSWER
private void getOverflowMenu() {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
I tested it and that work as i want. Unfortunately, triing to better understand what does this method do, i found that it's an awful hack that breaks consistency with the rest of the apps on the platform (adamp's comment)
Can somebody tell me why it's a hack, and what is the best solution ?
The Overflow menu is not shown for devices with a menu button.
The code you used will work fine on a more recent device and the overflow (three dots) will be displayed. As intented, these are not shown when there is a menu button
There is no way without a dirty hack that will break soon or later to force these dots to be dsiplayed on all devices
It's simple. Some devices have menu buttons others don't. On devices with menu buttons the overflowing action items are shown when pressing the menu button and on devices without they are shown when pressing the three dots. That's how the device manufacturer designed their devices and any attempt to force them to do it differently is regarded a "hack". It's inconsistent because other apps on the same device do it differently.
It all comes down to the question whether you want to have consistency for a single app across different devices or consistency for all apps on a single device. Because app development should be about users in the end, IMO the consistency on a single device (for one user) is what really counts.
The best way to deal with this is to use the android:showAsAction tags and let Android decide, how to display the actions in the ActionBar, whether an overflow menu is needed or not and if yes how the user accesses the overflowing action items.
So, turns out it's pretty simple, I recently implemented in my app.
The items that need to be shown in the overflow menu, nest them under one menu item as follows:
Now, edit the main activity file as follows: