Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Programmatically detect if device has hardware menu button

I'm currently struggling with this issue. I need to check if the device, where the app is installed, has a hardware menu key. Because it is not existing on some devices like Galaxy Nexus, I'm showing it directly in the UI in this case.

I have already looked at PackageManager.hasSystemFeature(), but didn't find anything useful there.

Has anyone already done this?

like image 437
NiThDi Avatar asked Jan 28 '12 11:01

NiThDi


4 Answers

ViewConfiguration.get(context).hasPermanentMenuKey()

See ViewConfiguration#hasPermanentMenuKey() for more information. Note that this is only available for API level 14+ (Android 4.0 Ice Cream Sandwich or newer).

like image 64
fhucho Avatar answered Nov 16 '22 07:11

fhucho


if(Build.VERSION.SDK_INT <= 10 || (Build.VERSION.SDK_INT >= 14 &&    
                              ViewConfiguration.get(this).hasPermanentMenuKey()))
{
   // menu key is present
}
else
{
  //No menu key
}
like image 22
TheMan Avatar answered Nov 16 '22 08:11

TheMan


Even on devices running Honeycomb and later, the system will supply a “Menu button” for apps written for 2.x versions of Android. Only it’s called the “overflow menu”. So there’s no point checking whether there will be such a button or not—it will be there if it’s needed.

As a general guideline, you should check for specific functionality, not look at system/API version numbers. Use the ActionBar class if it’s available, otherwise fallback to the 2.x options menu.

Have you looked at Google’s action-bar tutorial? That makes it clearer what you should be doing.

like image 3
Lawrence D'Oliveiro Avatar answered Nov 16 '22 06:11

Lawrence D'Oliveiro


I think a possible and better solution is to add a own actionbar. So every device can see it and you don't have to check hardware configuration or Api version.

like image 1
kinghomer Avatar answered Nov 16 '22 07:11

kinghomer