0

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()

Adifyr
  • 2,649
  • 7
  • 41
  • 62
  • By not interacted do you mean not touched the screen at all? – TMH Oct 25 '13 at 13:04
  • Yes, I mean exactly that. – Adifyr Oct 25 '13 at 13:06
  • 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 http://stackoverflow.com/a/10882600/993600 I'd put the isCancelled() check after the sleep and before showing the ActionBar – TMH Oct 25 '13 at 13:07
  • Should i use `SystemClock` or Thread.sleep (I'm guessing `SystemClock`) – Adifyr Oct 25 '13 at 13:17
  • I'm not particularly sure, I haven't had to use it before. I'd just Google it see which people say is the best to use. – TMH Oct 25 '13 at 13:19
  • @TomHart The `AsyncTask` works perfectly. I'm using `SystemClock`. Thank You. – Adifyr Oct 25 '13 at 13:28

3 Answers3

1

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.

Community
  • 1
  • 1
Mehul Joisar
  • 15,348
  • 6
  • 48
  • 57
0

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.

Alécio Carvalho
  • 13,481
  • 5
  • 68
  • 74
  • yes but for me it's the other way round. I want to hide it on user interaction and show it on idle time – Adifyr Oct 25 '13 at 13:16
0

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;
}
Community
  • 1
  • 1
TMH
  • 6,096
  • 7
  • 51
  • 88
  • I don't have any on hand, if you post your's in your original post I'll take a look see if I can spot the problem – TMH Oct 25 '13 at 13:39
  • See my post, try that. – TMH Oct 25 '13 at 13:49
  • I don't think there's any need to do that. When you do `AsyncTask.cancel(true)`, it goes to `onCancelled()` instead of `onPostExecute()`. All I needed to do was do `TimerTask.cancel()` before doing `TimerTask.execute` in my `onTouch()` method. Basically it's the same thing you posted. But Thanks, you've been a great help!! – Adifyr Oct 25 '13 at 13:56
  • Not a problem, glad to have helped :) – TMH Oct 25 '13 at 13:57
  • hey just one more question. Where do I put the `cancel(true);`? – Adifyr Oct 25 '13 at 14:16
  • I'd put it in any touch event where you want the ActionBar to stay hidden. – TMH Oct 25 '13 at 14:17