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.
Related
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;
}
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
I have a SearchView in a ActionBar. Now I want to use it but I don't know how to use it. In the custom SearchView I use it like this:
SearchView sear = (SearchView) findViewById(R.id.searchView);
sear.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
#Override
public boolean onQueryTextSubmit (String query){
return false;
}
#Override
public boolean onQueryTextChange (String newText){
listDataAdapter.getFilter().filter(newText);
return true;
}
}
);
Now I have SearchView in an action bar which I show through code:
public class DataListActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.data_list_layout);
#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()));
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(getApplicationContext(),MainMenu.class);
startActivity(intent);
return true;
case R.id.action_Exit:
openExit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void openExit() {
}
How can I use SearchView ? I don't know how to use SearchView in action bar.
Set the onQueryTextListener(..) similar to what you have mentioned in your question on the searchView.
I am trying to create an action bar with search.
I am able to create the search bar. How I can get the text entered in the field to string? Is there any good code for action bar with search?
What I done is
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.menu_search).getActionView();
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String search = intent.getStringExtra(SearchManager.QUERY);
You can use the search bar as like this.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search)
.getActionView();
if (null != searchView) {
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
}
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
public boolean onQueryTextChange(String newText) {
// this is your adapter that will be filtered
return true;
}
public boolean onQueryTextSubmit(String query) {
//Here u can get the value "query" which is entered in the search box.
}
};
searchView.setOnQueryTextListener(queryTextListener);
return super.onCreateOptionsMenu(menu);
}
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