options menu - removing focus from item
Asked Answered
R

1

8

how do I remove focus from options menu item? I.e. when I open the menu for the first time, none of the items has focus. however, if I focus on one of them using track ball, and then close and re-open the menu the focus is still there. How do I get rid of it?

I am clearing and recreating the menu in onPrepareOptionsMenu (as I have to adjust it to the current activity state).

EDIT:

public boolean onPrepareOptionsMenu(Menu menu){
    menu.clear();

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_menu, menu);

    return true;
}
Robynroc answered 17/10, 2010 at 10:55 Comment(2)
were you able to find the solution to this?Datha
Can you post code to reproduce the problem? I'll be happy to look into it for you. It's an interesting problem.Appointee
M
0

Once you use the trackball (or indeed press any other key on the keyboard), you exit touch mode. From that point on, there will be some view that has focus (you can see this by calling Activity.getCurrentFocus()). This mode persists until you touch the screen again, at which point you will re-enter touch mode and when you open the menu on subsequent occasions there will not be a focused item. This happens in every single Android application I have seen with a menu.

This situation exists not only for option menu items, but also for views in your layout. Once you leave touch mode, I don't think there is a way to enter it again (and hence remove focus from all views) unless you touch the screen. A solution I have seen cited for removing the visible effects of focus (when not in touch mode) is to simply give it to an element which is not visible. Could you create an invisible menu option and give it focus when you want to clear it?

Unless you find a way to programmatically, reliably enter touch mode or to remove focus from all views (calling View.clearFocus() just passes it to another view!), this may be your best bet.

Murry answered 7/8, 2011 at 14:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.