Android use support library to support SearchView - android

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

Related

Android MenuItemCompat.getActionView is Deprecated

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?

How to make Action Bar SearchView text to contain search icon

I'd like to have small search icon in text hint like that:
but I get it like so:
I am using:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.work, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
return true;
}

Toolbar is showing on left of SearchView in android

I want to show Back button but distorted ToolBar is coming on left as in the following image.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
TextView tv = (TextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
tv.setTextColor(getResources().getColor(android.R.color.white));
tv.setHintTextColor(getResources().getColor(android.R.color.white));
tv.setHint(getString(R.string.action_search));
ImageView imgCloseIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
imgCloseIcon.setImageResource(R.drawable.cross_btn);
searchView.setOnQueryTextListener(this);
}
How to show Back button and hide distorted ToolBar ??
I have used the below code and its working fine for me,
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(queryTextListener);

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?

Adding SearchView Widget. SearchManager error

I used this code from the android developing site to implement the search bar, but I get red error lines that says Unreachable code and the only fix is to remove the line of code. I am not sure what is wrong here.
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
// Do not iconify the widget; expand it by default
The line that gives me the errors is:
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
I was facing the same problem. Place the line
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); after return.
Example: `
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
return super.onCreateOptionsMenu(menu);
}
`

Categories

Resources