How to create a “Search” field in an action context bar?

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);
        // Associate searchable configuration with the SearchView
        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)
    {
        //resetHideToolbarsTimer();
    }
};

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. , , , . , .

enter image description here

+4
2

-, always app:showAsAction :

<?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:icon="@drawable/ic_pdf_action_search"
            android:title="@string/search"
            app:actionViewClass="android.support.v7.widget.SearchView"
            app:showAsAction="always"/>
        <item
            android:id="@+id/pdf_menu_search_prev"
            android:icon="@drawable/ic_pdf_action_search_prev"
            android:title="@string/search_prev"
            app:showAsAction="always"/>
        <item
            android:id="@+id/pdf_menu_search_next"
            android:icon="@drawable/ic_pdf_action_search_next"
            android:title="@string/search_next"
            app:showAsAction="always"/>
    </group>
</menu>

, Activity , , SearchView.

AndroidManifest.xml:

<activity
    android:name="com.myapp.myActivity"
    android:label="@string/app_name" />

ActionMode.Callback :

private ActionMode.Callback mActionModeSearchCallback = new ActionMode.Callback() {

    private SearchView mSearchView;

    @Override
    public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
        actionMode.getMenuInflater().inflate(R.menu.home, menu);
        mSearchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.pdf_menu_search_item));
        mSearchView.setIconifiedByDefault(false);
        mSearchView.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) {
        mSearchView.requestFocus();
        return true;
    }

    @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) {

    }
};

4.0+, . .

, .

+9

searchView CAB ( edittext) manu item, , searchview , , .

/search.xml

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

    <item
        android:id="@+id/edt_mySearch"
        android:actionLayout="@layout/search_bar"
        android:enabled="true"
        android:showAsAction="always"
        android:visible="true"/>

</menu>

layout/search _bar.xml

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


    <EditText 
        android:layout_alignParentLeft="true"
        android:id="@+id/edt_search"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint=" Search..."
        />


</RelativeLayout>

myAction

mActionModeCallback = new ActionMode.Callback() {

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                // mode.setTitle("Demo");
                getSupportMenuInflater().inflate(R.menu.search, menu);
                RelativeLayout m = (RelativeLayout) menu.findItem(
                        R.id.edt_mySearch).getActionView();
                EditText mSearchView = (EditText) m
                        .findViewById(R.id.edt_search);

                mSearchView.addTextChangedListener(new TextWatcher() {

                    @Override
                    public void onTextChanged(CharSequence s, int start,
                            int before, int count) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void beforeTextChanged(CharSequence s, int start,
                            int count, int after) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        // search here
                    }
                });
                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
            }

        };
+2

All Articles