In my application i am trying to add an searchView in actionbar. But while clicking on the search icon searchView is expanding to the entire actionbar while clicking the key down it is coming to normalView.
While clicking on the search icon. It is coming like this..
But I want a behavior like this..
options_menu.xml
<item
android:id="#+id/search"
myapp:actionViewClass="android.support.v7.widget.SearchView"
myapp:showAsAction="ifRoom|collapseActionView"
android:icon="#drawable/ic_action_search"
android:title="#string/search_title"/>
onCreateOptions() method
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.options_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
mSearchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
mSearchView.setBaselineAligned(false);
return super.onCreateOptionsMenu(menu);
}
please help me..Thanks in advance...
Add this line in your onCreateOptionsMenu()
searchView.setIconifiedByDefault(false);
Any particular reason to write the property twice in menu.xml ?
I just commented the code like this...Now it is working properly...
case R.id.search:
//onSearchRequested();
return true;
Related
I am having a weird problem. In my MainActivity toolbar I have an action search and this is how I use it:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(this);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(
new ComponentName(this, SearchableActivity.class)));
searchView.setIconified(false);
return true;
}
but the problem here is that when my app starts the keyboard pops up before I even click the search button in the toolbar. I tried to force the keyboard to hide in the manifest I added this to my MainActivity:
android:windowSoftInputMode="stateAlwaysHidden"
I even tried this:
android:windowSoftInputMode="stateHidden"
but they keyboard still pops up. I don't have any EditText or anything I only have an image and a FAB in my MainActivity.
1) android:windowSoftInputMode="adjustPan|stateHidden"
2) getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
3) mSearchView.clearFocus();
I need the suggestion for using the best way to show searchview with custom header.
My headerlayout should initially look like this
And after clicking the search button on the right of the header, it must replace header title and menu icon part with the search input field like this.
pressing back in this state, should direct to initial state.
How can I do this?
in Menu Resource directory
search_country_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/abc_ic_search"
app:showAsAction="always|collapseActionView"
android:orderInCategory="0"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
**In Activity's onCreateOptionmenu Method **
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_country_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
menu.findItem(R.id.action_search).expandActionView();
searchView.setQueryHint(getResources().getString(R.string.select_country));
searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String arg0) {
searchView.clearFocus();
return true;
}
#Override
public boolean onQueryTextChange(String arg0) {
etSearch.setText(arg0);
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
Do you want to customize the SearchViewor is a standard one fine for you?
If it is I suggest looking into the ActionBar with an ActionView
Android ActionBar documentation about adding an ActionView
In my app I am using an android.support.v7.widget.SearchView (from support v7 library) to provide search from action bar.
I setup this view in MainActivity, with the following code:
MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SupportMenuItem searchMenuItem = (SupportMenuItem) menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
searchView.setQueryRefinementEnabled(true);
searchView.setIconifiedByDefault(true);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
// OnQueryTextListener body
...
});
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
Then in a Fragment i need to ensure that the SearchView is collapsed:
MyFragment.java
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
MenuItem searchMenuItem = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
searchView.clearFocus(); // Try to prevent SearchView to steal focus. No luck!
searchView.setIconified(true);
}
#Override
public void onResume() {
super.onResume();
setHasOptionsMenu(true);
// other stuff
}
The problem is that not only the SearchView does not collapse (I tried also with MenuItemCompat.collapseActionView(searchMenuItem) with no success: it returns always false), but also the SearchView "steals" focus at every fragment transaction (e.g. replace one fragment with another), so when the "new" fragment is displayed, the SearchView has focus, so it shows its suggestions.
I also noticed that if I do not call setHasOptionsMenu then the problem of "stealing focus" does not occur.
Is there something I am doing wrong?
I use searchview in my Android app and I would like to add a button that user presses to start the search. Based on what I read on the Internet, I can use setSubmitButtonEnabled to invoke a submit button instead of putting a button in the layout file. Here's my code:
public void setSubmitButtonEnabled (boolean enabled) {
}
I put my setSubmitButtonEnabled in the menu inflater as below:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mylist, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
setSubmitButtonEnabled(true);
return true;
}
Apparently I am not doing it right because when I launch my app, I don't see any submit button on the screen. What's missing or what's wrong in my code? Is the submit button supposed to appear on the keypad or on the screen? Thank you.
You need to call
searchView.setSubmitButtonEnabled(true)
Why would you create your own version with no body and expect it to do anything?
For this use
searchView=findViewById(R.id.search_box);
searchView.setSubmitButtonEnabled(true);
For submit button icon use
app:goIcon="#drawable/ic_arrow_forward_black_24dp"
For SearchView default expanded
app:iconifiedByDefault="false"
I am trying to use SearchViewCompat with ActionBarSherlock in an API 8 app.
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.add("Search")
.setIcon(isLight ? R.drawable.ic_search_inverse : R.drawable.ic_search)
.setActionView(R.layout.collapsible_edittext);
item.setShowAsAction(
MenuItem.SHOW_AS_ACTION_ALWAYS |
MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
// To use SearchViewCompat, I need to add it to the Menu item as well:
View searchView = SearchViewCompat.newSearchView(this);
// ...
SearchViewCompat.setOnQueryTextListener(...);
// ...
item.setActionView(searchView);
Please note that both the top and bottom code needs to call setActionView(). Does that mean it is not possible to do search?
If you are using the ActionBarSherlock Library ver 4.2, you can replace the API 11 SearchView Widget with a ActionBarSherlock SearchView Widget to make it backward compatible:
search.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_search"
android:icon="#drawable/ic_action_search"
android:title="#string/description_search"
android:orderInCategory="0"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"
android:showAsAction="ifRoom|collapseActionView" />
</menu>
Activity class
//IMPORTANT!!!
import com.actionbarsherlock.widget.SearchView;
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getSupportMenuInflater().inflate(R.menu.search, menu);
setupSearchMenuItem(menu);
return true;
}
private void setupSearchMenuItem(Menu menu) {
MenuItem searchItem = menu.findItem(R.id.menu_search);
if (searchItem != null) {
SearchView searchView = (SearchView) searchItem.getActionView();
if (searchView != null) {
SearchManager searchManager =
(SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
}
}
}
}
What is the actual problem? SearchViewCompat will return null for pre-HC devices since the SearchView widget does not exist. This means you will have to provide your own custom action view that imitates the HC SearchView.
You can also backport the SearchView component from the Android sources and use that.
Otherwise, you can just use the existing search interfaces Android has, in which case for HC+ devices you use the action view to perform a search but on Froyo and Gingerbread devices the user clicks on the search icon and a search bar animates from the top.
Hope this helps.
At some point in your Activity:
public class HomeActivity extends SherlockFragmentActivity implements
SearchView.OnQueryTextListener {
// ...
SearchView searchView = (com.actionbarsherlock.widget.SearchView)
actionBarCustom.findViewById(R.id.search);
SearchManager sm = (SearchManager)getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(sm.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(this);
And then filter your list adapter:
#Override
public boolean onQueryTextSubmit(String query) {
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
mAdapter.getFilter().filter(newText.trim());
return false;
}
This way, your list adapter must implement filterable.
Better to use MenuItemCompat,I think this is helpful for you
getMenuInflater().inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.search);
SearchManager searchManager =(SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView)MenuItemCompat.getActionView(searchItem);
SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
searchView.setSearchableInfo(info);