Android - Actionbar Sherlock - Search Filter

16,367

first make a editTextLayout

layout_search.xml

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/search_edit_text"
    android:cursorVisible="true"
    android:hint="@string/search_friend_hint"
    android:imeOptions="actionDone"
    android:inputType="text"
    android:textColor="@android:color/black"
    android:textCursorDrawable="@android:color/black" />

In you menu xml add android:actionLayout and android:showAsAction="always|collapseActionView" for Search option. For other option make android:showAsAction="ifRoom"

menu.xml

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

    <item
        android:icon="@drawable/ic_action_sort"
        android:orderInCategory="1"
        android:showAsAction="ifRoom"
        android:title="@string/menu_sort"/>
    <item
        android:id="@+id/menu_search"
        android:actionLayout="@layout/layout_search"
        android:icon="@drawable/search"
        android:orderInCategory="0"
        android:showAsAction="always|collapseActionView"
        android:title="@string/search"/>

</menu>

in your activity or fragment override onCreateOptionsMenu like this fragment.java

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

        final EditText editText = (EditText) menu.findItem(
                R.id.menu_search).getActionView();
        editText.addTextChangedListener(textWatcher);

        MenuItem menuItem = menu.findItem(R.id.menu_search);
        menuItem.setOnActionExpandListener(new OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                // Do something when collapsed
                return true; // Return true to collapse action view
            }

            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                editText.clearFocus();
                return true; // Return true to expand action view
            }
        });
    }

and add textWatcherListener

private TextWatcher textWatcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

            if (null != mAdapter) {
                mAdapter.getFilter().filter(s);
            }
        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    };
Share:
16,367

Related videos on Youtube

suresh cheemalamudi
Author by

suresh cheemalamudi

Android Developer

Updated on October 14, 2020

Comments

  • suresh cheemalamudi
    suresh cheemalamudi over 3 years

    enter image description here

    I am trying to implement Action Bar using ActionBar Sherlock. I have three Action Button one of which is a Search Button. On Clicking of the Search Button the Search input field should be displayed which i have already implemented. But i want it to take the full width of the Action Bar. Any idea how i can achieve the same.

    • Alex Fu
      Alex Fu over 11 years
      Have you tried looking at the ActionBarSherlock samples? github.com/JakeWharton/ActionBarSherlock/tree/master/samples‌​/…
    • suresh cheemalamudi
      suresh cheemalamudi over 11 years
      @ Alex Fu:Ya i am using API demos as reference to code. but it dint help me to acheive that particular requirement.
    • jumper0k
      jumper0k over 11 years
      Did you try to use SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW for menu items?
    • suresh cheemalamudi
      suresh cheemalamudi over 11 years
      No. I have no idea how to add(SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW). could u please suggest.?
    • Alex Fu
      Alex Fu over 11 years
      @suresh -- github.com/JakeWharton/ActionBarSherlock/blob/master/samples‌​/… No sure how you could of missed this if you looked through the demo source code.
    • intrepidis
      intrepidis almost 11 years
      @Alex_Fu unfortunately your web links are broken.
  • cringe
    cringe over 10 years
    Can you explain what the mFollowingAdapter in your example is?
  • JafarKhQ
    JafarKhQ over 10 years
    @cringe its the adapter name(mAdapter: a ListView adapter), i have change it
  • JafarKhQ
    JafarKhQ about 10 years
    @Mehdiway you can see its a 'background', A PNG Image. you can remove it if you want the default editText bg.
  • JafarKhQ
    JafarKhQ almost 10 years
    @XiJiaopin if you use custom adapter (BaseAdapter), you have to implement the filter, check ArrayAdapter code for the Filter