0

I want to hide the action bar when an activity is started and when the user touches the screen show it for a few seconds, then hide it again.

What I have come up with is just below, but I want to know if there is anything better I could have done (ignoring some style issues: magic numbers, duplication of logic,etc).

And I am using actionbarsherlock.

Thanks


Runnable hideActionbarRunnable = new Runnable() {
        @Override
        public void run() {
            ActionBar bar = getSupportActionBar();
            bar.hide();
        }
    };

    Runnable showActionbarRunnable = new Runnable() {
        @Override
        public void run() {
            ActionBar bar = getSupportActionBar();
            bar.show();
        }
    };

    Runnable animateActionBarHide = new Runnable() {
        @Override
        public void run() {
            handler.postDelayed(hideActionbarRunnable,3000);
        }
    };

    Runnable animateActionBarShow = new Runnable() {
        @Override
        public void run() {
            handler.post(showActionbarRunnable);
            handler.postDelayed(hideActionbarRunnable,3000);
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(MainActivity.TAG,"CameraActivity:  onResume called");

        Thread t = new Thread(animateActionBarHide);
        t.start();
    }
 @Override
    public boolean onTouchEvent(MotionEvent event) {


        ActionBar bar = getSupportActionBar();
        if(event.getAction() == MotionEvent.ACTION_DOWN)
        {
            Thread t = new Thread(animateActionBarShow);
            t.start();
        }
        else
        {
            Thread t = new Thread(animateActionBarHide);
            t.start();
        }
        return super.onTouchEvent(event);
    }

1 Answer 1

3

The Handler class has a method postDelayed(Runnable r, long delayMillis). That would make it much shorter and much more elegant.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.