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.
Related
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 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.
After I tap the Search icon, I want the SearchView widget to expand, and cover up the content below it. Inbox masks the current content by overlaying an opaque gray color above it. How do I do the same thing? I am using the SearchView widget.
I achieved it by simply switching to another fragment when the searchview is expanded, and switch back when it is collapsed.. The code looks something like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
//Setup the search widget
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView mSearchView = (SearchView) menuItem.getActionView();
//Not sure if you need this line for the searchView to be expandable
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
//Update contents of the fragment
}
#Override
public boolean onQueryTextChange(String s) {
//Update contents of the fragment
}
});
MenuItemCompat.setOnActionExpandListener(menuItem,
new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
//Switch to search fragment
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//Switch back to previous fragment
}
})
;
return super.onCreateOptionsMenu(menu);
}
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.
When you enter in searchview mode with the action bar, the home button is changed to the manifest android:icon drawable for some reason. Is there a way to keep it unchanged or to change it back to the android:logo drawable?
Here's what I'm doing:
#Override
public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater)
{
getSherlockActivity().getSupportMenuInflater().inflate(R.menu.places_menu, menu);
SearchManager searchManager = (SearchManager) getSherlockActivity().getSystemService(Context.SEARCH_SERVICE);
final SearchView searchView = (SearchView) menu.findItem(R.id.item_menu_search).getActionView();
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener()
{
public boolean onQueryTextChange(String newText)
{
// this is your adapter that will be filtered
mAdapter.getFilter().filter(newText);
return true;
}
public boolean onQueryTextSubmit(String query)
{
// this is your adapter that will be filtered
mAdapter.getFilter().filter(query);
return true;
}
};
if (null != searchView )
{
searchView.setSearchableInfo(searchManager.getSearchableInfo(getSherlockActivity().getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(queryTextListener);
// CLOSE SEARCH WHEN SOFT KEYBOARD DISSMISSES
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean queryTextFocused) {
if(!queryTextFocused) {
menu.findItem(R.id.item_menu_search).collapseActionView();
searchView.setQuery("", false);
}
}
});
}
super.onCreateOptionsMenu(menu, inflater);
}
It seems to be possible by calling setIcon() with the logo.
ActionBar actionBar = getActionBar();
actionBar.setLogo(R.drawable.ab_logo);
actionBar.setIcon(R.drawable.ab_logo);