Question

Hi in my android app I would like to hide the action bar on user interaction and show it again when the user has stopped interacting for some time. Now I already have the code for hiding the action bar:

    mViewPager.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            getActionBar().hide();
            return false;
        }
    });

I simply added an onTouchListener to my main view

But I do not know how to implement the getActionBar.show(); method. How do i find out whether the user has been not interacting for, let's say, 2 seconds and thus implement getActionBar().show();?

Thanks in advance...

EDIT:

    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        SystemClock.sleep(3000);

        if (isCancelled()) {
            break;
        }
        return null;
    }

This code is giving an error. break can only be used inside loop or switch. How to implement onCancelled()

Was it helpful?

Solution

ultimately you have to track the user interactions. have a look at this post : tracking-user-idle-time-within-the-app-in-android

By using this concept you can track the time and save in preference.

Now remaining logic is only for showing the actionbar if user is not active for some time interval (suppose 2seconds).

For that, You can create a Thread inside your Activity which periodically (in your case,at every 2seconds) checks last user-interaction time by calling getElapsed() method.if getElapsed() is greater than your desired time(2seconds).then show actionbar using getActionBar().show() method.

OTHER TIPS

You're probably looking for a FullScreenActivity behaviour, like the one provided on the FullScreenActivity template. Read more about it: http://developer.android.com/tools/projects/templates.html#full-screen-activity

With this, you will have for free, a well tested code, with the behaviour: auto hiding the actionbar and showing it back when there's interaction.

You could set up an AsyncTask, put a sleep in there before showing the ActionBar, and if the user touches anything else cancel the task. See here for canceling task. I'd put the isCancelled() check after the sleep and before showing the ActionBar

EDIT: What I'd do for your problem is

protected String doInBackground(String... params) {
    // TODO Auto-generated method stub
    SystemClock.sleep(3000);

    if (!isCancelled()) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //Show the actionbar code here
            }
        });
    }
    return null;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top