Dark Leonhart Dark Leonhart -4 years ago 145
Android Question

Hide Action Bar on View scrolled

I've search anywhere but I haven't found it. Anything I've found this far is always hiding the Action Bar when a ListView, GridView or RecyclerView is scrolled. What I need is how to hide the Action Bar when a ViewGroup (using ScrollView for example) is scrolled? Because I can't really convert my layout to any of those three, and there is no onScrollListener for ScrollView.

Thank you.

Answer Source

1.implement ViewTreeObserver.OnScrollChangedListener into your class, 2.Write this line into your onCreate "getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);" (without quotes) 3.Now get ActionBar Height for Hide animation as follows -

    final TypedArray mstyled = getTheme().obtainStyledAttributes(new int[]{android.R.attr.actionBarSize });
mActionBarHeight = styledAttributes.getDimension(0, 0);

4.Get Action Bar.

mActionBar = getActionBar();

5.Intilize scrollview.


6.override onScrollChange and mode your method like this.

public void onScrollChanged() {
    float mfloat = ((ScrollView)findViewById(R.id.parent)).getScrollY();
    if (mfloat >= mActionBarHeight && mActionBar.isShowing()) {
    } else if ( mfloat==0 && !mActionBar.isShowing()) {

Hope it Helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download