Sorry to bother the community. But can I get a pointer here? I am new to programming and just learning.
I added SearchView to my toolbar, like so.
<item
android:id="#+id/search"
android:title="Search"
android:icon="#android:drawable/ic_menu_search"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="always" />
Then I am trying to capture the "ENTER" key from the keyboard or softkeypad for when someone is typing into the SearchView to make it "//do something"
#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 item = menu.findItem(R.id.search);
SearchView searchView = (SearchView) item.getActionView();
searchView.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_ENTER:
//do something
//do something
//do something
return true;
default:
break;
}
}
return false;
}
});
return MainActivity.super.onCreateOptionsMenu(menu);
}
But my "//do something" never triggers. Can I get some help please?
You can try below code
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
//Log.e("onQueryTextChange", "==called");
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
// Do something
return false;
}
});
Related
I have implemented SearchView in ActionBar, But the problem with it is, whenever am typing something and press submit, the SearchView doesn't collapse back into that Search icon, it stays expanded, neither when I press Back button, SearchView doesn't collapse back.
My implementation is like this:
menu_search.xml
<item
android:id="#+id/app_bar_search"
android:icon="#drawable/ic_search_white_24dp"
android:title="Search"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="always" />
MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
item = menu.findItem(R.id.app_bar_search);
searchView = (SearchView) item.getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
int searchBarId = searchView.getContext().getResources().getIdentifier("android:id/search_bar", null, null);
searchBar = searchView.findViewById(searchBarId);
searchBar.setLayoutTransition(new LayoutTransition());
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
item.collapseActionView();
searchView.setQuery("", false);
searchView.clearFocus();
searchBar.clearFocus();
.
.
.
.
.
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
Things I have tried:
#Override
public void onBackPressed() {
super.onBackPressed();
if (item.isActionViewExpanded()) {
item.collapseActionView();
} else {
super.onBackPressed();
}
}
and
#Override
public void onBackPressed() {
super.onBackPressed();
if (searchView != null) {
searchView.onActionViewCollapsed();
searchView.setQuery("", false);
searchView.clearFocus();
searchBar.clearFocus();
hideSoftKeyboard(MainActivity.this);
} else {
super.onBackPressed();
}
}
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(
activity.getCurrentFocus().getWindowToken(), 0);
}
SearchView expands from left to right to type but doesn't collapse back form right to left.
Please advice.
Try below code and let me know if it works.
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, ResultActivity.class)));
searchView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
#Override
public boolean onSuggestionSelect(int position) {
return false;
}
#Override
public boolean onSuggestionClick(int position) {
searchView.clearFocus();
item.collapseActionView();
return false;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
searchView.clearFocus();
item.collapseActionView();
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
I was going through the second page of Google search and found this.
It worked for me.
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
searchView.setIconified(true);
}
}
});
I have a SearchView. When the user clicks on the keyboard search button, I need to make a server call. What does the code for the listener look like? I am thinking I have to use OnClickListener. But the internal code for knowing it's the search button, I am not sure how to determine that.
I have done like this
the onQueryTextSubmit is the method you are looking for.
set setOnQueryTextListener on your search view.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.search_city);
searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint("Search View Hint");
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
//Log.e("onQueryTextChange", "called");
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
// Do your task here
return false;
}
});
return true;
}
hope this help
This is the kotlin version:
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextChange(s: String): Boolean {
// make a server call
return true
}
override fun onQueryTextSubmit(s: String): Boolean {
val intent = Intent(applicationContext, YourSearchableActivity::class.java)
intent.putExtra(SearchManager.QUERY, s)
intent.putExtra(CUSTOM_MESSAGE, "you can also add custom message when submitting the search action")
intent.setAction(Intent.ACTION_SEARCH)
startActivity(intent)
return true
}
})
Here you can go for the Android best answer not Kotlin
binding.edtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
// Your piece of code on keyboard search click
if (AppValidator.isValid(SearchEventsActivity.this, binding.edtSearch, "Please enter your search keyword.")) {
searchKeyword = binding.edtSearch.getText().toString().trim();
//Search Events categories
callSearchEventsApi();
}
return true;
}
return false;
}
});
Make sure you have added following line in your EditText
android:imeOptions="actionSearch"
Just like following:-
<EditText
android:id="#+id/edtSearch"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="#dimen/_15dp"
android:background="#null"
android:fontFamily="#font/roboto_regular"
android:textColorHint="#color/text_color_light"
android:layout_gravity="center"
android:textSize="#dimen/_16sp"
android:maxLines="1"
android:singleLine="true"
android:imeOptions="actionSearch"
android:hint="#string/search"/>
I hope this will help you to solve your issue.!!!!
SearchView method
setOnQueryTextLister
will do your work as below shown.
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
showSnackBar(query.toString());
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
Here searchView is object of SearchView.
I have search view in my fragment. when I click on it , keyboard is open and I can type text. I want when I click on search button in keyboard , my query send to my server and get result but I don't know how get search event. any solution?
You have to extend OnQueryTextListener, attach the listener and implement onQueryTextSubmit.
Example:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
searchView = (SearchView) menu.findItem(R.id.mActionSearch).getActionView();
searchView.setOnQueryTextListener(this);
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
//Do something here
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
Pozzo Apps Answer is right
but for api below 11 and compat library you can use something like this :
MenuItem search_menu=(MenuItem)menu.findItem(R.id.action_search);
SearchView searchView =(SearchView)MenuItemCompat.getActionView(search_menu);
You can also apply setOnKeyListener on search view like as below:
searchview.setOnKeyListener(new View.OnKeyListener(
{
Public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(event.getAction() == KeyEvent.ACTION_DOWN)
{
switch(keyCode)
{
Case KeyEvent.KECODE_ENTER:
// Apply action which you want on search key press on keypad
return true;
default:
break;
}
} return false;
}
});
You have to add new OnQueryTextListener, and implement onQueryTextSubmit. This also works in a fragment.
Example:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main_search, menu);
SearchView sv = (SearchView) menu.findItem(R.id.action_search).getActionView();
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
//Do something here
Toast.makeText(getActivity(), "Search: " + query, Toast.LENGTH_SHORT ).show();
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
super.onCreateOptionsMenu(menu,inflater);
}
I'm currently using ActionBarSherlock 4.2 and it's SearchView widget in my app.
I wanted to make it submit query even though it's empty. I tried to set imeOptions and OnKeyListener but both were ignored without a reason.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d(TAG, "onCreateOptionsMenu");
inflater.inflate(R.menu.interactive_map, menu);
mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
mSearchView.setImeOptions(EditorInfo.IME_ACTION_GO);
mSearchView.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.d(TAG, "keyCode: " + keyCode);
Log.d(TAG, "Action: " + event.getAction());
if(keyCode == EditorInfo.IME_ACTION_SEARCH){
onQueryTextSubmit(""+mSearchView.getQuery());
}
return false;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
onKey's never get triggered as both Logcat entries never appear in Logcat window. Not sure if this being inside SherlockFragment is the problem.
Nothing special's done to the menu item as well.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_search"
android:title="#string/app_name"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always"
android:actionViewClass="com.actionbarsherlock.widget.SearchView" />
</menu>
Is there a way to get around this? Or is there simpler way to enable submitting when query is empty? It doesn't seems to support any configuration in the source code though.
I switched to put SearchView via ActionMode instead. I'll accept this as an answer for now until someone has a better solution for this question.
private void showActionMode(){
MainActivity a = (MainActivity)getActivity();
a.startActionMode(new SearchActionMode());
}
private class SearchActionMode implements ActionMode.Callback{
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
Log.d(TAG, "onCreateActionMode creating inflater");
MenuInflater inflater = new MenuInflater(getActivity());
Log.d(TAG, "onCreateActionMode inflating");
inflater.inflate(R.menu.interactive_map, menu);
Log.d(TAG, "onCreateActionMode finding SearchView");
mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
Log.d(TAG, "mSearchView: " + mSearchView);
setupSearchView();
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Log.d(TAG, "onPrepareActionMode");
//TODO add searchView once working
mSearchView.requestFocus();
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Log.d(TAG, "onActionItemClicked");
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
Log.d(TAG, "onDestroyActionMode");
closeTopTray();
closeListView();
}
}
private void setupSearchView() {
mSearchView.setIconifiedByDefault(false);
mSearchView.setId(ID_SEARCH_VIEW);
mSearchView.setOnSearchClickListener(this);
mSearchView.setOnQueryTextListener(this);
mSearchView.setOnCloseListener(this);
mSearchView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
// To automatically display keyboard when display SearchView
mSearchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
Log.d(TAG, "mSearchView focus changed: " + hasFocus);
if (hasFocus) {
showInputMethod(v.findFocus());
}
}
});
mSearchView.setQueryHint("Type keyword");
mSearchView.setQuery((TextUtils.isEmpty(searchQuery)? "": searchQuery), false);
}
I had the same problem, the problem lies onSubmitQuery() in SearchView.java
private void onSubmitQuery() {
CharSequence query = mQueryTextView.getText();
if (query != null && TextUtils.getTrimmedLength(query) > 0) {
Empty query's are not supported so I had to download and use ActionBarSherlock and then modify this method.
This is how my onSubmitQuery() looks like now
private void onSubmitQuery() {
CharSequence query = mQueryTextView.getText();
if (query == null) {query = "";}
if (mOnQueryChangeListener == null
|| !mOnQueryChangeListener.onQueryTextSubmit(query.toString())) {
if (mSearchable != null) {
launchQuerySearch(KeyEvent.KEYCODE_UNKNOWN, null, query.toString());
setImeVisibility(false);
}
dismissSuggestions();
}
}
Hope this helps.
Here is my code. The onKey() method is never called when typing into the search box. Am I doing something wrong in setting up the listener? I have a breakpoint in the onKey() method, which is how I know it doesn't fire.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.locations_map_menu, menu);
ActionBar ab = getActionBar();
ab.setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar));
ab.setDisplayShowTitleEnabled(false);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
return true; // This code never fires
}
});
return true;
}
Add an OnQueryTextListener, see my answer here
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
// Do something
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
// Do something
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
if you want to get a key on serach view
you can use below code
TextView searchText = (TextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
return false;
}
});