android - has - How to find out which view is focused?




how to set focus on edittext in android xml (4)

I need to find out if any view is focused inside an Activity and what view it is. How to do this?

https://code.i-harness.com



From the source of Activity:

   /**
     * Calls {@link android.view.Window#getCurrentFocus} on the
     * Window of this Activity to return the currently focused view.
     * 
     * @return View The current View with focus or null.
     * 
     * @see #getWindow
     * @see android.view.Window#getCurrentFocus
     */
    public View getCurrentFocus() {
        return mWindow != null ? mWindow.getCurrentFocus() : null;
    }

ViewGroup has quite convenient method for retrieving focused child:

ViewGroup.getFocusedChild()

for some reason getCurrentFocus() method isn't available anymore; probably it's deprecated already, here the working alternative:

View focusedView = (View) yourParentView.getFocusedChild();




android