Expandir y dar foco a SearchView automáticamente

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

Mi problema es que el 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.

¿Cómo debería solucionarse esto?

preguntado el 29 de julio de 12 a las 15:07

18 Respuestas

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() en él también.

contestado el 04 de mayo de 20 a las 07:05

I'm not sure to be honest, sorry. - Alex Curran

Añadiendo un requestFocus() llamar onPrepareOptionsMenu(..) hizo el truco para mí. - El sucio calvinista

requestFocus doesn't work for me. To "focus" you can call setIconified con false - Pedro Chaula

@SweSnow You have to set both ...searchView.setIconifiedByDefault(false); searchView.requestFocus(); - pallav bohara

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 appeared - Riccardo

If you want to have it iconifiedByDefault, esto funcionó para mí. setFocusable y setIconified Se necesitan.

    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();

respondido 16 mar '17, 11:03

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

respondido 30 mar '13, 17:03

Si utiliza AppCompat, en lugar de llamar expandActionView() directamente en el searchMenuItem, you should use this instead: MenuItemCompat.expandActionView(searchMenuItem). - Carlos Madere

@CharlesMadere way is the only one that worked for me, API 23 - Mateo

más 1 por searchMenuItem.expandActionView(); - Vlad

MenuItemCompat.expandActionView(searchMenuItem) is deprecated now - afortunado

This solution is deprecated. - Favour Felix Chinemerem

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

mSearchView.onActionViewExpanded()

Respondido el 02 de diciembre de 13 a las 18:12

What does using in layout mean? - pascalius

I mean that you reference to it as a Viewno, MenuItem - riwnodennyk

that didn't work for me. Calling setIconified() did though. - señorarado

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??? - Tushar Srivastava

Kotlin

var searchView = menu.findItem(R.id.action_search).actionView as SearchView
searchView.isIconified = true

contestado el 26 de mayo de 21 a las 08:05

@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 Me gusta esto:

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;
    }
});

Respondido el 10 de Septiembre de 17 a las 06:09

Llame a expandActionView() on the menuItem.

menuItem.expandActionView()

contestado el 21 de mayo de 20 a las 03:05

I was having a hard time doing this with SearchView widget, Pero el 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

Respondido 06 Feb 21, 09:02

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.

contestado el 18 de mayo de 16 a las 12:05

For Appcompat Searchview you can use this method:

MenuItemCompat.expandActionView(mSearchMenuItem);

Respondido el 19 de Septiembre de 17 a las 14:09

Esto funcionó para mí:

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"

Respondido 08 Feb 15, 08:02

utilizar este

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);

Respondido 12 Jul 17, 13:07

android:iconifiedByDefault="true"

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

respondido 10 mar '20, 06:03

Esto funcionó para mí:

menu.expandActionView();

Respondido 08 ago 13, 14:08

but close icon is not visible initially in expanded form. stackoverflow.com/questions/27706192/… - Shadow

Puede utilizar el SearchView#setIconified() method on a SearchView handle in your Java code. More here:

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

También puedes hacer uso de SearchView#setIconifiedByDefault(). Más información aquí:

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

contestado el 27 de mayo de 15 a las 14:05

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

Respondido 30 Jul 15, 12:07

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

Respondido 24 ago 17, 19:08

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

respondido 04 mar '20, 06:03

It is working successfully for me, so that you can try it. - chandan-raj

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.