Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clearFocus() on empty editText is not working in Android

I tried to remove focus from empty editText but it isn't working correctly. I called clearFocus() on edittext ,and then I placed break point at my onFocusChanged() function call.

Here is what happened: onFocusChanged() called 4 times with the focused parameters values false,true,false,true.

What I thought was that onFocusChanged() must be called only once (with focused = false)

Sorry for my bad English. Any help would be appreciated. Thanks

like image 724
Vijay Avatar asked Sep 28 '16 13:09

Vijay


People also ask

How do you Unfocus textfield on Android?

setFocusable(false); editText. setFocusableInTouchMode(true); editText. setFocusable(true); EditText will lose focus, but can gain it again on a new touch event.

How do I turn off focus on Android?

On Android's Digital Wellbeing screen, tap Focus Mode. Choose the Set a Schedule option to block out the timeout you need from your phone. In the “Your distracting apps” list, select the programs you'd like to disable when Focus Mode is on.

What is Focusableintouchmode?

Focusable in touch mode is a property that you can set yourself either from code or XML.


2 Answers

This is happening because your EditText is the first focusable view.

From the docs,

Note: When a View clears focus the framework is trying to give focus to the first focusable View from the top. Hence, if this View is the first from the top that can take focus, then all callbacks related to clearing focus will be invoked after which the framework will give focus to this view.

You can try setting a dummy focusable view above the EditText to clear the focus from it.

like image 166
pratZ Avatar answered Sep 18 '22 07:09

pratZ


In xml, make parent layout

  android:focusable="true"
  android:focusableInTouchMode="true"

and then call clearFocus on edit text and then call parent request focus

 mFragmentBase.editText.clearFocus();
 mFragmentBase.parentLayout.requestFocus();
like image 22
Alok Gupta Avatar answered Sep 22 '22 07:09

Alok Gupta