Reputation: 4644
In my android app I need to check whether a particular view is focussed. Now I found the getCurrentFocus() function in Activity class but this returns a View Object.
How can I compare and check whether this returned View is same as the one in question. I mean there is no getName() function here. So after getting the View object, how can I compare to check which View class is this ?
Upvotes: 1
Views: 6892
Reputation: 631
The View.isFocused()
method tells whether the view in question is focused or not.
if (myView.isFocused()) {
// your code
}
If you still want to use the getCurrentFocus() method, you can simply check:
View focusView = getCurrentFocus();
if (myView == focusView) {
// your code
}
Or else, you can compare your views by id.
View focusView = getCurrentFocus();
if (focusView != null && myView.getId() == focusView.getId()) {
// your code
}
Upvotes: 7
Reputation: 5867
No need for getName(). Just use the == operator. See:
View myView = findViewById(...);
if (activitygetCurrentFocus() == myView)
// ha focus
}
Another option, one I usually prefer, is to set a focus listener for the views you are interested in monitoring:
myView.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// got focus logic
}
}
});
Upvotes: 0
Reputation: 3120
getCurrentFocus() on the parent view
http://developer.android.com/reference/android/app/Activity.html#getCurrentFocus%28%29
Upvotes: 0
Reputation: 1419
You could use the getId() method I guess?
e.g.
getCurrentFocus() == R.id.myView;
Upvotes: 0