Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Action Bar Sherlock SearchView not expanding on click of it

I have one Sherlock Fragment Activity from which i am setting the different sherlock fragments using fragment pager adapter.

Now to show the searchview in each of the fragment i have placed this method in onCreate of Fragment::

This method will show menu items in the ActionBar of the fragment.

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

I have now also set the onCreateOptionMenu() like this ::

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) 
{
    super.onCreateOptionsMenu(menu, inflater);

    inflater.inflate(R.menu.menu, menu);
    final MenuItem searchItem = menu.findItem(R.id.menu_item_search);
    final SearchView searchView = (SearchView) searchItem.getActionView();
    searchView.setQueryHint("Search Here");
}

Now the strange things happens that in my first fragment i have done the same thing and the searchview get opened successfully.

If

  • i go to the first fragment,open the searchview then go to the second
  • fragment the searchview will get opened.

Same thing happens for third fragment

  • go to 3rd fragment,
  • open searchview,
  • back to the second frgment,searchview get opened.

But if i go to directly on the second fragment the searchview not get opened.

Hope i am clear.

Any clue about this? Any suggestion/links will be appreciated..

Thanks in Advance...

EDIT ::

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

   <item
        android:id="@+id/menu_item_search"
        android:icon="@drawable/abs__ic_search"
        android:showAsAction="ifRoom|collapseActionView"
        android:actionViewClass="com.actionbarsherlock.widget.SearchView" 
        android:title="Search Products">
    </item> 
    <item
        android:id="@+id/root_menu"
        android:icon="@drawable/abs__ic_menu_moreoverflow_normal_holo_light"
        android:showAsAction="always"
        android:title="More">
        <menu>
            <item
                android:id="@+id/menu_Home"
                android:icon="@drawable/home"
                android:showAsAction="never"
                android:title="Home"/>
            <item
                android:id="@+id/menu_favourite"
                android:icon="@drawable/favourite"
                android:showAsAction="never"
                android:title="Favourite"/>
            <item
                android:id="@+id/menu_Balance"
                android:icon="@drawable/balance"
                android:showAsAction="never"
                android:title="Balance"/>
            <item
                android:id="@+id/menu_logout"
                android:icon="@drawable/btn_logout"
                android:showAsAction="never"
                android:title="Logout"/>
        </menu>
    </item>

</menu>

Logcat ::

    12-06 16:45:26.401: E/AndroidRuntime(1581): FATAL EXCEPTION: main
12-06 16:45:26.401: E/AndroidRuntime(1581): java.lang.ClassCastException: com.mobifin.subscriber.ButtonPayActivity
12-06 16:45:26.401: E/AndroidRuntime(1581):     at com.mobifin.subscriber.home.BillPayFragment.onCreateOptionsMenu(BillPayFragment.java:101)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.support.v4.app.Watson.onCreatePanelMenu(Watson.java:55)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:560)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at com.actionbarsherlock.internal.ActionBarSherlockCompat.preparePanel(ActionBarSherlockCompat.java:466)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at com.actionbarsherlock.internal.ActionBarSherlockCompat.dispatchInvalidateOptionsMenu(ActionBarSherlockCompat.java:265)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at com.actionbarsherlock.app.SherlockFragmentActivity.invalidateOptionsMenu(SherlockFragmentActivity.java:149)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at com.actionbarsherlock.app.SherlockFragmentActivity.supportInvalidateOptionsMenu(SherlockFragmentActivity.java:155)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.support.v4.app.Fragment.setMenuVisibility(Fragment.java:726)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:127)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.support.v4.view.ViewPager.populate(ViewPager.java:802)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1280)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.support.v4.view.ViewPager.computeScroll(ViewPager.java:1176)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewGroup.drawChild(ViewGroup.java:1562)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.View.draw(View.java:6883)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.View.draw(View.java:6883)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewRoot.draw(ViewRoot.java:1522)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.os.Looper.loop(Looper.java:130)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.app.ActivityThread.main(ActivityThread.java:3683)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at java.lang.reflect.Method.invokeNative(Native Method)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at java.lang.reflect.Method.invoke(Method.java:507)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at dalvik.system.NativeStart.main(Native Method)
like image 782
AndroidLearner Avatar asked Dec 06 '13 09:12

AndroidLearner


People also ask

How to set SearchView in android studio?

To add a SearchView widget to the app bar, create a file named res/menu/options_menu. xml in your project and add the following code to the file. This code defines how to create the search item, such as the icon to use and the title of the item.

How to make search View in android?

Android allows us to use the search functionality in our app by displaying the SearchView widget either in the ToolBar/ActionBar or inserting it into a layout. Android SearchView widget is available from Android 3.0 onwards. The SearchView is defined in the XML layout as shown below.


1 Answers

We generally use onCreateOptionsMenu() for creating Options Menu for fragments and activities. If you go through the documentation, there's another helper method called onPrepareOptionsMenu() which, as per the documentation says:

Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.

SearchView expanding/collapsing works fine when we are not instantiating fragments inside ViewPager or anything similar tab like switching scenarios. I've had the same problem once. I came up with using onPrepareOptionsMenu() to resolve my problem.

Just implement onPrepareOptionsMenu() in each of your fragments and inside of it, call onQueryTextChange("") passing "" as query string. It will do the trick to suggest the fragment container activity that this fragment wants to mind his own searching business.

OR , Not sure, but a call to invalidateOptionsMenu() on container activity could also come handy.

Let me know if that helps.

EDIT:

My Implementation:

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);

        SearchView searchView = new SearchView(getActivity());
        searchView.setQueryHint("Search for Events");
        searchView.setOnQueryTextListener(this);

        menu.add(0, ACTION_SEARCH, 0, "Search")
                .setIcon(R.drawable.ic_mailbox_search)
                .setActionView(searchView)
                .setOnActionExpandListener(menuExpandListener)
                .setShowAsAction(
                        MenuItem.SHOW_AS_ACTION_IF_ROOM
                    | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    }

@Override
    public void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        onQueryTextChange("");
    }

@Override
    public boolean onQueryTextSubmit(String query) {
        return true;
    }

    @Override
    public boolean onQueryTextChange(String newText) {

        if (mAdapter != null)
            mAdapter.getFilter().filter(newText);
        return false;
    }
like image 68
Purush Pawar Avatar answered Sep 17 '22 20:09

Purush Pawar