javaandroidbuttonhardware

Check if an Android device has a certain hardware button in code


I need to check if a certain android device has a hardware button in code. For example only some phones have the search button. So how do I check if a device has a hardware button(Search, camera, d-pad, etc) or not?


Solution

  • You can use PackageManager.hasSystemFeature().
    Example:

    boolean hasCamera = 
            getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA); 
    

    You can also get some of the features which are not testable by the PackageManager via the Configuration, e.g. the DPAD.

    Configuration c = getResources().getConfiguration();
    if(c.navigation == Configuration.NAVIGATION_DPAD)
          hasDpad = true;
    

    The only exception is the search button. There was a question here a few days ago, asking basically the same. I don't remember any answer and I don't know a way to detect the search button, since it's not in the list of features. (Edit: There you go, possible duplicate thread is the one i mentioned here)