How to remove focus from SearchView?
Asked Answered
R

11

44

I want to remove focus and text from SearchView in onResume().

I tried searchView.clearFocus() but it is not working.

This is my xml code:

<SearchView
    android:id="@+id/searchView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/toolbar"
    android:layout_alignTop="@+id/toolbar"
    android:layout_centerVertical="true"
    android:layout_marginBottom="4dp"
    android:layout_marginTop="4dp"
    android:iconifiedByDefault="false"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" />
Reptant answered 16/7, 2016 at 12:43 Comment(3)
How is it not working? Causing an error of some sort or just nothing happens? Please be more descriptive...Falange
show your code buddy :)Richelieu
@Falange actually it is not showing any error and the cursor does not remove.Reptant
S
66

I want to remove focus and text from SearchView in onResume()

To achieve this you could set your root layout focusable with the android:focusableInTouchMode attribute and request focus on that View in onResume().

For example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:focusableInTouchMode="true">

    <SearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:iconifiedByDefault="false"/>

</LinearLayout>

And then in your Activity:

private View rootView;
private SearchView searchView;

// ...

@Override
protected void onCreate(Bundle savedInstanceState) {

    // ...

    rootView = findViewById(R.id.root_layout);
    searchView = (SearchView) findViewById(R.id.search_view);
}

@Override
protected void onResume() {
    super.onResume();

    searchView.setQuery("", false);
    rootView.requestFocus();
}
Silicium answered 16/7, 2016 at 13:25 Comment(2)
If we remove focus (or transfer) from SearchView in onResume, we will see keyboard flashed for a second in some phones. Clearing focus in onPause made it look smoother.Defend
If you have an adapter, you need to double-check twice. Otherwise, your application may crash.Hydrotropism
B
22

when in activity go to parent layout ,and in fragment too also do the same thing in framelayout. Just add this attribute:

android:focusableInTouchMode="true"
Bump answered 29/9, 2016 at 11:52 Comment(0)
H
13

Use this line of code in your onResume method

if (searchView != null) {
    searchView.setQuery("", false);
    searchView.clearFocus();
    searchView.onActionViewCollapsed();
}
Homebody answered 16/7, 2016 at 13:5 Comment(2)
it is disabling my focusListner that i don't want, I just want whenever tap on it should come back. and when fragment get load again it should remove.Reptant
Your question was not like this what you have explained now. (I want to remove focus and text from SearchView in onResume() I tried searchView.clearFocus(); but it is not working.)Homebody
K
12

In your SearchView widget, just add

  android:focusable="false"

Full widget code

    <android.support.v7.widget.SearchView
        android:id="@+id/your_search_view_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:iconifiedByDefault="false"
        app:queryHint="Search something..." 
        android:focusable="false" />
Koster answered 28/6, 2019 at 22:29 Comment(2)
hmm, this seems to work just fine. Makes me wonder why all the other answers are more complicated.Angelus
To me, this should be the accepted answer, for the focus part. Excerpt from documentation about "focusable": Controls whether a view can take focus. By default, this is "auto" which lets the framework determine whether a user can move focus to a view. By setting this attribute to true the view is allowed to take focus. By setting it to "false" the view will not take focus. This value does not impact the behavior of directly calling {@link android.view.View#requestFocus}, which will always request focus regardless of this view. It only impacts where focus navigation will try to move focus.Joeyjoffre
C
3

Write this code in onResume()

  searchView.setText("");    
  this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);     
Cellaret answered 16/7, 2016 at 13:11 Comment(0)
O
3

Just searchView.clearFocus(); in onResume()helped my case

Overlie answered 4/6, 2019 at 12:0 Comment(0)
S
2

I had just request focus to some other viewgroup and it removed focus from th searchview. Like in the parent viewgroup eg(Linearlayout, RelativeLayout) in which searchview is present, add

focusable = false; focusableInTouchMode=true

to the parent viewgroup.

Schaffhausen answered 13/11, 2017 at 6:25 Comment(0)
A
0

The simplest (and I think, the only 100% working) solution is, to create an invisible LinearLayout. This LinearLayout will grab the focus from the EditText.

Artful answered 16/7, 2016 at 13:6 Comment(0)
S
0

You can manually hide the keyboard in onResume() function as:

InputMethodManager imm = (InputMethodManager) context.getSystemService(context.INPUT_METHOD_SERVICE);
        View view = context.getCurrentFocus();

        if (view == null) {
            view = new View(context);
        }
        if (view != null) {

            IBinder iBinder = view.getWindowToken();

             imm.hideSoftInputFromWindow(iBinder, 0);
         }
Sexagenarian answered 14/6, 2018 at 7:32 Comment(0)
H
0

For those who are looking to get rid of the focus when the Activity is started AND the SearchView is in the Menu, I did these 3 things, it might work with just a few:

1.- Create a parent class of SearchView. Let's call it RafaSearchView:

class RafaSearchView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
    : SearchView(context, attrs, defStyleAttr) {

    init {
        setIconifiedByDefault(false)
    }
}

2.- Get the xml where you have your Toolbar, and set touchable="false" and focusableInTouchMode="true":

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/searchToolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/white"
        android:focusable="false"
        android:focusableInTouchMode="true"
        android:theme="@style/ToolBarStyle"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

3.- On onCreateOptionsMenu(Menu) when I'm declaring my RafaSearchView, call:

    searchView?.isIconified = false
    searchView?.clearFocus()
Hydrography answered 13/12, 2018 at 15:59 Comment(0)
Q
-1

This will work 100% to remove focus from SearchView.

override fun onResume() {
    if (searchView != null) {
        searchView.clearFocus();
    }
    super.onResume()
}
Quintal answered 9/5, 2022 at 21:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.