Java tutorial
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.ActionMode; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Switch; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private final int MENU_DOWNLOAD = 1; private final int MENU_SETTINGS = 2; private boolean showDownloadMenu = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, MENU_DOWNLOAD, 0, R.string.menu_download); menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings); return true; } @Override public boolean onPrepareOptionsMenu(Menu menu) { MenuItem menuItem = menu.findItem(MENU_DOWNLOAD); menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); menuItem.setVisible(showDownloadMenu); return true; } public void toggleMenu(View view) { showDownloadMenu = !showDownloadMenu; invalidateOptionsMenu(); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case MENU_DOWNLOAD: Toast.makeText(this, R.string.menu_download, Toast.LENGTH_LONG).show(); break; case MENU_SETTINGS: Toast.makeText(this, R.string.menu_settings, Toast.LENGTH_LONG).show(); break; default: return super.onContextItemSelected(item); } return true; } }