Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SearchView requires two clicks to expand the view

Short: When I click on my SearchViewIcon, the SearchView doesn't collapse/expand.

Long:

I"m using a SearchView to filter a RecyclerView in a Fragment that is in my MainActivity.

When I click on the SearchViewIcon (SearchView is iconified by default). I open the tab with the correct Fragment with this code:

searchView.setOnSearchClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(2, false);
            }
        });

The Tab with the correct Fragment is opened like expected. Also the text input is shown, but the SearchView stays iconified. (See picture below.)

Application

My SearchView in XML:

<item
    android:id="@+id/action_search"
    android:orderInCategory="1"
    android:icon="@android:drawable/ic_menu_search"
    android:title="@string/menu_item_search_title"
    app:showAsAction="ifRoom"
    app:queryHint="Search name or function"
    app:actionViewClass="android.support.v7.widget.SearchView" />

Things I already tried:

Setting my showAsAction to always or ifRoom|collapseActionView

app:showAsAction="always"
app:showAsAction="ifRoom|collapseActionView"

Request focus on my SearchView:

searchView.requestFocus();

Expanding my SearchViewItem:

MenuItem searchViewItem = menu.findItem(R.id.action_search);
searchViewItem.expandActionView();

But non of these things worked...

EDIT Like the title says, if I click the SearchViewIcon again, the SearchView does expand.

like image 203
Kerkhofsd Avatar asked Feb 19 '16 12:02

Kerkhofsd


People also ask

How do I use searchview in Android?

This example demonstrates how do I use SearchView in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.java.

How to expand menuItem from icon to searchview in Android?

Call MenuItem.expandActionView to expand MenuItem from icon into SearchView. To prevent focus, call searchView.clearFocus on searchView.doOnLayout (using android-ktx)

How to prevent focus on searchview in Android?

To prevent focus, call searchView.clearFocus on searchView.doOnLayout (using android-ktx)

How to display a searchview and listview using its different attributes?

In this step we open an xml file and add the code for displaying a SearchView and ListView by using its different attributes. In this step we open MainActivity and add the code to initiate SearchView and ListView. In this we create an Animal name list and then set the adapter to fill the data in ListView.


2 Answers

I've also encountered a similar problem, and was able to solve it. The main problems I've needed to solve were:

  1. searchView was not expanding by default
  2. the search EditText didn't get a focus until a second click
  3. even after solving (2), the keyboard didn't show up until a second click

solutions:

  1. in menu.xml need to define app:showAsAction as "collapseActionView" + in java code need also to call searchView.setIconifiedByDefault(false) for the expansion to take priority over the iconification (that is - when the icon is pressed, expand and don't stay in icon mode)
  2. add a MenuItem.OnActionExpandListener for your search menu-item object and use a handler to post a runnable that will request focus to your search-view object. (why runnable? because requesting focus when the menu is still not fully inflated is not guaranteed to work. When requesting focus in a handler-runnable, I'm making sure the focus request happens AFTER all the work in the onCreateOptionsMenu() is ready and finished.
  3. in The same defined runnable from (2), also ask android OS to show the keyboard.

complete code solving all those problems:

menu.xml:

    <item
    android:id="@+id/searchContacts"
    android:icon="@drawable/ic_search_white_24dp"
    android:title="search"
    app:showAsAction="collapseActionView|always"
    app:actionViewClass="android.widget.SearchView"
    />
 <!-- and of course your other menu items -->

searchable configuration:

Need to create such xml file. right-click your res folder and choose new --> android resource file. put whatever you want as file name ("searchable" will work ok, for example), and choose XML as resource type. Then copy & paste this code in the created file (replace the hint string with your own):

<?xml version="1.0" encoding="utf-8"?>

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="put here your hint string to be shown"
    />

MainActivity:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu, menu);
        implementSearch(menu);
        return true;
    }

    private void implementSearch(final Menu menu) {
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        final MenuItem searchMenuItem = menu.findItem(R.id.searchContacts);
        final SearchView searchView = (SearchView) searchMenuItem.getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);

        searchMenuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener(){
            @Override
            public boolean onMenuItemActionExpand(MenuItem item){
                // the search view is now open. add your logic if you want
                new Handler().post(new Runnable() {
                    @Override
                    public void run() {
                        searchView.requestFocus();
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        if (imm != null) { // it's never null. I've added this line just to make the compiler happy
                            imm.showSoftInput(searchView.findFocus(), 0);
                        }


                    }
                });
                return true;
            }


            @Override
            public boolean onMenuItemActionCollapse(MenuItem item){
                // the search view is closing. add your logic if you want
                return true;
            }

        });

    }

Also, if you want to use your own callbacks for the search's text change and submitted (instead of Android intents), in the implementSearch() method add a call to searchMenuItem.setOnActionExpandListener(new SearchView.OnQueryTextListener({...})).

view this SO question for more details

like image 124
Re'em Avatar answered Nov 15 '22 13:11

Re'em


The simplest solution is to use always option for app:showAsAction:

app:showAsAction="always"

<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
android:title="search"
app:showAsAction="always"
app:actionViewClass="android.widget.SearchView"
/>
like image 34
HAXM Avatar answered Nov 15 '22 13:11

HAXM