4

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?

Daniel Ryan
  • 6,976
  • 5
  • 45
  • 62

3 Answers3

9

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)

  • Awesome. Though shame about the search button, I'll tick this and just watch the other thread. – Daniel Ryan Sep 05 '11 at 01:34
  • 1
    I don't think this answer can be accepted, this will only check if there is a camera installed on the device and NOT if there is a camera hardware button installed. AFAIK there is no such feature in the SDK. – user961186 Sep 23 '11 at 12:55
5

Here's a good clean way of checking if the Hardware "Menu" button is present:

ViewConfiguration.get(context).hasPermanentMenuKey();

From: https://stackoverflow.com/a/9481965 See also: http://developer.android.com/reference/android/view/ViewConfiguration.html#hasPermanentMenuKey()

Community
  • 1
  • 1
Jimmy
  • 51
  • 1
  • 1
1

I know this post is over a year old, but with it still showing up in Google searches (on the first page). I figured, I would post a slight update or suggestion with what I am using in-case someone like me is searching for the same thing.

public boolean onKeyDown(int keyCode, KeyEvent event) { 
           if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { 
               Toast.makeText(this,"VOL Down", Toast.LENGTH_LONG).show();
               return true;
           } else if(keyCode == KeyEvent.KEYCODE_SEARCH){
               Toast.makeText(this,"search", Toast.LENGTH_LONG).show();
               return false;
           }else{
               Toast.makeText(this,"Vol Up", Toast.LENGTH_LONG).show();
               return true;
               //return super.onKeyDown(keyCode, event); 
           }
    }

I have placed out side the onCreate().. This works fine on my Thunderbolt (Rooted running MikMik GingerGrits)

acrichm
  • 179
  • 1
  • 4
  • 16
  • Originally, I needed to check if the device had a hardware button before the app is shown. This will only work once that hardware button is pressed. But thanks for sharing. – Daniel Ryan Jul 15 '13 at 21:40
  • Ya this is a working solution. Ya I checked it with HTC ADR6300. This is the best way to detect a hardware search button stroke. – Jashan PJ Aug 05 '13 at 05:53