Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clear focus on content when navigation drawer is showed

What would be the best way to clear the focus in an android activity having a navigation drawer.

The problem it's that when the user has a textbox selected, if you open the navigation drawer the back event it's not consumed by the drawer, and the navigation goes back one step leaving the drawer opened.

Right now I solved using this on the activity, but i would like to know if someone use another solution where i don't need to use the ActionBarDrawerToggle events.

/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
    View window = findViewById(R.id.content_frame);
    window.clearFocus();
}
like image 505
Jokin Avatar asked Oct 24 '13 16:10

Jokin


1 Answers

Indeed it seems the drawer doesn't have focus when it's opened so it's onKeyUp won't be called. I checked this in the host activity and in onBackPressed DrawerLayout#hasFocus() returns false if the activity has a view that gained focus - an EditText or something else.

I even tried to mess up with its descendantFocusability property and that didn't work. I managed to fix this with a similar approach as you did: the drawer layout is requesting the focus:

mDrawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
    @Override
    public void onDrawerOpened(View drawerView) {
        mDrawerLayout.requestFocus();
    }
});

You are clearing the focus from content ViewGroup, but I am not sure who gets the focus then, most probably no view.
Honestly speaking, this seems to me a bug in DrawerLayout as if it's opened, it should have the focus gained.

like image 129
gunar Avatar answered Sep 22 '22 20:09

gunar