Searchview Getactionview Returning Null

SearchView getActionView returning null

Today I had the same problem and I think I solved it. It turns out I did couple of things that were not exactly as per the ActionBarCompat contract:

  • Each activity that uses the ActionBarCompat should extend ActionBarActivity instead of FragmentActivity directly
  • Each activity that uses the ActionBarCompat should declare its theme as inheriting from the ActionBarCompat themes.

Both of those I found watching the explanation video from Google.

Now my searchView is not null anymore.

(SearchView) MenuItemCompat.getActionView(item) always null

MenuItemCompat.getActionView(MenuItem item) is deprecated in API level 26.1.0.

Now the recommended way is calling getActionView() directly on the menu item:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();

// ...
}

MenuItemCompat.getActionView always returns null

Finally I found the solution.

  1. Changing namespace of actionViewClass from android:actionViewClass to app:actionViewClass

  2. Implementing android.support.v7.widget.SearchView.OnQueryTextListener interface for current activity.

  3. Directly use setOnQueryTextListener instead of SearchViewCompat.setOnQueryTextListener

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

    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    if (searchView != null) {
    searchView.setOnQueryTextListener(this);
    }

    return super.onCreateOptionsMenu(menu);
    }

searchMenuItem.getActionView() returning null

I saw that you have app:showAsAction="always" the app namespace means that you are using Appcompat v7 library ...

Appcompat library has it owns method for menu items as static method in MenuCompat/MenuItemCompat classes (and you should use 'em like instead menu.methodXXX() use MenuCompat.methodXXX(menu) )

Now, to define a actionViewClass(and others attributes added in api newer then 11) in menu you should use the app namespace for this instead android namespace

so android:actionViewClass should become app:actionViewClass

in the code you should use MenuItemCompat.getActionView(searchItem) instead searchItem.getActionView()

remeber to add namespace app in root element of menu xml file like xmlns:app ="http://schemas.android.com/apk/res-auto"

also small hint (as you are using 11 as min sdk your code should works fine but ...) replace android.widget.SearchView to android.support.v7.widget.SearchView as it(standard SearchView) not works in the same way on different API versions from 11 to newest one(also you will get method not found if you use methods added in API > 11 to SearchView on devce with API 11)

MenuItemCompat.getActionView always returns null

Finally I found the solution.

  1. Changing namespace of actionViewClass from android:actionViewClass to app:actionViewClass

  2. Implementing android.support.v7.widget.SearchView.OnQueryTextListener interface for current activity.

  3. Directly use setOnQueryTextListener instead of SearchViewCompat.setOnQueryTextListener

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

    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    if (searchView != null) {
    searchView.setOnQueryTextListener(this);
    }

    return super.onCreateOptionsMenu(menu);
    }

getActionView returning null

Try using this instead:

android:actionViewClass="android.widget.SearchView"


Related Topics



Leave a reply



Submit