Android MenuItemCompat.getActionView is Deprecated - android

I am implementing a SearchView like this:
MenuItem search = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(search);
But this gives me an inspection report
'getActionView(android.view.MenuItem)' is deprecated
Now in the docs, it is mentioned:
This method was deprecated in API level 26.0.0
Use getActionView() directly.
So I tried this:
MenuItem search = menu.findItem(R.id.search);
SearchView searchView = (SearchView) getActionView(search);
But it still gives me the deprecated message. So what is the best practice?

Try:
MenuItem search = menu.findItem(R.id.search);
SearchView searchView = (SearchView) search.getActionView();
getActionView is deprecated?

Related

SearchView in Action bar with customize search button

In the menu, I have search view. when I clicked search in menu search view should expand with customized search button.like below image currently it is opening like normal search
I want to change the default submit button with custom button
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchView.setSubmitButtonEnabled(true);
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
Any help much appreciated.
ImageView b = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_go_btn);
b.setImageResource(R.drawable.your_button_drawable);
try this one to change default search submit button

how to set an expanding animation to a searchview

I want to set an expanding animation to a searchview and I try to apply the method that I found:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) item.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
final int searchBarId = searchView.getContext().getResources().getIdentifier("android:id/search_bar", null, null);
LinearLayout searchBar = (LinearLayout) searchView.findViewById(searchBarId);
searchBar.setLayoutTransition(new LayoutTransition());
return true;
}
However, the variable searchBar is always null and it always crashes at the line
searchBar.setLayoutTransition(new LayoutTransition());
I don't know what is causing the error. Can someboby help me out? Thanks!
I had the same problem. It turns out that you get null in your searchBar if you use android.support.v7.widget.SearchView
I changed it to android.widget.SearchView (recommended if supporting api > 11). Now I get the needed linear layout and animation works fine.
Edit
If you need a support version, you have to use proper id: LinearLayout searchBar = (LinearLayout) mFilterInput.findViewById(android.support.v7.appcompat.R.id.search_bar);

Android use support library to support SearchView

This is my code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
MenuItem searchItem = menu.findItem(R.id.searchMenuItem);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// searchView.set
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
}
My minimum API is 8.
I have this compile error
Call requires API level 11 (current min is 8):
android.widget.SearchView#setSearchableInfo
Note, that I can't change the minimum SDK.
You can use
android.support.v7.widget.SearchView
instead of
android.widget.SearchView
Your code could look like this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
MenuItem searchItem = menu.findItem(R.id.searchMenuItem);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
android.support.v7.widget.SearchView searchView = (android.support.v7.widget.SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
}
Also please read more about SearchViewCompat

How to use or workaround searchManager.getSearchableInfo() on Android API 7?

I can't find anywhere how to use the support v7 appcompat library with searchManager.getSearchableInfo().
#Override
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 menuItem = (MenuItem) menu.findItem(R.id.search_widget);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
searchView.setSubmitButtonEnabled(true);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName()));
return true;
}
Here's what I get:
Call requires API level 8 (current min is 7): android.app.SearchManager#getSearchableInfo MainActivity.java /MyApp/src/com/aat line 113 Android Lint Problem
And if I ignore the error with #TargetApi I get this:
11-21 22:14:48.108: E/AndroidRuntime(2387): java.lang.NoSuchMethodError: android.app.SearchManager.getSearchableInfo
Any help?
Have you tried actionBarShelock with SearchViewCompat for API 7?
Best alternative for SearchView for Android api level 7 and up?

Android SearchView: styling suggestions list

I have problems to style the suggestions list from a ActionBar SearchView (support v7).
Which style attribute I have to style to change the background and text color?
Ralph
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// Theme the SearchView's AutoCompleteTextView drop down. For some reason this wasn't working in styles.xml
SearchAutoComplete autoCompleteTextView = (SearchAutoComplete) searchView.findViewById(R.id.search_src_text);
if (autoCompleteTextView != null) {
autoCompleteTextView.setDropDownBackgroundResource(R.drawable.abc_search_dropdown_light);
}
}

Categories

Resources