3

I am designing a simple search feature for my application, I have Successfully created a search icon on the toolbar of my screen. Now I'm having problems in implementing brains (working) behind the search feature

  • Through Android's official documentation for setting up search interface, I
    came across this various procedure of creating a searchable configuration, searchable activity, ACTION_SEARCH!

  • I'm new to android development, I'm having a tough time in understanding all these concepts, Can someone explain the above procedure in simple terms?

black sheep 369
  • 564
  • 8
  • 19

2 Answers2

1
  1. Through Android's official documentation for setting up search interface, I came across this various procedure of creating a searchable configuration, searchable activity, ACTION_SEARCH!

  2. I'm new to android development, I'm having a tough time in understanding all these concepts, Can someone explain the above procedure in simple terms?

Well, It's easy. First, Read the Creating a Search Interface. Second, Starts from here by adding an xml in the res/xml/ project directory.

Then, You'll need to create a Searchable Activity by following link:

Creating a Searchable Activity

After creating the Searchable Activity, You'll need to declare it on AndroidManifest.xml, Just add the following to your SearchableActivity:

<activity android:name=".SearchableActivity" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
                   android:resource="@xml/searchable"/>
    </activity>

After all these, Read this : PerformingSearch And Receive the query from your Activity when user do the searches(Like MainActivity to SearchableActivity).

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
0

Inflate the XML menu resource (res/menu/options_menu.xml) in the onCreateOptionsMenu() method of your activity:and getMenu Item .Write following code

MenuItem searchItem=menu.findItem(R.id.searchMenu);
SearchView searchView=(SearchView)                
MenuItemCompat.getActionView(searchItem);

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
     public boolean onQueryTextSubmit(String query) {
           Toast.makeText(MainActivity.this, ""+query,     
    Toast.LENGTH_SHORT).show();
    return false;
}
Ameer Hamza
  • 27
  • 1
  • 5
  • I put the above code into the onCreate..(), I get this error java.lang.NullPointerException: Attempt to invoke interface method 'android.view.View android.view.MenuItem.getActionView()' on a null object reference – black sheep 369 Oct 02 '17 at 11:42
  • @HoneyKhandelwal Yes, You need to paste them inside the OnCreateOptionsMenu(); But it's just returns the searched query! – ʍѳђઽ૯ท Oct 02 '17 at 12:41
  • You must first need to create an xml for search menu item then you can get it's reference in activity . If you do not create search menu in xml you get null pointers exception – Ameer Hamza Oct 02 '17 at 14:38