Expand and give focus to SearchView automatically
Asked Answered
E

20

82

I'm developing an application where the user presses the "Search" icon in the ActionBar and a SearchView is made visible at the top of the screen.

My problem is that the SearchView is not in focus nor expanded so the user has to press the search button on the Searchview to make it expand and bring out the keyboard.

How should this be solved?

Essence answered 29/7, 2012 at 14:32 Comment(0)
C
108

To make the SearchView expanded by default, call setIconifiedByDefault(false) on it when you initialise it (e.g. in onCreateOptionsMenu(..) or onPrepareOptionsMenu(..)). I've found in most cases this will give it focus automatically, but if not simply call requestFocus() on it too.

Chuck answered 29/7, 2012 at 14:41 Comment(5)
I'm not sure to be honest, sorry.Chuck
Adding a requestFocus() call in onPrepareOptionsMenu(..) did the trick for me.Paraprofessional
requestFocus doesn't work for me. To "focus" you can call setIconified with falseHoule
@Essence You have to set both ...searchView.setIconifiedByDefault(false); searchView.requestFocus();Anyhow
be sure to have set the menu item property app:showAsAction="always". When I removed the autofocus the icon disappeared and the 3 dots icon appearedLunisolar
N
113

You can also call to expandActionView() method in order to force it:

@Override
public boolean onCreateOptionsMenu( Menu menu )
{
    super.onCreateOptionsMenu( menu );

    MenuItem searchMenuItem = menu.findItem( R.id.mi_search ); // get my MenuItem with placeholder submenu
    searchMenuItem.expandActionView(); // Expand the search menu item in order to show by default the query

    return true;
}

Search item in the Action Bar layout:

<item
        android:id="@+id/mi_search"
        android:icon="@drawable/abs__ic_search_api_holo_light"
        android:title="@string/search"
        android:showAsAction="ifRoom|collapseActionView"
        android:actionViewClass="com.actionbarsherlock.widget.SearchView"
        />
Neel answered 30/3, 2013 at 17:34 Comment(6)
If you use AppCompat, instead of calling expandActionView() directly on the searchMenuItem, you should use this instead: MenuItemCompat.expandActionView(searchMenuItem).Loutish
@CharlesMadere way is the only one that worked for me, API 23Paulus
plus 1 for searchMenuItem.expandActionView();Presently
MenuItemCompat.expandActionView(searchMenuItem) is deprecated nowSplutter
This solution is deprecated.Personage
Finally, one using showAsAction(ifRoom|collapseActionView). Also, I had to add: searchView.setIconifiedByDefault(false)Artemisia
C
108

To make the SearchView expanded by default, call setIconifiedByDefault(false) on it when you initialise it (e.g. in onCreateOptionsMenu(..) or onPrepareOptionsMenu(..)). I've found in most cases this will give it focus automatically, but if not simply call requestFocus() on it too.

Chuck answered 29/7, 2012 at 14:41 Comment(5)
I'm not sure to be honest, sorry.Chuck
Adding a requestFocus() call in onPrepareOptionsMenu(..) did the trick for me.Paraprofessional
requestFocus doesn't work for me. To "focus" you can call setIconified with falseHoule
@Essence You have to set both ...searchView.setIconifiedByDefault(false); searchView.requestFocus();Anyhow
be sure to have set the menu item property app:showAsAction="always". When I removed the autofocus the icon disappeared and the 3 dots icon appearedLunisolar
O
66

If you want to have it iconifiedByDefault, this worked for me. setFocusable and setIconified are needed.

    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setIconifiedByDefault(true);
    searchView.setFocusable(true);
    searchView.setIconified(false);
    searchView.requestFocusFromTouch();

Update: If you are Using android.support.v7.widget.SearchView the behaviour us very different. clearFocus is needed if you don't want the keyboard pop-up all the time. For some reason the menu is recreated all the time, when using appcompat.

SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setIconified(false);
searchView.clearFocus();
Offing answered 26/4, 2015 at 9:48 Comment(0)
E
28

If you're using it in layout, you can call

mSearchView.onActionViewExpanded()

Enwind answered 2/12, 2013 at 18:31 Comment(4)
What does using in layout mean?Offing
I mean that you reference to it as a View, not MenuItemEnwind
that didn't work for me. Calling setIconified() did though.Autoerotic
worked for me too, I am not sure why in official website they have not included in sample code. I mean if i am clicking on search button the keyboard should open right???Siu
A
8

This worked for me :

In the root layout :

xmlns:app="http://schemas.android.com/apk/res-auto"

SearchView defined as follows :

 <android.support.v7.widget.SearchView
        android:id="@+id/search_contacts"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="15dp"
        android:background="@drawable/search_view"        
        app:iconifiedByDefault="false"
        app:queryHint="Search name or email"
        >

        <requestFocus />
    </android.support.v7.widget.SearchView>

The difference is with app tag.

app:iconifiedByDefault="false"
app:queryHint="Search name or email"
Applicator answered 8/2, 2015 at 8:33 Comment(0)
T
6

If you are using inside an activity you need to use

view.onActionViewExpanded();

if you are using inside menu options you need to use

MenuItem.expandActionView();

Note: it works only for SearchView

these two situations are worked for me.

Teriteria answered 18/5, 2016 at 9:42 Comment(0)
N
4

This worked for me:

menu.expandActionView();
Ngo answered 8/8, 2013 at 13:54 Comment(1)
but close icon is not visible initially in expanded form. #27706692Gangrel
A
2

You can use the SearchView#setIconified() method on a SearchView handle in your Java code. More here:

http://developer.android.com/reference/android/widget/SearchView.html#setIconified(boolean)

You can also make use of SearchView#setIconifiedByDefault(). More info here:

http://developer.android.com/reference/android/widget/SearchView.html#setIconifiedByDefault(boolean)

Adonis answered 27/5, 2015 at 13:59 Comment(0)
S
2

use this

SearchManager searchManager = (SearchManager) mActivity.getSystemService(Context.SEARCH_SERVICE);
                SearchView searchView = new SearchView(mActivity.actionBar.getThemedContext());
                searchView.setSearchableInfo(searchManager.getSearchableInfo(mActivity.getComponentName()));
                searchView.setIconifiedByDefault(false);
                searchView.setQueryHint("Search");
                menu.findItem(R.id.action_search).setActionView(searchView);
Selfcontrol answered 12/7, 2017 at 12:9 Comment(0)
S
2

@Pascalius's answer worked for me. But every time you close the SearchView, and click again, you lost the Focus. So I inserted the code in a setOnMenuItemClickListener like this:

MenuItem item = menu.findItem(R.id.action_search);

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchView searchView = (SearchView) item.getActionView();

item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem menuItem) {
        searchView.setIconifiedByDefault(true);
        searchView.setFocusable(true);
        searchView.setIconified(false);
        searchView.requestFocusFromTouch();

        return false;
    }
});
Socket answered 10/9, 2017 at 2:26 Comment(0)
A
2

For Appcompat Searchview you can use this method:

MenuItemCompat.expandActionView(mSearchMenuItem);
Aaronaaronic answered 19/9, 2017 at 13:31 Comment(0)
E
2

Kotlin

var searchView = menu.findItem(R.id.action_search).actionView as SearchView
searchView.isIconified = true
Extrajudicial answered 26/5, 2021 at 7:49 Comment(0)
P
1

Call expandActionView() on the menuItem.

menuItem.expandActionView()
Personage answered 21/5, 2020 at 2:45 Comment(0)
N
1

I was having a hard time doing this with SearchView widget, but the expandActionView() method is actually defined in the MenuItem class, not SearchView class. So, I solved it by

        MenuItem searchItem = menu.findItem(R.id.item_search);
        SearchView   searchView = (SearchView) searchItem.getActionView();
        searchItem.expandActionView();//the method expandActionView is called on searchItem not searchView
Nieman answered 7/11, 2020 at 15:50 Comment(0)
S
1

"android.widget.SearchView" has different behavior from the other menu items. So you should use "setOnSearchClickListener", not "OnMenuItemClickListener" nor "onOptionsItemSelected".

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    super.onCreateOptionsMenu(menu);
    final MenuItem menuItem = menu.findItem(R.id.search_menu_search_view);
    final SearchView searchView = (SearchView)menuItem.getActionView();
    searchView.setIconifiedByDefault(true);

    // Just like this!
    searchView.setOnSearchClickListener(view -> {
        view.requestFocus();
    });

    return true;
}
Schuster answered 21/6, 2023 at 13:34 Comment(0)
G
0

MenuItemCompat's SearchView has a property named maxWidth.

final MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setMaxWidth(xxx);

use screen width instead of xxx offcourse

Guereza answered 30/7, 2015 at 11:28 Comment(0)
L
0

I am using android.widget Searchview and iconified by default.Below code in xml helped me make it expand and autofocus on search view,when clicked:

<SearchView
                android:id="@+id/searchView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:iconifiedByDefault="true"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:queryHint="Search"/>
Lisbethlisbon answered 24/8, 2017 at 18:27 Comment(0)
M
0

 <item
        android:id="@+id/search"
        android:icon="@drawable/ic_search"
        android:title="Search"
        app:actionViewClass="androidx.appcompat.widget.SearchView"
        app:showAsAction="always"/>

 public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search_menu, menu);
        // Associate searchable configuration with the SearchView
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setQueryHint("Search the customer...");
        searchView.setSearchableInfo(Objects.requireNonNull(searchManager).getSearchableInfo(getComponentName()));
        searchView.setMaxWidth(Integer.MAX_VALUE);
        searchView.setIconifiedByDefault(false);
        searchView.requestFocus();

    }

<pre>

 <item
        android:id="@+id/search"
        android:icon="@drawable/ic_search"
        android:title="Search"
        app:actionViewClass="androidx.appcompat.widget.SearchView"
        app:showAsAction="always"/>

 public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search_menu, menu);
        // Associate searchable configuration with the SearchView
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setQueryHint("Search the customer...");
        searchView.setSearchableInfo(Objects.requireNonNull(searchManager).getSearchableInfo(getComponentName()));
        searchView.setMaxWidth(Integer.MAX_VALUE);
        searchView.setIconifiedByDefault(false);
        searchView.requestFocus();

    }

</pre>
Moonraker answered 4/3, 2020 at 6:50 Comment(0)
K
0

This is my usage combination:

searchView.onActionViewExpanded(); //focus searchView
searchView.setQuery(searchKey, false); //set query

//Hide Keyboard
InputMethodManager imm = (InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE);

imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
Kravits answered 26/9, 2023 at 15:22 Comment(0)
S
-2

android:iconifiedByDefault="true"

Above is the code in XML helped me make it expand and autofocus on search view when clicked:

Synergism answered 27/1, 2017 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.