azeemofwalsall
azeemofwalsall

Reputation: 75

How do you customize default behaviour of search dialog?

A search button in the app action bar launches the search dialog box. When the search query is submitted (by tapping enter) on the on-screen keyboard, the search results are displayed but:

Here is a screen shot:

enter image description here

I would like the search dialog to disappear when the query is submitted and the keyboard to close.

I have searched extensively and read the android documentation but I can't find the answer to my question

Here is the layout for the app bar:

<?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">

    <item
        android:id="@+id/search_box"
        android:icon="@drawable/ic_outline_search_24"
        android:title="Search"
        app:actionViewClass="android.widget.SearchView"
        app:showAsAction="collapseActionView|ifRoom" />
</menu>

Here is the code for the onCreateOptionsMenu():

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.appbar_menu, menu);

        // associate searchable configuration with the SearchView
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search_box).getActionView();

        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    return super.onCreateOptionsMenu(menu);
    }

And the intent handler:

   public void handleIntent(Intent intent) {
        TextView tv = (TextView) popupView.findViewById(R.id.list_food_name);
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            Log.e("Message :","The search intent was received");
            Log.e("Message :","The search query was: " + query);

            // Bind the popup children
            Iterator iterator = DatabaseOpenHelper.foodList.iterator();
            while (iterator.hasNext()) {
                Food food = (Food) iterator.next();
                if (food.getFoodName().equals(query)) {
                    Log.e("Message :", "Food name is :" + food.getFoodName());
                    tv.setText(food.getFoodName());
                    Log.e("Message :", "The listFoodName TextView text is :" + tv.getText());
                }
            }
            popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
        }
    }

Any help or insights much appreciated.

Upvotes: 0

Views: 43

Answers (1)

azeemofwalsall
azeemofwalsall

Reputation: 75

I found the answer here:

https://stackoverflow.com/search?q=android+-+collapse+searchview+after+submit

and by reading Android Class SearchView documentation

Upvotes: 1

Related Questions