9

I need help figuring out how to check if a device is equipped with the search hardware button or not. Is this possible?

EDIT: I'm talking about finding out if the device has the search hardware button or not. Simple question. Each android device has a set of hardware buttons; menu, home button, back button, and search button. But some devices is only equipped with some of them, not all.

EDIT 2: The reason why I ask is because I want to have a software button showing in my UI if the device is not equipped with a hardware button. I am using the searchable interface in my activity. I am not following the EditText / TextField approach.

ScratchMyTail
  • 281
  • 3
  • 10
  • if I m not going wrong, you told about device search hardware button or about some graphical view button? – user370305 Sep 01 '11 at 13:17
  • I thought it was a requirement for Android devices. – rds Sep 01 '11 at 14:59
  • Not all devices are equipped with hardware buttons. The new Galaxy Tab 10.1 does not have any of those buttons I mentioned before. – ScratchMyTail Sep 01 '11 at 17:24
  • In order to check hardware Menu. you can use ViewConfiguration.get(Splashy.this).hasPermanentMenuKey(); but it requires minimum Api 14, But can't help for search button – Xar-e-ahmer Khan Nov 05 '15 at 07:24

2 Answers2

1

I don't think you need to detect if it actually has a search hardware button. The framework will help you out here (though, I'm sure this process will be simplified once Ice Cream Sandwich is released)

Currently, the only devices that won't have hardware search are Honeycomb tablets. So, by using android:targetSdkVersion="11" (or higher), adding implements OnQueryTextListener to your Fragment or Activity, and then adding something like:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.notebooks_menu, menu);
    final SearchView sv = new SearchView(getActivity());
    sv.setOnQueryTextListener(this);
    menu.findItem(R.id.search_notebooks).setActionView(sv);
}

You will essentially solve the problem. Now, to get it working on pre-Honeycomb devices, you may need to use the compatibility library, or use reflection or some other guards in your code.

EDIT

The Samsung Galaxy S II does not have a dedicated hardware search button, but if you hold the menu button down for a couple of seconds, it will begin acting as a hardware search button.

Jon Willis
  • 6,993
  • 4
  • 43
  • 51
  • You are correct, Honeycomb tablets does not have the search hardware button. Neither does the new Samsung Galaxy SII. I think it's sad to see companies making their own twist on their devices. It makes it even more challenging supporting all Android devices. The Galaxy 10.1 even has a dedicated screenshot button which I find strange. The best solution is probably implementing a menu containing the search button as you mentioned, this will of course work on all devices, at least those equipped with the menu button (which I think all Android devices have). Thanks for input. – ScratchMyTail Sep 19 '11 at 07:04
  • 1
    Please see my edit about the SGS2's menu button becoming search. I agree that it is not *great* (but you can't really blame them for trying to innovate) when hardware manufacturers pull stuff like this, as it breaks user and developer expectations at the least. – Jon Willis Sep 19 '11 at 11:17
  • Thanks Jon. Btw I think support library doesn't come with SearchView. So we need to use default search dialog in older SDKs. Here is [documentation](http://developer.android.com/training/search/backward-compat.html). –  Nov 27 '12 at 03:01
-2

Better way you can do usign two ways

  1. put the search button and call to onSearchRequested();

  2. Second way on click the editText put android:imeOptions="actionSearch" so you need to
    check for key

    searchBox.setOnEditorActionListener(new OnEditorActionListener()
    {
        public boolean onEditorAction(TextView textView, int id,KeyEvent event) 
        {
        if (id == EditorInfo.IME_ACTION_SEARCH)
            {
                 //do what ever you want
            }
        return true;
        }
    });
    

Hope this will help you.

Sameer Z.
  • 3,265
  • 9
  • 48
  • 72
  • 1
    I'm not asking how to get the searchfield active. I'm asking how we can find out wether a device is equipped with a hardware search or not. The idea is simple. If the device does NOT have a hardware search button I must show a software button. If it does have a hardware button I will not show the software button. Thats my plan. – ScratchMyTail Sep 01 '11 at 17:22
  • 1
    I have vote down the answer because it does not well justify the question and the solution required – Arpit Garg Apr 03 '12 at 09:48