SearchView does not get focus

EDIT For anyone who wondered, my problem was to use android.widget.SearchViewinstead android.support.v7.widget.SearchView. Hope this helps someone else with the same issue!

Original publication

I am trying to implement SearchView in an Android ActionBar according to the official guide: http://developer.android.com/training/search/setup.html

After failing to find the problem, I finally split into the most basic Hello World application and was surprised to find that the error still persists in the minimal application!

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

Here is the relevant code, although I literally just created a new Android application and added an item to the / menu _main.xml menu. 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" />
+4
source share
1 answer

use this 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;
+1
source

All Articles