SweSnow
SweSnow

Reputation: 17364

Expand and give focus to SearchView automatically

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?

Upvotes: 82

Views: 96659

Answers (20)

Minh Việt
Minh Việt

Reputation: 11

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

Upvotes: 0

KYU
KYU

Reputation: 61

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

Upvotes: 1

R.Sanchez
R.Sanchez

Reputation: 33

Kotlin

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

Upvotes: 2

Nasib
Nasib

Reputation: 1549

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

Upvotes: 1

Favour Felix Chinemerem
Favour Felix Chinemerem

Reputation: 1408

Call expandActionView() on the menuItem.

menuItem.expandActionView()

Upvotes: 1

Alex Curran
Alex Curran

Reputation: 8828

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.

Upvotes: 108

Nikhil Dinesh
Nikhil Dinesh

Reputation: 3409

android:iconifiedByDefault="true"

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

Upvotes: -2

chandan raj
chandan raj

Reputation: 51

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

Upvotes: 0

Javier
Javier

Reputation: 1685

For Appcompat Searchview you can use this method:

MenuItemCompat.expandActionView(mSearchMenuItem);

Upvotes: 2

Cesar Rodriguez
Cesar Rodriguez

Reputation: 153

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

Upvotes: 2

DISHA
DISHA

Reputation: 161

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

Upvotes: 0

manish poddar
manish poddar

Reputation: 484

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

Upvotes: 2

Pascalius
Pascalius

Reputation: 14659

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

Upvotes: 66

saibaba vali
saibaba vali

Reputation: 2671

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.

Upvotes: 6

Wops
Wops

Reputation: 993

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

Upvotes: 0

Joshua Michael Calafell
Joshua Michael Calafell

Reputation: 3107

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)

Upvotes: 2

Parth mehta
Parth mehta

Reputation: 1436

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"

Upvotes: 8

riwnodennyk
riwnodennyk

Reputation: 8258

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

mSearchView.onActionViewExpanded()

Upvotes: 28

Kai Burghardt
Kai Burghardt

Reputation: 1503

This worked for me:

menu.expandActionView();

Upvotes: 4

JavierCane
JavierCane

Reputation: 2412

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

Upvotes: 113

Related Questions