Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Hide Sherlock Action Bar

I have added Sherlock action bar to an activity by using the following line in manifest:

android:theme="@style/Theme.Sherlock.Light"

I want to save on screen space and hide the action bar by default and show it only when user taps on the screen. I have seen it in Aldiko app but don't know how to implement it.

Any help is welcomed.

like image 995
Piyush-Ask Any Difference Avatar asked Jun 13 '13 06:06

Piyush-Ask Any Difference


2 Answers

First give the id to your wrapping node of activity layout (main_layout.xml):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/containerMain"

Second implement this in your code for activity:

boolean isActionBarShow=true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);

    a=getSupportActionBar();
    RelativeLayout rl= (RelativeLayout)findViewById(R.id.containerMain);
    rl.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction()==MotionEvent.ACTION_DOWN)
                if (isActionBarShow)
                {
                    a.hide();
                    isActionBarShow=false;                      
                }
                else
                {
                    a.show();
                    isActionBarShow=true;

                }
            return false;
        }
    });
like image 33
Tomislav Avatar answered Nov 13 '22 01:11

Tomislav


To hide use below code on your activity onCreate() method,

getSupportActionBar().hide();

To show use below code on tap event,

getSupportActionBar().show();

Follow ActionBarSherlock Usage for more details.

like image 112
Bishan Avatar answered Nov 13 '22 01:11

Bishan