I need to find out if any view is focused inside an Activity and what view it is. How to do this?
Asked
Active
Viewed 9.3k times
6 Answers
134
Call getCurrentFocus() on the Activity.
-
1For some reason it returns null after cycling all child views with "next" action. – WindRider Dec 13 '14 at 16:44
-
10BTW, getCurrentFocus() is a method of activity, not of view. – ToolmakerSteve Nov 13 '15 at 03:30
-
3... so in fragments we can use getActivity().getCurrentFocus().clearFocus() e.g... – Martin Pfeffer Jun 10 '16 at 12:50
-
1It is possible to [get hosting Activity from a View](http://stackoverflow.com/questions/8276634/android-get-hosting-activity-from-a-view) and call `getCurrentFocus()`, but not that reliable. – Eido95 Dec 29 '16 at 12:19
-
1kotlin: in fragment - `activity?.currentFocus` – Mohammad Reza Khahani Nov 16 '19 at 11:12
-
From view: `(v.context as? Activity)?.currentFocus`. – CoolMind Feb 20 '23 at 10:47
13
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;
}
Tobrun
- 18,291
- 10
- 66
- 81
13
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.
Haroun Hajem
- 5,223
- 3
- 26
- 39
-
You could extend this code and add a highlight so you can see which item is selected. It should work as long the item is inside the screen. But using the `logcat` is much more secure. – Haroun Hajem Nov 16 '20 at 12:24
6
for some reason getCurrentFocus() method isn't available anymore; probably it's deprecated already, here the working alternative:
View focusedView = (View) yourParentView.getFocusedChild();
John F
- 685
- 1
- 8
- 18
-
1It is two different methods. getCurrentFocus() is an Activity class method and getFocusedChild() belongs to View class – BoredT Jan 09 '15 at 11:59
-
2
1
ViewGroup has quite convenient method for retrieving focused child:
ViewGroup.getFocusedChild()
Ramps
- 5,190
- 1
- 45
- 47