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 extendActionBarActivity
instead ofFragmentActivity
directly - Each activity that uses the
ActionBarCompat
should declare its theme as inheriting from theActionBarCompat
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.
Changing namespace of
actionViewClass
fromandroid:actionViewClass
toapp:actionViewClass
Implementing
android.support.v7.widget.SearchView.OnQueryTextListener
interface for current activity.Directly use
setOnQueryTextListener
instead ofSearchViewCompat.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.
Changing namespace of
actionViewClass
fromandroid:actionViewClass
toapp:actionViewClass
Implementing
android.support.v7.widget.SearchView.OnQueryTextListener
interface for current activity.Directly use
setOnQueryTextListener
instead ofSearchViewCompat.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
Clear Application Cache on Exit in Android
How to Retrieve HTML Content from Webview (As a String)
Could Not Find Com.Android.Tools.Lint:Lint-Gradle Android Studio 3
How to Detect Wifi Tethering State
Android Studio 3.0 Compile Issue (Cannot Choose Between Configurations)
Detecting Gps On/Off Switch in Android Phones
Viewing Excel Files in My Android App
How to Calculate Azimuth, Pitch, Orientation When My Android Device Isn't Flat
Data Directory Has No Read/Write Permission in Android
How to Customize the Action Mode's Color and Text
Firebase: How to Set Default Notification Channel in Android App
What Is the Android UI Thread Stack Size Limit and How to Overcome It
Getting Context in Androidtestcase or Instrumentationtestcase in Android Studio's Unit Test Feature