Search code examples
androidsearchview

Search View is not working in android


I have a Listview which displays the names of the flowers which are fetched from a URL (JSON data). I have made a ArrayList of the type class "Flower" in which I have saved all the flowers information which is there in the JSON data. I have added the SearchView in the app. Its properly configured in the application but when I enter some text inside it and click on the search button in the keyboard of mobile then there is no action done after that.

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.titlebar, menu);
        SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView=(SearchView)menu.findItem(R.id.new_search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this,FrontPage.class)));


        return  true;
    }

This is my SearchResultActivity.java file.

package com.example.hsports.flowers;

import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;

public class SearchResultsActivity extends AppCompatActivity {

    FrontPage frontPage=new FrontPage();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_results);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        handleIntent(getIntent());


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        handleIntent(getIntent());

    }


    private void handleIntent(Intent intent) {

        TextView name=(TextView)findViewById(R.id.displayName);
        TextView url=(TextView)findViewById(R.id.displayUrl);


        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);

            for(Flower f:frontPage.flowersList)
            {
                if(f.name.equalsIgnoreCase(query))
                {

                    name.setText(f.name);
                    url.setText(f.url);

                }
            }



        }
    }
}

I have configured this activity in the androidmanifest.xml file as:

 <activity
            android:name=".SearchResultsActivity"
            android:label="@string/title_activity_search_results"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>


        </activity>

When I am clicking on the searchview icon and entering some text and pressing enter from keyboard , its not giving me any result. I have one more doubt , where will the flow go after entering query into the searchView ?


Solution

  • You are missing android:launchMode="singleTop"in the activity level of your manifest

    EDIT: You also need to create an searchable.xml file in an xml directory with the following code

    <?xml version="1.0" encoding="utf-8"?>
    <searchable xmlns:android="http://schemas.android.com/apk/res/android"
                android:label="@string/app_name"
                android:hint="Search" />

    then in your manifest for the same activity add the following meta-data

    <activity   android:name=".SearchResultsActivity"
                android:label="@string/title_activity_search_results"
                android:theme="@style/AppTheme.NoActionBar"
                android:launchMode="singleTop">
                <intent-filter>
                    <action android:name="android.intent.action.SEARCH" /> 
                </intent-filter>
                <meta-data android:name="android.app.searchable"
                           android:resource="@xml/searchable" />
            </activity>