4

I'm trying to figure out if an Android phone has hardware menu button, I've searched and found this method:

ViewConfiguration.get(getApplicationContext()).hasPermanentMenuKey();

But this doesn't seem to work in Android 2.1, and I'm trying to create an app that works on Android 2.1 and higher. Is it possible to detect if there is an hardware button on a phone with Android version less than 3.0?

halfer
  • 19,824
  • 17
  • 99
  • 186
Robin.v
  • 719
  • 3
  • 8
  • 16

3 Answers3

3

Every compatible 2.1 android device had a menu key as it was part of the CDD:

http://source.android.com/compatibility/2.1/android-2.1-cdd.pdf

See section 8.7:

"The Home, Menu and Back functions are essential to the Android navigation paradigm. Device implementations MUST make these functions available to the user at all times, regardless of application state."

Therefore, if the device is running android 2.1 it's safe to assume it has a menu key. If it's running a later version you can use the API you found.

Ian Newson
  • 7,679
  • 2
  • 47
  • 80
  • 1
    I understand this but I'm creating an app for 2.1 but it can also be used on higher/newer versions so if the version is 2.1 they all have a menu button but what if an user with version 3 or higher/newer uses the app how can I detect if it has a hardware menu button while my base version still is 2.1? – Robin.v Jun 07 '12 at 10:31
  • As long as your build target is set to Android 3 or higher, but your min sdk version is set to 2.1, you can still include Android 3 specific code. You just need to make sure you only call that code when running on an Android 3+ device. – Ian Newson Jun 07 '12 at 10:32
  • Alright, thanks guys! I'll give it a try. Thanks for your replies! – Robin.v Jun 07 '12 at 10:38
1

hasPermanentMenuKey() was introduced since API Level 14 because from Android 3.0 the devices were allowed not to have a menu key. so I assume that you can safely assume that a 2.1 device will have a menu key. Check the android documentatin on this for more. Android view Configuration hasPermanantMenuKey

Orlymee
  • 2,349
  • 1
  • 22
  • 24
  • 1
    I understand this but I'm creating an app for 2.1 but it can also be used on higher/newer versions so if the version is 2.1 they all have a menu button but what if an user with version 3 or higher/newer uses the app how can I detect if it has a hardware menu button while my base version still is 2.1? – Robin.v Jun 07 '12 at 10:31
  • I have the same problem. I am building my app with 2.3.3 API level 10. I cannot use hasPermanantKey(). – Karthik Andhamil Dec 28 '12 at 10:24
  • 1
    The huge problem is that from API level 11 (Android 3.0) devices do not require to have the menu button, but `hasPermanentMenuKey()` was not introduced until API level 14 (Android 4.0). So for API levels 11, 12 and 13 you are screwed. – cprcrack Apr 20 '13 at 14:42
  • I found `ViewConfiguration.get(this).hasPermanentMenuKey()` giving `false` for devices with hardware menu buttons (Note 4 with Android 4.4) and `true` for some devices without. It is not reliable. Yet, I don't have alternatives. – Luis A. Florit Jun 02 '18 at 19:37
0

hasPermanentMenuKey() became available at API level 14 (3.0). I would believe it is safe to assume there is a key on devices running below 3.0. Above 3.0 you can call this method to determine if you need to provide an alternative method.

cstrutton
  • 5,667
  • 3
  • 25
  • 32
  • API level 14 is actually ICS --- Android 4.0.1. [Here is a link to the definitive list.](http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) – David Given Feb 07 '13 at 16:46