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"
Related
I have the search view at the top of my layout I want when I( clicked the button the cursor should show in the search bar.
here is the image enter image description here
Create an ID for the SearchView and use this code to create actions within it.
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.beneficiary_search, menu);
MenuItem search = menu.findItem(R.id.searchicon);
SearchView searchView = (SearchView) search.getActionView();
searchView.requestFocus()
searchView.setOnQueryTextListener(this);
return true;
}
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 am implementing a searchview for my app. Here is how i set my searchview:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
this.menu = menu;
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String query) {
loadHistory(query);
return true;
}
});
return true;
}
I want to show search suggestions to user when user starts typing. I can do this using a listview:
private void loadHistory(String query) {
// query db etc...
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchView search = (SearchView) menu.findItem(R.id.search).getActionView();
search.setSuggestionsAdapter(new ExampleAdapter(this, cursor, items));
}
But suppose I do not want to show search suggestions as a listview, and show them in a custom view, where I can add some more stuff to my layout other then just search suggestions. For example, I want to show the following custom view instead of suggested searches listview:
How can I do that? There is a function for setting suggestions adapter, setSuggestionsAdapter(adapter), but I could not find a function like setCustomSuggestionsView(view).
Thanks.
I think what you need is a recycler view for your results. Please see this answer for more information. A recycler view gives you all the freedom layout wise. To use more viewtypes see this post. Hope this helps. I am not familliar enough with your case from your description.
Recently I've added a tool for searching inside fragments. Some fragments have ListViews, so when a user types a search text, a ListView filters rows.
This is done with ActionBar after this tutorial.
I attached listeners from several fragments to a MainActivity, so when a user types a text at the top of the screen, it filters rows in a fragment. But in other fragments there is no need to search for anything, so a magnifying glass should be hidden.
However a magnifier is drawn at every screen. I also tried to not show it in activity and draw only in a fragment with this code:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main_invitro, menu);
menu.clear();
// Associate searchable configuration with the SearchView
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.clearFocus();
super.onCreateOptionsMenu(menu,inflater);
}
But a magnifier stays in other fragments, also it is possible to type a text at the top.
Is there a correct way to show a magnifier only in some fragments?
I've got an answer at another forum.
In MainActivity:
boolean isVisible;
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.action_search).setVisible(isVisible);
return true;
}
To update an ActionBar it should be called so:
invalidateOptionsMenu();
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;