I want to remove focus and text from SearchView
in onResume()
.
I tried searchView.clearFocus()
but it is not working.
Thi
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()
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
.
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.
Just searchView.clearFocus();
in onResume()
helped my case