package com.elevenzon.collapsingtoolbar ; import android.os.Bundle ; import android.support.design.widget.AppBarLayout ; import android.support.v7.app.AppCompatActivity ; import android.support.v7.widget.Toolbar ; import android.view.Menu ; import android.view.MenuItem ; import android.view.View ; public class MainActivity extends AppCompatActivity { Toolbar toolbar ; Menu menu ; AppBarLayout appBarLayout ; @Override protected void onCreate (Bundle savedInstanceState) { super .onCreate(savedInstanceState) ; setContentView(R.layout.activity_main) ; appBarLayout = (AppBarLayout) findViewById(R.id.app_bar) ; toolbar = (Toolbar) findViewById(R.id.toolbar) ; setSupportActionBar(toolbar) ; getSupportActionBar().setDisplayShowTitleEnabled(false) ; toolbar.setNavigationOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { finish() ; } }) ; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. this .menu=menu ; getMenuInflater().inflate(R.menu.main_menu, menu) ; hide_show_edit() ; return true ; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId() ; if (id == R.id.edit) { return true ; } return super .onOptionsItemSelected(item) ; } public void hide_show_edit() { appBarLayout.addOnOffsetChangedListener( new AppBarLayout.OnOffsetChangedListener() { @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { MenuItem menuItem = menu.findItem(R.id.edit) ; if (Math.abs(verticalOffset) == appBarLayout.getTotalScrollRange()) { menuItem.setVisible(true) ; } else if (verticalOffset == 0) { menuItem.setVisible(false) ; } } }) ; } }