Search code examples
androidbuttonandroid-4.0-ice-cream-sandwich

How to detect virtual buttons (Android 4)?


I would like to detect if the phone running my Android application uses hardware buttons or virtual buttons (Galaxy Nexus).

Is there a way to get this information?


Solution

  • Since API level 14 you can use ViewConfiguration.hasPermanentMenuKey() to detect if a device has a permanent menu key.

    Generally, virtual and physical buttons are mutually exclusive, so this would help you. Example:

    public class MyActivity extends Activity {
    
        // ...
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            boolean hasPermanentMenuKey = ViewConfiguration.get(this).hasPermanentMenuKey();
            boolean hasVirtualKeys = !hasPermanentMenuKey;
            String message = hasVirtualKeys ? "This device has virtual menu keys" : "This device has physical keys";
            Toast.makeText(this, message, Toast.LENGTH_LONG).show();
        }
        // ...
    
    }
    

    WARNING: There are some cases in which this won't help you. For example, say a manufacturer decides to use virtual keys along with physical menu keys; then this method wouldn't help you. Another case would be if the phone has no virtual keys and no physical menu key (e.g. Samsung S5).