Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 4.0 detect if the user has hardware back/home keys

Tags:

android

Is there a way in ICS / HC to detect if the user has access to a hardware menu key?

Depending on the configuration I'm hoping to change how the actionbar is populated, e.g:

If the user has a physical home button, reduce the number of buttons shown in the actionbar.

Thanks, Laurence

like image 661
Ljdawson Avatar asked Feb 08 '12 23:02

Ljdawson


1 Answers

Took me some time but I've found a more reliable way than relying on hasPermanentMenuKey() which doesn't work for newer phones like the HTC One which have no menu key but do have home & back keys so don't need (or show) the soft navigation bar. To get around this try the following code which checks for a back button too:

boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);

if(!hasMenuKey && !hasBackKey) {
    // Do whatever you need to do, this device has a navigation bar
}
like image 114
philask Avatar answered Oct 18 '22 17:10

philask