2

I am trying to Implement the Search Functionality in my Android Application using the link http://developer.android.com/guide/topics/search/search-dialog.html.

It can only search the text entered when pressed on the search button. But it cannot perform search when the text in the search bar changes. I want to implement that kind of functionality, just as it is in the "People" or "Contact" App in the Android phones.

I have tried to search for this a lot but unable to find any links. If anyone can direct my to those links, it would be useful. Thanks in advance.

Edit:

I have implemented the search functionality in the MainActivity Only. The code for MainActivity.java:

findViewById(R.id.id1).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        onSearchRequested();
    }
});

Android Manifest:

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:launchMode="singleTop">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <action android:name="android.intent.action.SEARCH" />
        <action android:name="android.intent.action.VIEW" />
    </intent-filter>
    <meta-data
        android:name="android.app.searchable"
        android:resource="@xml/activity_search" />
</activity>
Rohit Suthar
  • 2,635
  • 1
  • 22
  • 27
Shripal Gandhi
  • 126
  • 2
  • 9

2 Answers2

1

Just do this if you are Using Search bar,

lv.setTextFilterEnabled(true);

But I recommend you to use EditText instead of Search bar, if you agree do this for EditText

edittext = (EditText) findViewById(R.id.edittext);
edittext.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            Search_Activity.this.adapter.getFilter().filter(s);
        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

I hope this will help you.

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
0

First of all you need to have handler in your activity :

    private static class SearchHandler extends Handler {
    private WeakReference<CitySelectionActivity> mTarget;

    SearchHandler(CitySelectionActivity target) {
      mTarget = new WeakReference<>(target);
    }

    public void setTarget(CitySelectionActivity target) {
      mTarget.clear();
      mTarget = new WeakReference<>(target);
    }

    @Override
    public void handleMessage(final Message msg) {
      if (msg.what == CitySelectionActivity.TRIGGER_SEARCH) {
        SelectionActivity activity = mTarget.get();
        activity.makeAjaxRequest(mSearchText.trim());
      }
    }
  }

Now you need to add text change listener to your search edit text

searchView.addTextChangedListener(new TextWatcher() {
      @Override
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {
      }

      @Override
      public void onTextChanged(CharSequence s, int start, int before, int count) {
        mSearchText = searchView.getText().toString();
        if (!mSearchText.trim().isEmpty()) {
          handler.removeMessages(CitySelectionActivity.TRIGGER_SEARCH);
          handler.sendEmptyMessageDelayed(SelectionActivity.TRIGGER_SEARCH, SelectionActivity.SEARCH_TRIGGER_DELAY_IN_MS);
        } else {
          suggestList.clear();
        }
      }

      @Override
      public void afterTextChanged(Editable s) {
      }
    });