Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find out which view currently has focus? [duplicate]

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);
like image 231
Jacksonkr Avatar asked Aug 04 '11 04:08

Jacksonkr


People also ask

How do you know if a view is focused?

isFocused() method tells whether the view in question is focused or not. Or else, you can compare your views by id. @CGR Well, isFocused() is the preferred way to go here. getCurrentFocus() can return a null view, so you should do a null check first if you decide to use getId().

How do you find the focus of a website?

Click in the rendered webpage to put focus on it, and then press Tab or Shift + Tab to move focus around in the rendered webpage.

How do you remove focus from active element?

Use the blur() method to remove the focus from an element, e.g. input. blur() . If you need to remove the focus from the currently active element without selecting it, call the blur method on the activeElement property - document. activeElement.


2 Answers

getWindow().getCurrentFocus();
like image 172
Hafthor Avatar answered Sep 20 '22 18:09

Hafthor


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);
}
like image 39
ngesh Avatar answered Sep 22 '22 18:09

ngesh