2

I have Toolbar and ScrollView in my layout. I wanna Hide/Show Toolbar when user scroll ScrollView. There was the same questions before here in stackoverflow but I didnt find right answer. Here below code which I used. Unfortunatly it works only with ActionBar. Is it any was to use this with Toolbar cause now I have errors in isShowing, hide, show methods?

Thanks for any help!

public class MainActivity extends ActionBarActivity implements ViewTreeObserver.OnScrollChangedListener{

    private float mToolbarBarHeight;
    Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mToolbar = (Toolbar) findViewById(R.id.application_toolbar);
        setSupportActionBar(mToolbar);

        //hiding the toolbar
        final TypedArray mTypedArray = getTheme().obtainStyledAttributes(new int[] { android.R.attr.actionBarSize });
        mToolbarBarHeight = mTypedArray.getDimension(0, 0);
        mTypedArray.recycle();
        ((ScrollView)findViewById(R.id.mScrollView)).getViewTreeObserver().addOnScrollChangedListener(this);
    }

    ****

    @Override
    public void onScrollChanged() {
        float y = ((ScrollView)findViewById(R.id.mScrollView)).getScrollY();
        if (y >= mToolbarBarHeight && mToolbar.isShowing()) {
            mToolbar.hide();
        } else if ( y==0 && !mToolbar.isShowing()) {
            mToolbar.show();
        }
    }
}
Nurzhan Nogerbek
  • 4,806
  • 16
  • 87
  • 193

0 Answers0