Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SearchView not getting focus

EDIT For anyone wondering, my problem was I was using android.widget.SearchView instead of android.support.v7.widget.SearchView. I hope this helps anyone else with the same problem!

Original Post

I'm trying to implement SearchView in the Android ActionBar as per the official guide: http://developer.android.com/training/search/setup.html

After failing to find the problem, I finally stripped down to the most basic Hello World application and found to my surprise that the bug still persists in a minimal app!

Here's the bug: The search icon appears in the menu bar, no problem. When I click it, the search bar expands (as expected) but there is no cursor and no soft keyboard appears. (I want to post a picture but my reputation is too low :(

Here is the relevant code, although I literally just created a new Android Application and added the item to menu/menu_main.xml. MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

menu_main.xml

<item android:id="@+id/action_search"
    android:icon="@android:drawable/ic_menu_search"
    android:title="@android:string/search_go"
    app:showAsAction="collapseActionView|ifRoom"
    app:actionViewClass="android.widget.SearchView" />
like image 834
P Chan Avatar asked Feb 22 '15 01:02

P Chan


2 Answers

use this is menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="your activity context here"><item
    android:id="@+id/mi_search"
    android:title="@string/search"
    android:orderInCategory="2"
    android:icon="@drawable/searchicon"
    app:showAsAction="collapseActionView|ifRoom"
    app:actionViewClass="android.support.v7.widget.SearchView" />

and this code in onCreateOptionsMenu(Menu menu):

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    MenuItem searchItem = menu.findItem(R.id.mi_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

    searchView.setOnQueryTextListener(this);

    return super.onCreateOptionsMenu(menu);
}

in onOptionsItemSelected():

case R.id.mi_search:
    onSearchRequested();
    break;
like image 60
rogerwar Avatar answered Oct 29 '22 13:10

rogerwar


Here in 2022 this question still applies but the answer must be updated if you use androidx (which you should).

The menu item must use androidx.appcompat.widget.SearchView instead of either android.widget.SearchView or android.support.v7.widget.SearchView.

<item android:id="@+id/action_search"
    android:icon="@android:drawable/ic_menu_search"
    android:title="@android:string/search_go"
    app:showAsAction="collapseActionView|ifRoom"
    app:actionViewClass="androidx.appcompat.widget.SearchView" />

The onCreateOptionmenu override must also be updated to use the androidx class. Here is the Kotlin code for that:

override fun onCreateOptionsMenu(menu: Menu): Boolean { 
    menuInflater.inflate(R.menu.main, menu)
    val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
        menu.findItem(R.id.search).actionView as androidx.appcompat.widget.SearchView).apply {
        setSearchableInfo(searchManager.getSearchableInfo(componentName))
    }
}
like image 1
Loudenvier Avatar answered Oct 29 '22 14:10

Loudenvier