Pradeep Deshmukh
Pradeep Deshmukh

Reputation: 764

How to remove focus from SearchView?

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" />

Upvotes: 44

Views: 34235

Answers (11)

siddhartha shankar
siddhartha shankar

Reputation: 1460

This will work 100% to remove focus from SearchView.

override fun onResume() {
    if (searchView != null) {
        searchView.clearFocus();
    }
    super.onResume()
}

Upvotes: -1

Raghav Sharma
Raghav Sharma

Reputation: 800

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.

Upvotes: 2

Cedriga
Cedriga

Reputation: 4166

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" />

Upvotes: 12

Pointyhat
Pointyhat

Reputation: 507

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

Upvotes: 3

Rafael Ruiz Mu&#241;oz
Rafael Ruiz Mu&#241;oz

Reputation: 5472

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()

Upvotes: 0

earthw0rmjim
earthw0rmjim

Reputation: 19427

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();
}

Upvotes: 66

taranjeetsapra
taranjeetsapra

Reputation: 572

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);
         }

Upvotes: 0

mohd khalid Siddiqui
mohd khalid Siddiqui

Reputation: 178

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"

Upvotes: 22

Megha Sharma
Megha Sharma

Reputation: 81

Write this code in onResume()

  searchView.setText("");    
  this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);     

Upvotes: 3

Zeeshan Yousaf
Zeeshan Yousaf

Reputation: 1

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.

Upvotes: 0

Masum
Masum

Reputation: 4969

Use this line of code in your onResume method

if (searchView != null) {
    searchView.setQuery("", false);
    searchView.clearFocus();
    searchView.onActionViewCollapsed();
}

Upvotes: 13

Related Questions