JustLearningAgain
JustLearningAgain

Reputation: 2287

Search action item not showing edit control

I have a search action item associated with an AppCompatActivity. The search icon appears but when I click on it, it doesn't show the edit control allowing me to put in the search text. I've tried all the obvious issues but I just can't seem to get this to work.

This app loads a PDF a switches out the Toolbar based on user choices. The first toolbar shows a search item. Once this is selected it adds the searchNext and searchPrev buttons. All this worked until I switched to Material Design. I think the problem has something to do with replacing the menu but I'm not sure.

Here is the manifest:

<activity
    android:name="com.mupdf.MuPDFActivity"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"
    android:configChanges="orientation|screenSize|keyboardHidden"
    android:label="@string/app_name" >
</activity>

Here is the menu resources:

MAIN MENU:

<item android:id="@+id/pdf_action_search"
    android:title="@string/pdf_action_search"
    android:icon="@drawable/ic_pdf_action_search"
    android:orderInCategory="100"
    app:showAsAction="ifRoom"/>

<item android:id="@+id/pdf_action_export"
      android:title="@string/pdf_action_export"
      android:icon="@drawable/ic_pdf_action_export"
      android:orderInCategory="100"
      app:showAsAction="ifRoom">

    <menu>
        <item
            android:id="@+id/pdf_action_share"
            android:title="@string/pdf_action_share"
            app:showAsAction="ifRoom"
            app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
        <item
            android:id="@+id/pdf_action_print"
            android:title="@string/pdf_action_print"
            app:showAsAction="always|withText"/>
    </menu>

</item>
<item android:id="@+id/pdf_action_tools"
      android:title="@string/pdf_action_tools"
      android:icon="@drawable/ic_pdf_action_tools"
      android:orderInCategory="100"
      app:showAsAction="ifRoom">

    <menu>
        <item
            android:id="@+id/pdf_action_highlight"
            android:title="@string/pdf_action_highlight"
            app:showAsAction="always|withText"/>
        <item
            android:id="@+id/pdf_action_underline"
            android:title="@string/pdf_action_underline"
            app:showAsAction="always|withText"/>
        <item
            android:id="@+id/pdf_action_strikeout"
            android:title="@string/pdf_action_strikeout"
            app:showAsAction="always|withText"/>
        <item
            android:id="@+id/pdf_action_pen"
            android:title="@string/pdf_action_pen"
            app:showAsAction="always|withText"/>
    </menu>

</item>

REPLACEMENT MENU:

 <?xml version="1.0" encoding="utf-8"?>
 <menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:id="@+id/group_search_mode">
    <item
        android:id="@+id/pdf_menu_search_item"
        android:title="@string/search"
        android:icon="@drawable/ic_pdf_action_search"
        app:showAsAction="always|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView"/>
    <item
        android:id="@+id/pdf_menu_search_prev"
        android:title="@string/search_prev"
        android:icon="@drawable/ic_pdf_action_search_prev"
        app:showAsAction="always" />
    <item
        android:id="@+id/pdf_menu_search_next"
        android:title="@string/search_next"
        android:icon="@drawable/ic_pdf_action_search_next"
        app:showAsAction="always" />
</group>

This is activity:

 import android.support.v4.view.MenuItemCompat;
 import android.support.v7.app.ActionBar;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.view.ActionMode;
 import android.support.v7.widget.SearchView;
 import android.support.v7.widget.ShareActionProvider;
 import android.support.v7.widget.Toolbar;
 import android.text.method.PasswordTransformationMethod;
 import android.view.Menu;
 import android.view.MenuItem;

 public class MuPDFActivity extends AppCompatActivity implements      FilePicker.FilePickerSupport
 {
/* The core rendering instance */
enum TopBarMode {Main, Search, Annot, Delete, More, Accept};
enum AcceptMode {Highlight, Underline, StrikeOut, Ink, CopyText};

private TopBarMode   mTopBarMode = TopBarMode.Main;
private AcceptMode   mAcceptMode;
private SearchTask   mSearchTask;

private SearchView mSearchView;
private MenuItem mSearchMenuPrev;
private MenuItem mSearchMenuNext;
private String mSearchString;
private ActionBar mActionBar;
private Toolbar toolbar;

// ***************************************************************************************************
//
//  onCreate
//
// ***************************************************************************************************
@Override
public void onCreate(Bundle savedInstanceState)
{
    /** Called when the activity is first created. */
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR | Window.FEATURE_ACTION_BAR_OVERLAY);
    super.onCreate(savedInstanceState);

    createUI(savedInstanceState);
}

// ***************************************************************************************************
//
//  setupToolbar
//
// ***************************************************************************************************
private void setupToolbar()
{
    //toolbar = (Toolbar) findViewById(R.id.toolbar);
    if(toolbar != null)
    {
        setSupportActionBar(toolbar);
        mActionBar = getSupportActionBar();
        if(mActionBar != null)
        {
            mActionBar.setDisplayHomeAsUpEnabled(true);
            mActionBar.setHomeButtonEnabled(true);
            mActionBar.setDisplayShowHomeEnabled(false);
            mActionBar.setDisplayUseLogoEnabled(false);
            mActionBar.setIcon(null);
            mActionBar.setBackgroundDrawable(new ColorDrawable(MINAppConfiguration.getSharedInstance().getCurrentVisualElements().tbTopBackgroundColor));

            mActionBar.setTitle(pdfName);
        }
    }
    if (Build.VERSION.SDK_INT >= 21)
    {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.setStatusBarColor(MINAppConfiguration.getSharedInstance().getCurrentVisualElements().tbStatusBarBackgroundColor);
    }
}

// ***************************************************************************************************
//
//  onCreateOptionsMenu
//
// ***************************************************************************************************
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater().inflate(R.menu.pdf_menu_main, menu);
    MenuItem shareMenuItem = menu.findItem(R.id.pdf_action_share);
    mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareMenuItem);
    setShareIntent();
    return true;
}

// ***************************************************************************************************
//
//  onOptionsItemSelected
//
// ***************************************************************************************************
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    boolean bHandledEvent = false;
    int id = item.getItemId();
    switch (id)
    {
        case R.id.pdf_action_search:
            bHandledEvent = true;
            mActionModeSearch = this.startSupportActionMode(mActionModeSearchCallback);
            break;
    }

    if(!bHandledEvent)
    {
        return super.onOptionsItemSelected(item);
    }
    else
    {
        return bHandledEvent;
    }
}


// ***************************************************************************************************
//
//  mActionModeEditCallback
//
// ***************************************************************************************************
protected ActionMode mActionModeSearch;
private ActionMode.Callback mActionModeSearchCallback = new ActionMode.Callback()
{
    @Override
    public boolean onCreateActionMode(ActionMode actionMode, Menu menu)
    {
        actionMode.getMenuInflater().inflate(R.menu.pdf_menu_search, menu);
        mSearchMenuPrev = (MenuItem) menu.findItem(R.id.pdf_menu_search_prev);
        mSearchMenuPrev.setEnabled(false);
        mSearchMenuNext = (MenuItem) menu.findItem(R.id.pdf_menu_search_next);
        mSearchMenuNext.setEnabled(false);
        //mSearchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.pdf_menu_search_item));

        // TODO this search functionality still isn't working
        MenuItem searchItem = menu.findItem(R.id.pdf_menu_search_item);


        mSearchView=(SearchView)MenuItemCompat.getActionView(searchItem);
        if(mSearchView != null)
        {
            mSearchView.setIconifiedByDefault(false);
            mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
            {
                @Override
                public boolean onQueryTextSubmit(String s)
                {
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String s)
                {
                    mSearchString = s;
                    if(s.length() > 0)
                    {
                        mSearchMenuPrev.setEnabled(true);
                        mSearchMenuNext.setEnabled(true);
                    }

                    // Remove any previous search results
                    if (SearchTaskResult.get() != null && !mSearchString.equals(SearchTaskResult.get().txt))
                    {
                        SearchTaskResult.set(null);
                        mDocView.resetupChildren();
                    }

                    return false;
                }
            });
            searchModeOn();
        }
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode actionMode, Menu menu)
    {
        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem)
    {
        switch(menuItem.getItemId())
        {
            case R.id.pdf_menu_search_prev:
                mSearchView.clearFocus();
                hideKeyboard();
                search(-1);
                return true;
            case R.id.pdf_menu_search_next:
                mSearchView.clearFocus();
                hideKeyboard();
                search(1);
                return true;
            default:
                return false;
        }
    }

    @Override
    public void onDestroyActionMode(ActionMode actionMode)
    {
        searchModeOff();
        bPopoverLoaded = false;
        mActionModeEdit = null;
        mActionModeSearch = null;
        resetHideToolbarsTimer();
    }
};

// ***************************************************************************************************
//
//  createUI
//
// ***************************************************************************************************
public void createUI(Bundle savedInstanceState)
{
    if (core == null)
        return;

    // Now create the UI.
    // First create the document view
    mDocView = new MuPDFReaderView(this) {
        @Override
        protected void onMoveToChild(int i) {
            if (core == null)
                return;
            mPageNumberView.setText(String.format("%d / %d", i + 1,
                    core.countPages()));
            mPageSlider.setMax((core.countPages() - 1) * mPageSliderRes);
            mPageSlider.setProgress(i * mPageSliderRes);
            super.onMoveToChild(i);
        }

        @Override
        protected void onTapMainDocArea()
        {
            // Hide/Show action bar
            if(isToolbarsVisible())
            {
                setToolbarsVisible(false);
            }
            else
            {
                setToolbarsVisible(true);
            }
        }

        @Override
        protected void onDocMotion() {
            hideButtons();
        }

        @Override
        protected void onHit(Hit item) {
            switch (mTopBarMode) {
            case Annot:
                if (item == Hit.Annotation) {
                    showButtons();
                    mTopBarMode = TopBarMode.Delete;
                    //mTopBarSwitcher.setDisplayedChild(mTopBarMode.ordinal());
                }
                break;
            case Delete:
                mTopBarMode = TopBarMode.Annot;
            default:
                // Not in annotation editing mode, but the pageview will
                // still select and highlight hit annotations, so
                // deselect just in case.
                MuPDFView pageView = (MuPDFView) mDocView.getDisplayedView();
                if (pageView != null)
                    pageView.deselectAnnotation();
                break;
            }
        }
    };
    mDocView.setAdapter(new MuPDFPageAdapter(this, this, core));

    mSearchTask = new SearchTask(this, core)
    {
        @Override
        protected void onTextFound(SearchTaskResult result)
        {
            SearchTaskResult.set(result);
            // Ask the ReaderView to move to the resulting page
            mDocView.setDisplayedViewIndex(result.pageNumber);
            // Make the ReaderView act on the change to SearchTaskResult
            // via overridden onChildSetup method.
            mDocView.resetupChildren();
        }
    };

    // Make the buttons overlay, and store all its
    // controls in variables
    makeButtonsView();

    // Set up the page slider
    int smax = Math.max(core.countPages()-1,1);
    mPageSliderRes = ((10 + smax - 1)/smax) * 2;

    // Activate the seekbar
    mPageSlider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
    {
        public void onStopTrackingTouch(SeekBar seekBar)
        {
            mDocView.setDisplayedViewIndex((seekBar.getProgress()+mPageSliderRes/2)/mPageSliderRes);
        }

        public void onStartTrackingTouch(SeekBar seekBar)
        {
        }

        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
        {
            updatePageNumView((progress+mPageSliderRes/2)/mPageSliderRes);
        }
    });

    if (savedInstanceState == null || !savedInstanceState.getBoolean("ButtonsHidden", false))
        showButtons();

    // Stick the document view and the buttons overlay into a parent view
    RelativeLayout layout = new RelativeLayout(this);
    layout.addView(mDocView);
    layout.addView(mButtonsView);
    toolbar = new Toolbar(this);
    //toolbar.setMinimumWidth(android.app.ActionBar.LayoutParams.MATCH_PARENT);
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    toolbar.setLayoutParams(layoutParams);
    layout.addView(toolbar);
    //RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) layout.getLayoutParams();
    //params.width = android.app.ActionBar.LayoutParams.MATCH_PARENT;

    setContentView(layout);

    // Setup New Toolbar implementation
    setupToolbar();

    // Set up custom colors, etc...
    setCustomAttributes();
}

// ***************************************************************************************************
//
//  onResume
//
// ***************************************************************************************************
@Override
public void onResume()
{
    super.onResume();
    // Start timer to hide toolbars.
    bPopoverLoaded = false;
    resetHideToolbarsTimer();
}

private void showButtons()
{
    if (core == null)
        return;
    if (!mButtonsVisible)
    {
        mButtonsVisible = true;
        // Update page number text and slider
        int index = mDocView.getDisplayedViewIndex();
        updatePageNumView(index);
        mPageSlider.setMax((core.countPages()-1)*mPageSliderRes);
        mPageSlider.setProgress(index*mPageSliderRes);
        if (mTopBarMode == TopBarMode.Search)
        {
            //mSearchText.requestFocus();
            showKeyboard();
        }

        Animation anim = new TranslateAnimation(0, 0, mPageSlider.getHeight(), 0);
        anim.setDuration(200);
        anim.setAnimationListener(new Animation.AnimationListener()
        {
            public void onAnimationStart(Animation animation) {
                mPageSlider.setVisibility(View.VISIBLE);
            }
            public void onAnimationRepeat(Animation animation) {}
            public void onAnimationEnd(Animation animation) {
                mPageNumberView.setVisibility(View.VISIBLE);
            }
        });
        mPageSlider.startAnimation(anim);
    }
}

private void hideButtons()
{
    if (mButtonsVisible)
    {
        mButtonsVisible = false;
        hideKeyboard();
        Animation anim = new TranslateAnimation(0, 0, 0, mPageSlider.getHeight());
        anim.setDuration(200);
        anim.setAnimationListener(new Animation.AnimationListener()
        {
            public void onAnimationStart(Animation animation)
            {
                mPageNumberView.setVisibility(View.INVISIBLE);
            }
            public void onAnimationRepeat(Animation animation) {}
            public void onAnimationEnd(Animation animation)
            {
                mPageSlider.setVisibility(View.INVISIBLE);
            }
        });
        mPageSlider.startAnimation(anim);
    }
}

private void searchModeOn()
{
    if (mTopBarMode != TopBarMode.Search)
    {
        mTopBarMode = TopBarMode.Search;
        //Focus on EditTextWidget
        showKeyboard();
    }
}

private void searchModeOff()
{
    if (mTopBarMode == TopBarMode.Search)
    {
        mTopBarMode = TopBarMode.Main;
        hideKeyboard();
        SearchTaskResult.set(null);
        // Make the ReaderView act on the change to mSearchTaskResult
        // via overridden onChildSetup method.
        mDocView.resetupChildren();
    }
}

private void makeButtonsView()
{
    mButtonsView = getLayoutInflater().inflate(R.layout.buttons,null);

    mPageNumberView = (TextView)mButtonsView.findViewById(R.id.pageNumber);
    mPageNumberView.setVisibility(View.INVISIBLE);
    mInfoView = (TextView)mButtonsView.findViewById(R.id.info);
    mInfoView.setVisibility(View.INVISIBLE);
    mPageSlider = (SeekBar)mButtonsView.findViewById(R.id.pageSlider);
    mPageSlider.setVisibility(View.INVISIBLE);
}

private void search(int direction)
{
    hideKeyboard();
    int displayPage = mDocView.getDisplayedViewIndex();
    SearchTaskResult r = SearchTaskResult.get();
    int searchPage = r != null ? r.pageNumber : -1;
    mSearchTask.go(mSearchString, direction, displayPage, searchPage);
}

@Override
public boolean onSearchRequested()
{
    if (mButtonsVisible && mTopBarMode == TopBarMode.Search)
    {
        hideButtons();
    }
    else
    {
        showButtons();
        searchModeOn();
    }
    return super.onSearchRequested();
}

@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
    if (mButtonsVisible && mTopBarMode != TopBarMode.Search) {
        hideButtons();
    } else {
        showButtons();
        searchModeOff();
    }
    return super.onPrepareOptionsMenu(menu);
}

 }

EDIT

One item of note: the call get getActionView on the search item is returning null. I think this might have something to do with the problem. I then try and force it but I'm not sure that's right either. Here is the code in question:

    protected ActionMode mActionModeSearch;
private ActionMode.Callback mActionModeSearchCallback = new ActionMode.Callback()
{
    @Override
    public boolean onCreateActionMode(ActionMode actionMode, Menu menu)
    {

        actionMode.getMenuInflater().inflate(R.menu.pdf_menu_search, menu);
        mSearchMenuPrev = (MenuItem) menu.findItem(R.id.pdf_menu_search_prev);
        mSearchMenuPrev.setEnabled(false);
        mSearchMenuNext = (MenuItem) menu.findItem(R.id.pdf_menu_search_next);
        mSearchMenuNext.setEnabled(false);
        mSearchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.pdf_menu_search_item));

        // TODO this search functionality still isn't working
        MenuItem searchItem = menu.findItem(R.id.pdf_menu_search_item);


        mSearchView=(SearchView)MenuItemCompat.getActionView(searchItem);  // ALWAYS COMES BACK AS NULL
        if(mSearchView==null)
        {
            //MenuItemCompat.setShowAsAction(searchItem,MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW|MenuItem.SHOW_AS_ACTION_ALWAYS);
            MenuItemCompat.setShowAsAction(searchItem, MenuItem.SHOW_AS_ACTION_ALWAYS);
            mSearchView = new SearchView(MuPDFActivity.this);
            MenuItemCompat.setActionView(searchItem, mSearchView);
        }
             if(mSearchView != null)
        {
            mSearchView.setIconifiedByDefault(false);
            mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
            {
                @Override
                public boolean onQueryTextSubmit(String s)
                {
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String s)
                {
                    mSearchString = s;
                    if(s.length() > 0)
                    {
                        mSearchMenuPrev.setEnabled(true);
                        mSearchMenuNext.setEnabled(true);
                    }

                    // Remove any previous search results
                    if (SearchTaskResult.get() != null && !mSearchString.equals(SearchTaskResult.get().txt))
                    {
                        SearchTaskResult.set(null);
                        mDocView.resetupChildren();
                    }

                    return false;
                }
            });
            searchModeOn();
        }
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode actionMode, Menu menu)
    {
        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem)
    {
        switch(menuItem.getItemId())
        {
            case R.id.pdf_menu_search_prev:
                mSearchView.clearFocus();
                hideKeyboard();
                search(-1);
                return true;
            case R.id.pdf_menu_search_next:
                mSearchView.clearFocus();
                hideKeyboard();
                search(1);
                return true;
            default:
                return false;
        }
    }

    @Override
    public void onDestroyActionMode(ActionMode actionMode)
    {
        searchModeOff();
        bPopoverLoaded = false;
        mActionModeEdit = null;
        mActionModeSearch = null;
        resetHideToolbarsTimer();
    }
};

EDIT 2:

I got the control to come up but now the "next" and "prev" menu items are gone. Also, the control is not left aligned. And, I still have to expand the search control.

Screenshot:

enter image description here

Here is the updated code that got me here:

         public boolean onCreateActionMode(ActionMode actionMode, Menu menu)
    {
        actionMode.getMenuInflater().inflate(R.menu.pdf_menu_search, menu);
        mSearchMenuPrev = menu.findItem(R.id.pdf_menu_search_prev);
        mSearchMenuPrev.setEnabled(false);
        mSearchMenuNext = menu.findItem(R.id.pdf_menu_search_next);
        mSearchMenuNext.setEnabled(false);
        MenuItem searchItem = menu.findItem(R.id.pdf_menu_search_item);
        MenuItemCompat.setShowAsAction(searchItem, MenuItem.SHOW_AS_ACTION_ALWAYS);
        mSearchView = new SearchView(MuPDFActivity.this);
        MenuItemCompat.setActionView(searchItem, mSearchView);
        //mSearchView.setIconifiedByDefault(false);
        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
        {
            @Override
            public boolean onQueryTextSubmit(String s)
            {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s)
            {
                mSearchString = s;
                if(s.length() > 0)
                {
                    mSearchMenuPrev.setEnabled(true);
                    mSearchMenuNext.setEnabled(true);
                }

                // Remove any previous search results
                if (SearchTaskResult.get() != null && !mSearchString.equals(SearchTaskResult.get().txt))
                {
                    SearchTaskResult.set(null);
                    mDocView.resetupChildren();
                }

                return false;
            }
        });
        searchModeOn();
        return true;
    }

If I call mSearchView.setIconified(false), I get the following screenshots. The menu items are all there but they are in overflow. If I expand overflow, I see the search menu but when I click on it, it goes back to my original problem of no edit box to enter search string. In other words, it looks like the first screenshot below when I click on "Search":

enter image description here

enter image description here

Upvotes: 1

Views: 218

Answers (1)

JustLearningAgain
JustLearningAgain

Reputation: 2287

I finally had to build it manually. I changed the menu xml file to:

 <?xml version="1.0" encoding="utf-8"?>
 <menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">
    <group android:id="@+id/group_search_mode">
        <item
        android:id="@+id/pdf_menu_search_item"
        android:title="@string/search"
        android:icon="@drawable/ic_pdf_action_search"
        app:showAsAction="ifRoom"
        app:actionViewClass="android.support.v7.widget.SearchView"/>
</group>

I then changed the code in the ActionMode.Callback to manually build the menu and force the search control open. I think it might be overkill but it works:

private static final int PREV_MENU_ITEM_ID = 1;
private static final int NEXT_MENU_ITEM_ID = 2;
protected ActionMode mActionModeSearch;
private ActionMode.Callback mActionModeSearchCallback = new ActionMode.Callback()
{
    @Override
    public boolean onCreateActionMode(ActionMode actionMode, Menu menu)
    {
        actionMode.getMenuInflater().inflate(R.menu.pdf_menu_search, menu);
        // Load search action
        MenuItem searchItem = menu.findItem(R.id.pdf_menu_search_item);
        MenuItemCompat.setShowAsAction(searchItem, MenuItem.SHOW_AS_ACTION_ALWAYS);
        mSearchView = new SearchView(MuPDFActivity.this);
        MenuItemCompat.setActionView(searchItem, mSearchView);
        MenuItemCompat.expandActionView(searchItem);
        mSearchView.setIconifiedByDefault(false);
        mSearchView.setIconified(false);

        // Load prev and next menu items
        mSearchMenuPrev = menu.add(0, PREV_MENU_ITEM_ID, 1, R.string.search_prev);
        mSearchMenuPrev.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        mSearchMenuPrev.setEnabled(false);
        mSearchMenuNext = menu.add(0, NEXT_MENU_ITEM_ID, 1, R.string.search_next);
        mSearchMenuNext.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        mSearchMenuNext.setEnabled(false);

        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
        {
            @Override
            public boolean onQueryTextSubmit(String s)
            {
                search(1);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s)
            {
                mSearchString = s;
                if(s.length() > 0)
                {
                    mSearchMenuPrev.setEnabled(true);
                    mSearchMenuNext.setEnabled(true);
                }

                // Remove any previous search results
                if (SearchTaskResult.get() != null && !mSearchString.equals(SearchTaskResult.get().txt))
                {
                    SearchTaskResult.set(null);
                    mDocView.resetupChildren();
                }

                return false;
            }
        });
        searchModeOn();
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode actionMode, Menu menu)
    {
        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem)
    {
        switch(menuItem.getItemId())
        {
            case PREV_MENU_ITEM_ID:
                mSearchView.clearFocus();
                hideKeyboard();
                search(-1);
                return true;
            case NEXT_MENU_ITEM_ID:
                mSearchView.clearFocus();
                hideKeyboard();
                search(1);
                return true;
            default:
                return false;
        }
    }

    @Override
    public void onDestroyActionMode(ActionMode actionMode)
    {
        searchModeOff();
        bPopoverLoaded = false;
        mActionModeEdit = null;
        mActionModeSearch = null;
        resetHideToolbarsTimer();
    }
};

I hope this helps someone out there. What a pain!

Upvotes: 3

Related Questions