0


As the title says i have a problem implementing search widget in some Fragment. My problem is that when i use inflater to inflate custom view inside a onCreateOptionsMenu and after that I find SearchView it is always null!
If anyone had same or similar problem I would be grateful for some explanation! :)
I banging my head on this for the last two days... Here is the layout:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@+id/menu_search"
        android:title="Search"
        android:icon="@drawable/abs__ic_search"
        android:showAsAction="always|collapseActionView"
        android:orderInCategory="0"
        android:actionViewClass="com.actionbarsherlock.widget.SearchView"/>

    <item android:id="@+id/menu_settings"
        android:title="Settings"
        android:icon="@drawable/jk_uma_button_settings_normal"
        android:showAsAction="always"
        android:orderInCategory="1"
         />
</menu>

Code

Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_explore, menu);
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus){
                Log.i(TAG, "======================FOCUS TRUE============");
            }else{
                Log.i(TAG, "======================FOCUS FALSE===========");
            }
        }

    });
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i(TAG, "onCreate");
    api_key = Configuration.get_prefereence_string(getActivity(), "user_activation_key", null);
    user_id = Configuration.get_prefereence_string(getActivity(), "user_id", null);
    setHasOptionsMenu(true);
}
Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99
Jovan
  • 1,741
  • 4
  • 19
  • 38

1 Answers1

4

As far as I know, onCreateOptionsMenu is called during onCreate, but you enable options menu feature in the end of the method. Try to setHasOptionsMenu(true) in the constructor of the Fragment.

public YourFragment() {
    this.setHasOptionsMenu(true);
}

To catch hardware search button press from the Activity

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_SEARCH) {
        this.onSearchClicked();
        return true;
    }
    return false;
}
Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99
  • You are a life savior!!! One more question...How to catch when user click a search button on keyboard? He writes something in a search view and clicks search on keboard...how to catch it? – Jovan Mar 12 '13 at 14:28
  • I found it...it was setOnQueryTextListener...I skip it somehow... Thanks again man!!!! – Jovan Mar 12 '13 at 14:31
  • Updated the answer for catching the hardware search button. – Yaroslav Mytkalyk Mar 12 '13 at 14:32
  • One question more: When i catch search on soft keyboard, keyboard is still visible and text is still in search edit box. Is this on me to remove or system have some method to call... I know that I'm boring :D – Jovan Mar 12 '13 at 14:40
  • 1
    The keyboard is dismissed when you press back button. But you can hide the soft keyboard manually if you wish http://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard – Yaroslav Mytkalyk Mar 12 '13 at 15:53