I have an action with an ActionBar. One option is to search for a document (not necessarily local). I want to use a contextual action panel (CAB) with the following elements:
- edit field - a place for the user to enter the text to be found
- Previous button - go to the previous item that matches the search
- Next button - go to the next item that matches the search
I want to use CAB for several reasons. First of all, I want the user to select an option that displays the CAB defined above. When the user has performed a search, they select the done button and the ActionBar returns to its previous state.
Here is the problem. I cannot get the search item to appear in my CAB in an advanced state. NOTE. The activity I'm trying to change is NOT the main activity, but it is an activity that the user triggers based on certain events. Once this action loads, the user may or may not decide to use the search function that I am describing. I also don't want Android to search. I have an API that will search for the results I want and let me go to the previous / next search result.
My code for CAB (which gets called correctly):
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);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.pdf_menu_search_item).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
@Override
public boolean onQueryTextSubmit(String s)
{
return false;
}
@Override
public boolean onQueryTextChange(String s)
{
return false;
}
});
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:
findPrevSearchResult();
return true;
case R.id.pdf_menu_search_next:
findNextSearchResult();
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode actionMode)
{
}
};
CAB Menu Code:
<?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="collapseActionView|ifRoom"
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="ifRoom" />
<item
android:id="@+id/pdf_menu_search_next"
android:title="@string/search_next"
android:icon="@drawable/ic_pdf_action_search_next"
app:showAsAction="ifRoom" />
</group>
I also changed the definition of my activity in AndroidManifest.xml, although I'm not sure if this is necessary:
<activity
android:name="com.myapp.myActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.default_searchable"
android:value="com.myapp.myActivity" />
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
, . , CAB , , . , , onCreateActionMode , . , , , , "". "", . CAB . ????
, CAB. , , , . , .
