Omar Abdelhafiz Omar Abdelhafiz - 1 year ago 77
Android Question

I want to remove the optionMenu when there are no items in ListView (emptyView is shown)?

I have an app that adds items to an sqlite database and returns a cursor, this cursor is then used with a custom CursorAdapter called StoreCursorAdapter to show the items in a ListView.

There is a (delete all) button as an optionsMenuItem.

I want to hide this optionsMenuItem when no items are avaliable in the ListView.



Sorry for the links I am a new user so they don't allow me to embed images yet. :-(

protected void onCreate(Bundle savedInstanceState) {

//Declare the views
FloatingActionButton fab = (FloatingActionButton) findViewById(;
ListView list = (ListView) findViewById(;
emptyView = findViewById(;

//Set the screen to be shown when there are no list items

//StoreCursorAdapter is a custom CursorAdapter
mAdapter = new StoreCursorAdapter(this, null);

protected void onStart() {

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu options from the res/menu/menu_inventory.xml file.
// This adds menu items to the app bar.
getMenuInflater().inflate(, menu);
return true;

public boolean onOptionsItemSelected(MenuItem item) {
// User clicked on a menu option in the app bar overflow menu
return super.onOptionsItemSelected(item);

I tried

1 - emptyView.visibilty() == View.INVISBLE

2 - list.getAdapter == null

but they didn't work

What statement will do the job?!

public boolean onPrepareOptionsMenu(Menu menu) {
if (//what statement) {
MenuItem menuItem = menu.findItem(;
return true;


OnStart() gets called after I get back from the EditorActivity


In my app I can delete individual items from another activity so adding invalidateOptionsMenu(); in the onOptionsItemSelected won't do the job.

Answer Source

the correct condition to put inside onPrepareOptionsMenu is:


that is the same comparison the ListView uses manages the empty view (minus null check) (

but I believe there's another issue here, is that the data is changing after the activity started, during showDeleteConfirmationDialog(). That means you have to call invalidateOptionsMenu() the moment the data is changed. There're two ways of doing it. One more robust and the other is faster to code:

  1. faster to code (but not very good/clean):

add invalidateOptionsMenu() after the code that executes the DB operations.

  1. more robust/clean

you'll use start/stop callbacks to listen to changes in the data. Something like the following:

@Override protected void onStart(){

@Override protected void onStop(){

private final DataSetObserver dataObserver = new DataSetObserver(){
  @Override public void onChanged(){
  @Override public void onInvalidated(){

all code above was typed by heart, there're likely typos, but that's the idea and the typos you can fix later at your code.

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