Setting Background for Actionbar SearchItem - android

I would like to set the background (something like EditText Background) to my ActionBar SearchItem. That's the current action bar.
I would like to set this background drawable to that.
Here's how I create the searchItem
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.guest_list, menu);
MenuItem searchItem = menu.findItem(R.id.search);
SearchManager searchManager = (SearchManager) getActivity()
.getSystemService(Context.SEARCH_SERVICE);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getActivity().getComponentName()));
searchView.setIconified(true);
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
setFilter(query);
return false;
}
#Override
public boolean onQueryTextChange(String query) {
setFilter(query);
return false;
}
});
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getActivity().getComponentName()));
searchView.setIconified(true);
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
setFilter(query);
return false;
}
#Override
public boolean onQueryTextChange(String query) {
setFilter(query);
return false;
}
});
}
}
I've tried setting background by searchView.setBackgroundDrawable but I am not getting what I like to implement.

Please check below link because it explains everything about searchbar in actionbar:
Changing the background drawable of the searchview widget

Related

My app doesn't load toolbar options

I'm doing a search bar, but I found a problem, because if I put the code at the end of MainActivity it doesn't load options.
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
SearchManager manager = ( SearchManager ) getSystemService ( Context.SEARCH_SERVICE );
SearchView search = (SearchView) menu.findItem(R.id.action_search).getActionView();
search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String query) {
return false;
}
});
}}
What should I do? Did I do something wrong?
Thanks in advance
Your logic is after the return statement, hence, never reached. Try this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
SearchManager manager = ( SearchManager ) getSystemService ( Context.SEARCH_SERVICE );
SearchView search = (SearchView) menu.findItem(R.id.action_search).getActionView();
search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String query) {
return false;
}
});
}
return super.onCreateOptionsMenu(menu);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
That is never reached, your return statement is before that. The rest looks fine.

How to use searchable.xml

I read lots of posts on this but I actually don't understand how it works:
I created a searchable.xml in the xml folder:
Then I referenced it to my Manifest like this:
I can't figure out how can I use it in the search code, I tried with the SearchManager but without success:
SearchView searchView;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate( R.menu.main, menu);
MenuItem myActionMenuItem = menu.findItem( R.id.action_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) myActionMenuItem.getActionView();
ComponentName componentName = new ComponentName(this, MainActivity.class);
searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String s) {
EventBus.getDefault().post(new SearchQueryEvent(s));
return false;
}
});
return true;
}

SearchView in actionBar

I've followed this code to implement SearchView inside ActionBar (as an item inside the menu).
But when i reach this line
mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
getActionView() return a null value.
I can't figure out what is wrong.
Try replacing
mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
with
mSearchMenuItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(mSearchMenuItem);
Try this..
private SearchView mSearchView;
....
....
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) searchItem.getActionView();
setupSearchView(searchItem);
return super.onCreateOptionsMenu(menu);
}
private void setupSearchView(MenuItem searchItem) {
if (isAlwaysExpanded()) {
mSearchView.setIconifiedByDefault(false);
} else {
searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
}
mSearchView.setOnQueryTextListener(this);
}
public boolean onQueryTextChange(String newText) {
//Query
return false;
}
public boolean onQueryTextSubmit(String query) {
//query
return false;
}
public boolean onClose() {
return false;
}
protected boolean isAlwaysExpanded() {
return false;
}
And don't forget to implement the searchview...
implements SearchView.OnQueryTextListener

android searchView in actionbar filter issue

i have searchview in the action bar it is not working i try to do this like as
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// Inflate menu to add items to action bar if it is present.
inflater.inflate(R.menu.menu_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
android.support.v7.widget.SearchView searchView =
(android.support.v7.widget.SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
return true;
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
}
but it give me error that android.support.v7.widget.SearchViewOnQueryTextListener in searchview can not be apply
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// Inflate menu to add items to action bar if it is present.
inflater.inflate(R.menu.menu_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
android.support.v7.widget.SearchView searchView =
(android.support.v7.widget.SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return true;
}
Return should be at the end,
last three braces where not in correct order.

Where to Auto-Collapse ActionBar SearchView

When a user submits a app wide search in the Search Widget in the ActionBar, I want it to auto collapse after submit. Submit basically opens up the SearchableActivity. Currently, if they close the search results activity (SearchableActivity), the Search Widget is still open. I just want it closed; and with no text.
From reading around, I seem to think that the answer is this
`searchView.onActionViewCollapsed() ;'
however, I am not sure where to put it?
Here is my code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.menu_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setupNewSearchView(searchItem, searchManager);
}
return true;
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupNewSearchView(final MenuItem searchItem,
SearchManager searchManager) {
android.widget.SearchView searchView = (android.widget.SearchView) searchItem
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem About = menu.findItem(R.id.About);
MenuItem Login = menu.findItem(R.id.Login);
MenuItem Logout = menu.findItem(R.id.Logout);
MenuItem Settings = menu.findItem(R.id.Settings);
MenuItem Search = menu.findItem(R.id.menu_search);
MenuItem Add = menu.findItem(R.id.Add);
Add.setVisible(false);
if (LoggedStatus == true) {
// show the log out option
Logout.setVisible(true);
Login.setVisible(false);
Settings.setVisible(true);
} else {
// show the log in option
Logout.setVisible(false);
Login.setVisible(true);
Settings.setVisible(false);
}
About.setVisible(true);
return true;
}
Note, I am using SherlockActionBar.
I did this:
searchView.setOnQueryTextListener(new OnQueryTextListener() {
public boolean onQueryTextChange(String arg0) {
// TODO Auto-generated method stub
return false;
}
public boolean onQueryTextSubmit(String arg0) {
if (searchItem != null) {
searchItem.collapseActionView();
}
return false;
}
});
and in my xml menu file:
android:showAsAction="always|collapseActionView"
it works.

Categories

Resources