Android how to focus ActionBar searchView
Asked Answered
E

11

55

Hello i need to put some text inside the searchView and focus/expand the searchView widget.

this is what i tried but it doesn't work

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
...
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    searchView.setQuery(this.filtro, false);
    searchView.setFocusable(true);
    searchView.requestFocus();
    searchView.requestFocusFromTouch();
Ellene answered 10/4, 2012 at 13:44 Comment(0)
Z
123

Try using searchView.setIconified(false). This worked for me:

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ActionBar actionBar = getActionBar();
    SearchView searchView = new SearchView(this);
    actionBar.setCustomView(searchView);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    searchView.setQuery("test",true);
    searchView.setFocusable(true);
    searchView.setIconified(false);
    searchView.requestFocusFromTouch();
}
Zeist answered 25/4, 2012 at 14:12 Comment(3)
Neat. Looks much cleaner than overriding onCreateOptionsMenu and setting up xmls.Labarum
"But I'm already calling setIconifiedByDefault(false)," you say. "This must not apply to me, " you say. Don't be like me, kids. Call setIconified(false) instead of chasing your tail for an hour.Electrograph
Strange but setIconified(false) really does the job!Stollings
P
4

Here is another solution.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    MenuItem menu_search = menu.findItem(R.id.menu_search);


    menu_search.setOnActionExpandListener(new OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            // Do something when collapsed
            return true;  // Return true to collapse action view
        }

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            //get focus
            item.getActionView().requestFocus();
            //get input method
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            return true;  // Return true to expand action view
        }
    });
    return true;
}
Phylogeny answered 17/12, 2012 at 13:52 Comment(3)
Thanks, mate! Only yours version of keyboard toggle method worked correctly with ActionBarSherlock's search view, after tens of other failed tries.Paralytic
item.expandActionView(); is enough for me. :)Gerlachovka
After exception java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.setOnActionExpandListener() I changed the code a little, but nothing happened.Gamba
P
2

This works for me:

        @Override
        public boolean onMenuItemActionExpand(MenuItem item)
        {
            if (sm.isMenuShowing()) {
                sm.toggle();
            }
            searchView.setIconified(true);
            KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, 32);
            searchView.onKeyDown(32, event);
            return true;
        }
Pronator answered 22/10, 2013 at 17:50 Comment(0)
G
1

For me the following works:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    ...
    MenuItem miSearch = menu.findItem(R.id.menu_search);
    SearchView mSearchView = (SearchView) miSearch.getActionView();

    mSearchView.setOnSearchClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mSearchView.setFocusable(true);
            mSearchView.requestFocusFromTouch();
        }
    });

I use android.support.v7.widget.SearchView. When tried to set mSearchView.setIconified(false); it entered into an infinite loop.

Gamba answered 28/12, 2016 at 11:8 Comment(2)
This one worked for me too. I'm using android.support.v7.widget.SearchView Thank you pal. After two days finally one answer works! :) I also added: searchView.setIconified(false);Noam
Thanks, @Mohsen! Yes, you are right, sometimes Android is too annoying. Good luck!Gamba
S
0
searchView.setQuery("typeAnything",true);
searchView.setFocusable(true);

will do the trick.

Stonewort answered 14/12, 2015 at 8:6 Comment(1)
For me that was not enough (I use android.support.v7.widget.SearchView).Gamba
L
0
public void setIconified(boolean iconify) {
    if (iconify) {
        onCloseClicked();
    } else {
        onSearchClicked();
    }
}

this is code in SearchView

setIconified() is worked for me

Lucullus answered 16/4, 2018 at 2:43 Comment(0)
T
0

In the activity xml you can use this line:

android:iconifiedByDefault="false"
Tenancy answered 4/6, 2018 at 1:56 Comment(0)
P
0

In case SearchView is made by menu configuration, I expand the action view using menu item.

private var searchMenuItem: MenuItem? = null

// create search menu item
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.search_menu, menu)
    searchMenuItem = menu?.findItem(R.id.action_search)
    val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
    (searchMenuItem?.actionView as? SearchView)?.apply {
        setSearchableInfo(searchManager.getSearchableInfo(componentName))
    }
    return super.onCreateOptionsMenu(menu)
}


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_search)
    setSupportActionBar(findViewById(R.id.toolbar))
    // Execute 'expandActionView' delayed 
    // because I want to focus field only when first start.
    Handler().postDelayed({
        // show field and focus it.
        searchMenuItem?.expandActionView()
    }, 500)
}
Paluas answered 8/2, 2019 at 18:20 Comment(0)
E
0

I implemented it in onCreateOptionMenu and it worked. You don't need to using setIconified(false);

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);

        // Associate searchable configuration with the SearchView
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setMaxWidth(Integer.MAX_VALUE);
        searchView.setOnClickListener(view -> menu.findItem(R.id.action_sleep_timer).setVisible(false));
        searchView.setOnCloseListener(() -> {
            menu.findItem(R.id.action_sleep_timer).setVisible(true);
            return false;
        });
        searchView.setOnSearchClickListener(view -> {
            menu.findItem(R.id.action_sleep_timer).setVisible(false);
            searchView.requestFocus();
        });
return true;
    }
Eastwards answered 24/4, 2019 at 2:9 Comment(0)
I
0

use MenuItem's expandActionView()

override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_search, menu)
        menuItemSearch = menu.findItem(R.id.action_search)
        searchView = menuItemSearch?.actionView as SearchView
        // ...
        menuItemSearch?.expandActionView()
        return true
    }

you need to add this line:

menuItemSearch?.expandActionView()
Inunction answered 4/6, 2021 at 10:1 Comment(0)
D
0

For me, the approach from @farmer.chs only worked after I added a delay:

 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
 super.onViewCreated(view, savedInstanceState)

 activity?.let {
            searchBar?.postDelayed({
                  searchBar.isIconified = false
                  searchBar.isFocusable = true
                  searchBar.requestFocusFromTouch()
            }, 100)
        }

P.S. this code is called in the Fragment

Desdee answered 22/2, 2022 at 9:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.