1

Is there a way to get an event in my Android activity when the focus is cleared from all views in it (ie. getCurrentFocus() will return null)?

We have an Android app that combines a GLSurfaceView with EditText objects positioned on top of it (not as children, that's not possible). At some point, clearFocus() is called on the EditText, so focus is removed from it. The focus is not given to our surface view.

FrameLayout - not focusable
  - GLSurfaceView (regular rendering view) - focusable
  - RelativeLayout 
    - EditText - focusable

clearFocus() will try to set focus to the first focusable view, but it doesn't find our GLSurfaceView, I guess because its a sibling not a parent.

So we end up with nothing focussed. I would like to detect this situation, without having to add code to every possible case that causes it.

Farhad
  • 12,178
  • 5
  • 32
  • 60
O'Rooney
  • 2,878
  • 2
  • 27
  • 41
  • Not really expecting this is possible, actually, and that I will have to go and find all the cases where clearFocus is called :) – O'Rooney Sep 16 '21 at 04:22
  • 1
    See this question: https://stackoverflow.com/questions/20185136/child-view-focus-change-android – Rediska Sep 16 '21 at 05:07
  • addOnGlobalFocusChangeListener looks pretty promising, I think you could put this is an answer. Will try anyway – O'Rooney Sep 16 '21 at 05:18
  • @Rediska thank you, this works in a nice general way! I just get my rootView and observe focus changes in it's tree. If you make an answer I can accept it. – O'Rooney Sep 20 '21 at 00:00
  • Well, that wasn't my answer, but I am glad it works for you. – Rediska Sep 20 '21 at 00:54

0 Answers0