16

My app uses keyboard control and when moving around I've noticed that occasionally you get what I'll refer to as a "Mystery View" - I say that because nothing visible seems to be selected, yet nothing visible is focussed either

Is there any way to find out what I'm focussed on at any given time? eg. a listener that fires on a view selection, etc.

Thanks

** edit - some code for ntc **

LinearLayout col1 = (LinearLayout)findViewById(R.id.col1);
Button btn = new Button(this);
btn.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        LinearLayout col1 = (LinearLayout)findViewById(R.id.col1);
        if(col1.getFocusedChild() != null) {
            Log.d(TAG, col1.getFocusedChild().toString());
        }
    }
});
col1.addChild(btn);
Jacksonkr
  • 31,583
  • 39
  • 180
  • 284

2 Answers2

28
getWindow().getCurrentFocus();
Hafthor
  • 16,358
  • 9
  • 56
  • 65
1

You can probably use this method for every view. Maybe this may gets bigger and hectic but is sure to work:

if(yourChildView.isFocused()) {
       yourChildView.setFocusable(false);
}
gvlasov
  • 18,638
  • 21
  • 74
  • 110
ngesh
  • 13,398
  • 4
  • 44
  • 60
  • I'll give this a try first thing. There's only like 5 parents to choose from so I'll try them all! Thanks. – Jacksonkr Aug 04 '11 at 05:03
  • Right now my container is a LinearLayout and the "getFocusedChild" comes back as null, even if I add the Log to an onclick listener. I've posted some sample code in an edit to my original. – Jacksonkr Aug 04 '11 at 05:25
  • well currently your button is focussed.. so Your linear layout will not be focussd.. thus it returns null.... tell me your actual purpose so that i can help.. – ngesh Aug 04 '11 at 05:49
  • If I navigate to the end of a LinearLayout, I can iterate fine through the contained items until the end. it seems as though there are random Views that get selected, but they no selection is visible to the display so I have no idea what is being selected. I'm just trying to find out what they are so I can disable them.. – Jacksonkr Aug 04 '11 at 05:58