How to find out which view is focused?

Android

Android Problem Overview


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

Android Solutions


Solution 1 - Android

Call getCurrentFocus() on the Activity.

Solution 2 - Android

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

Solution 3 - Android

Try this instead, put everything inside a thread and print the id and classname live to logcat. Just put this code inside your Activity, in the onCreate method then look into your logcat to see what is currently focused.

JAVA

  new Thread(() -> {
        int oldId = -1;
        while (true) {
            View newView= this.getCurrentFocus();
            if (newView != null && newView.getId() != oldId) {
                oldId = view.getId();
                String idName = "";
			    try {
				   idName = getResources().getResourceEntryName(newView.getId());
			     } catch (Resources.NotFoundException e) {
				   idName = String.valueOf(newView.getId());
			     }
                Log.i(TAG, "Focused Id: \t" + idName + "\tClass: \t" + newView.getClass());
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();

KOTLIN

      Thread(Runnable {
            var oldId = -1
            while (true) {
                val newView: View? = this.currentFocus
                if (newView != null && newView.id != oldId) {
                    oldId = newView.id
                    var idName: String = try {
                        resources.getResourceEntryName(newView.id)
                    } catch (e: Resources.NotFoundException) {
                        newView.id.toString()
                    }
                    Log.i(TAG, "Focused Id: \t" + idName + "\tClass: \t" + newView.javaClass)
                }
                try {
                    Thread.sleep(100)
                } catch (e: InterruptedException) {
                    e.printStackTrace()
                }
            }
        }).start()

Be aware this thread runs in a 100ms cycle so it doesn't overflow the CPU with unnecessary work.

Solution 4 - Android

if you are in a fragment you can use

getView().findFocus()

Solution 5 - Android

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

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

Solution 6 - Android

ViewGroup has quite convenient method for retrieving focused child:

ViewGroup.getFocusedChild()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFixpointView Question on Stackoverflow
Solution 1 - AndroidKaranView Answer on Stackoverflow
Solution 2 - AndroidTobrunView Answer on Stackoverflow
Solution 3 - AndroidHaroun HajemView Answer on Stackoverflow
Solution 4 - AndroidPablo JohnsonView Answer on Stackoverflow
Solution 5 - AndroidJohn FView Answer on Stackoverflow
Solution 6 - AndroidRampsView Answer on Stackoverflow