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);
Related
Here is what I did so far -
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
super.onCreateOptionsMenu(menu, inflater);
getActivity().setTitle("DASHBOARD");
SearchManager searchManager = (SearchManager)getContext(). getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setIconifiedByDefault(false);
SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener()
{
#Override
public boolean onQueryTextChange(String newText)
{
// this is your adapter that will be filtadapter.getFilter().filter(newText);
System.out.println("on text chnge text: "+newText);
return true;
}
#Override
public boolean onQueryTextSubmit(String query)
{
// this is your adapter that will be filtered
adapter.getFilter().filter(query);
System.out.println("on query submit: "+query);
return true;
}
};
searchView.setOnQueryTextListener(textChangeListener);
}
Here code Search view in action bar, it only filter the one name other does not work . The code adapter class code is below . Actionbar doing in fragment
adapter = new FavouritesAdapter(getContext(),distanceArr,contactName,status,leadDetailsId,addressLine1,addressLine2,city,state,zipCode);
list.setAdapter(adapter);
list.setTextFilterEnabled(true);
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.
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);
}
I have an activity that shows search in title bar perfectly, but right now i want to get the text entered in search in action bar to a string. Please help !
Her is my menu/xml
<?xml version="1.0" encoding="utf-8"?>
<item android:id="#+id/action_search"
android:title="Search"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView"
/>
</menu>
Her is the code in onCreate
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
Have you used searchView? You can use following code:
#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 you can get the value "query" which is entered in the search box.**
}
};
searchView.setOnQueryTextListener(queryTextListener);
return super.onCreateOptionsMenu(menu);
}
If you are using a Toolbar you can do this:
MenuItem menuItem = getToolbar().getMenu().findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
Log.i("well", " this worked");
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
Log.i("well", " this worked");
return false;
}
});
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