Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeatedly dim status bar (SYSTEM_UI_FLAG_LOW_PROFILE) on tablets in full screen mode?

I know how to dim status bar on Android tablets. I do this with that code:

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);

It works fine but only once. When I touch status bar, it activates and when after that I'm back in my application's activity, status bar is still activated (with icons instead of dots). I tried to log onResume calls, but it's not called, so I googled again and found another solution - using handler for changing visibility of status bar:

getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
    @Override
    public void onSystemUiVisibilityChange(int visibility) {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
    }
});

It sometimes works but after several tries it breaks down.

I need this for game - theoretically I can try to call it after every touch or every time in main loop but this seems to be bad idea (and additionally it must be called from specific thread - Only the original thread that created a view hierarchy can touch its views.).

My question is: what is the best way to implement auto dim of status bar? Or, in my situation, any way.

like image 305
Tomaszek Avatar asked Oct 20 '12 18:10

Tomaszek


1 Answers

A solution to this is having a View.OnSystemUiVisibilityChangeListener on you root view.

When an event fires you have to wait some time to dim them again. A Timer would be suitable.

private class MySystemUiVisibilityChangeListener implements View.OnSystemUiVisibilityChangeListener {

    @Override
    public void onSystemUiVisibilityChange(int visibility) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                MyActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mRootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);                           
                    }
                });
            }
        }, 1000);
    }

}
like image 152
Karanlos Avatar answered Nov 01 '22 04:11

Karanlos