2

I'm trying to inspect a code for a very big Android (Amazon Fire TV) activity but i keep loosing the focus in the running app and i don't know what element is being focused.

I'm looking for a way (Wether it's an App, a developer setting - Show Layout Limits gets near - or something i can code inside the activity) to see what view is being focused, without having to change the layout (Selectors) of every single view.

What do you suggest?

rekire
  • 47,260
  • 30
  • 167
  • 264
Luca Vitucci
  • 3,674
  • 4
  • 36
  • 60

1 Answers1

1

Activity has a method called getCurrentFocus().

Maybe you could call hasFocus() on all the Views if the above doesn't work. I imagine the method would look something like this:

public View getFocusedView(View layout)
{
    View focusedView = null;

    // Note: I'm not sure if FOCUS_DOWN is the right one to use here
    // so you may want to see the other constants offered
    ArrayList<View> views = layout.getFocusables(View.FOCUS_DOWN)
    for(View v: views)
    {
        if(v.hasFocus())
        {
            focusedView = v;
        }
    }
    return focusedView;
}
Andrew Schuster
  • 3,229
  • 2
  • 21
  • 32
  • I was actually trying to do something like that, but have to get all view and childs and subchilds and so on. – Luca Vitucci Jun 06 '14 at 18:07
  • getCurrentFocus might do the trick but i need something to trigger it when focus change. onFocusChangeListener on the root view doesn't seems to work, so maybe the solution is to attach that listener to every view in the layout, and from there call getCurrentFocus. I'm trying... – Luca Vitucci Jun 06 '14 at 18:08
  • If this is only for debugging purposes, maybe you could have a button that you press whenever you lose where the focus is. The button could then activate `getCurrentFocus()` and make the view flash or do whatever so you can find it – Andrew Schuster Jun 06 '14 at 18:11
  • That is a good point! :D Working for so many hours on the same issue ruins my brain. I'll test this right now – Luca Vitucci Jun 06 '14 at 18:12