How to detect a click anywhere on a SearchView - android

How to detect a click on searchView?
I have tried using setOnClickListener, setOnSearchClickListener, setOnFocusChangedListener, but to no avail.
The problem seems to be only click on textbox. If I click anywhere outside of textbox onClickListener triggers, but not if click inside the textbox.
The problem that I am trying to solve is that I need to close a if user clicks anywhere. But I dont know how to handle the SearchView.

I have managed to find a way to solve my problem.
It's a bit hackish, but it works. You have to get the editText from searchView and set OnClickListener to it
int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
EditText editText = (EditText) searchView.findViewById(id);
editText.setOnClickListener(listener);

Try this:
searchView = (SearchView)findViewById(R.id.searchView);
searchView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
searchView.setIconified(false);
}
});

Try adding app:iconifiedByDefault="false" to the SearchView tag in your layout XML.

An answer from a similar question: How to detect if SearchView is expanded?
searchView.setOnQueryTextFocusChangeListener { _ , hasFocus ->
if (hasFocus) {
// searchView expanded
} else {
// searchView not expanded
}
}

Related

Android searchView clear text button listener

how I can set a listener into 'clear text' button in searchView (not close). And no, onQueryTextChange set to empty does not solve my problem.
Greetings
Try to get clearButton from SearchView and set OnClickListener like below:
ImageView clearButton = searchView.findViewById(androidx.appcompat.R.id.search_close_btn);
clearButton.setOnClickListener(v -> {
if(searchView.getQuery().length() == 0) {
searchView.setIconified(true);
} else {
// Do your task here
searchView.setQuery("", false);
}
});
if you got a is a TextInputLayout with id 'searchInput', then simply:
searchInput.setEndIconOnClickListener {
// Do something
}
(If you can't find it, you might have to update your libs)
Doc: https://developer.android.com/reference/com/google/android/material/textfield/TextInputLayout#setEndIconOnClickListener(android.view.View.OnClickListener)

searchview loases focus on orientation change

I am using searchview in my app with action bar and fragments. searchview is opened in the portrate mode and when we change the orientation it closes. If there is any text present in searchview it will not close on orientation chnage. I want to retain the state of searchview on orientation change even if the searchview is empty.How can i do this?i tried
setRetainInstance(true);
But it is not working. Please help me.
I implemented it myself since Android does not seem to save it for me.
final String s = savedSearchString;
if (s != null) {
searchViewMenuItem.expandActionView(); // opens the action view
}
searchView.post(new Runnable() {
#Override
public void run() {
searchView.setQuery(s, false); // sets the last search string on the view
}
});
'savedSearchString' is coming from the savedInstanceState. Make sure you set the initial conent of the search window on "" and not on null (default), otherwise it won't re-open an empty window.
You can create a boolean.
And when you open the searchView, you put boolean to true.
When you close the searchView, yout put boolean false.
In the onCreate, if the boolean = true, open the searchView.
final SearchView searchView = (SearchView) itemSearch.getActionView();
searchView.setQueryHint(Html
.fromHtml("<font color = #ffffff>Search...</font>"));
searchView.setOnSearchClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//your code
}
});

How to disable searchview?

Can you help me on how to disable searchview when button is pressed? I'm trying this code:
searchView.setEnabled(false);
searchView.setFocusableInTouchMode(false);
searchView.clearFocus();
but it seems not working. I can still input text in searchview.
Thanks.. :))
All above questions don't work for me.
Becase SearchView is a ViewGroup, so we have to disable all its child views.
private void enableSearchView(View view, boolean enabled) {
view.setEnabled(enabled);
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
enableSearchView(child, enabled);
}
}
}
In other place, call this:
enableSearchView(searchView, true/false);
You can use:
searchView.clearFocus();
and if you want to hide it using:
searchView.setVisibility(View.GONE);
Try the following:
searchview.setInputType(InputType.TYPE_NULL);
SearchView isn't the actual EditText you type into, but rather is a LinearLayout which holds a bunch of views, including the EditText.
To get the view you actually want for disabling it:
EditText searchViewEditText = (EditText) searchView.findViewById(R.id.search_src_text);
Note, this only works if you're using support v7 SearchView, since the particular resource id is internal if you use the framework SearchView.
searchView.findViewById(R.id.search_button).setEnabled(false);
But you must check it for null and hide searchView:
ImageView searchBtn = (ImageView) searchView.findViewById(R.id.search_button);
if (searchBtn != null) searchBtn.setEnabled(false);
searchView.setVisibility(View.GONE);
I think this will work
Did you try
searchView.setVisibility(View.GONE); ?
This worked for me
ImageView searchIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
searchIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Don't perform any action.
}
});
searchView.setOnQueryTextFocusChangeListener( new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus && !v.isEnabled()) v.clearFocus();
}
}
Try below code:
searchView.setIconified(true);
val searchEditText: EditText =
searchView.findViewById(androidx.appcompat.R.id.search_src_text)
searchEditText.isEnabled=false
This will work
If you want to clear it (SearchView), do:
searchView.clearFocus();
and if you want to hide it temporarily, do:
searchView.setVisibility(View.GONE);

SearchView setOnClickListener NOT WORkING

Can anyone see why this is not working..
My SearchView is in the ActionBar and is always shown. I want to know when a user PRESSES the searchview... not when it expands or gains focus.
This code sits within onCreateOptionsMenu
SearchView = _searchView;
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
_searchView = (SearchView) menu.findItem(R.id.menu_finder_text_search).getActionView();
_searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
_searchView.setIconifiedByDefault(false); // Do not iconify the widget, we want to keep it open!
_searchView.setFocusable(false);
_searchView.setClickable(true);
_searchView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//DO SOMETHING!
}
});
Anyone?
SearchView is inherited from LinearLayout, so we can setOnClickListener for each child, like this:
public static void setSearchViewOnClickListener(View v, OnClickListener listener) {
if (v instanceof ViewGroup) {
ViewGroup group = (ViewGroup)v;
int count = group.getChildCount();
for (int i = 0; i < count; i++) {
View child = group.getChildAt(i);
if (child instanceof LinearLayout || child instanceof RelativeLayout) {
setSearchViewOnClickListener(child, listener);
}
if (child instanceof TextView) {
TextView text = (TextView)child;
text.setFocusable(false);
}
child.setOnClickListener(listener);
}
}
}
from: http://www.trinea.cn/android/searchview-setonclicklistener-not-working/
Ok, it does not answer the problem it only avoids it.
I have used this link to create a listener for when the keyboard is shown. This gives me an event at the right time for me.
https://stackoverflow.com/a/7423586/1312937
Try this:
1) Bind the view
#BindView(R.id.search) SearchView search;
2) In your onCreate(), write the following code.
search.setIconifiedByDefault(false);
search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
search.setIconified(false);
}
});
3) And your SearchView should have this following attributes.
<SearchView
android:id="#+id/search"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:background="#drawable/rounded_corners_box"
android:drawableLeft="#android:drawable/ic_menu_search"
android:imeOptions="actionSearch"
android:inputType="text"
android:maxLines="1"
android:queryHint="Search your item.."
android:textColor="#android:color/black"
android:textColorHint="#color/colorPrimary"
app:defaultQueryHint="Select locality"/>
NOTE:
android:background="#drawable/rounded_corners_box" -- your custom border xml file.
android:drawableLeft="#android:drawable/ic_menu_search" -- search icon from drawable file.
Bind the Searchviews button to a custom ImageView and add the onClickListener there
ImageView searchButton = this.searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
searchButton.setOnClickListener(v -> {
// Your code here
//This is needed since you are overwriting the default click behaviour
searchView.setIconified(false);
});
Recently stuck with this problem and found a simple solution.
searchView.setOnQueryTextFocusChangeListener(object : View.OnFocusChangeListener{
override fun onFocusChange(p0: View?, p1: Boolean) {
// Enter your code here
}
})
This method will be called when you will tap on search field and soft keyboard will appear.
Use the interface OnTouchListener: http://developer.android.com/reference/android/view/View.OnTouchListener.html
This requires a tiny bit more implementation code, but gives superior control over the UI. This solution assumes the user will be using a touch screen to interact with the View.
int search_button_id = context.getResources().getIdentifier("android:id/search_button", null, null);
ImageView search_button_view = (ImageView) mSearchView.findViewById(search_button_id);
search_button_view.setOnTouchListener((view, motionEvent) -> {
mSearchView.setIconified(false);
return true;
});

How do I select all text when clicking on a EditText view?

so I tried android:selectAllOnFocus and of course I'm using android:hint.
The app loads, requestFocus triggers and the full text is selected.
The problem is that when I click in the EditText the selection is lost.
I've already read:
Select all text inside EditText when it gets focus
For some reason, it worked (on Jelly Bean) only if I posted it with a Handler:
new Handler().post(new Runnable() {
#Override
public void run() {
paidView.selectAll();
}
});
Set the selection in an View.OnClickListener like so:
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setSelection(0, editText.getText().length() - 1);
}
}
Set the selection in an View.OnClickListener like so: (with the 0 and text length opposite to the previously approved response) - this will ensure that when the user starts typing the content will be overridden.
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setSelection(editText.getText().length() - 1, 0);
}
}
I realize this is an old post, but I had this same problem. For me, the reasoning was that I like to move the keyboard out of the way (dismiss it) to get my bearings (or hit buttons to add or remove rows of data) and when i touched back on the EditText I was previously editing, it was annoying to have the keyboard pop back up and the text un-select, forcing me to either work to get the cursor to where I wanted to start deleting, or touch another EditText and then touch back on the original to re-select everything. I just want to have the keyboard pop back up and have the text selected and ready to overwrite whenever I go to that EditText.
There are easy solutions to this:
1) Long tap does this for you on most occasions.
2) If you're already using setSelectAllOnFocus(true), you can just throw a simple clearFocus() and requestFocus() in your onClick listener:
etMyEditText.setSelectAllOnFocus(true);
etMyEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
view.clearFocus();
view.requestFocus();
}
});
This way the EditText has everything selected when you tap on it, regardless of soft keyboard status.
Additional bonus:
Add android:windowSoftInputMode="adjustPan" inside your <activity .../> tag in your AndroidManifest.xml file to force the selected EditText to stay in sight when the soft keyboard pops up.
try This
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.selectAll();
//or this.selectAll();
}
To select all the text.
Do the code below after you did findViewById and you are good to go:
edtProductPrice.setSelectAllOnFocus(true);
edtProductPrice.requestFocus();
edtProductPrice.selectAll();

Categories

Resources