Dan_Dan_Man
Dan_Dan_Man

Reputation: 504

Android SubMenus not doing anything when clicked

I feel this may be a stupid question but I have no idea what to do.

I have a menu which works correctly. One of the items in the menu: "Search" brings up a submenu with different items like "Restaurant", "Cafe", etc.

Below is the XML code for creating the menu and sub menu:

<?xml version="1.0" encoding="utf-8"?>
<menu 
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_entry"
      android:title="@string/new_entry"
      android:icon="@drawable/add_new">
</item>
 <item android:id="@+id/search"
      android:title="@string/search"
      android:icon="@drawable/search">
      <menu>
        <item android:id="@+id/five"
        android:title="@string/five">
        </item>
        <item android:id="@+id/ten"
        android:title="@string/ten">
        </item>
        <item android:id="@+id/fifteen"
        android:title="@string/fifteen">
        </item>
        <item android:id="@+id/restaurant"
        android:title="@string/restaurant">
        </item>
        <item android:id="@+id/cafe"
        android:title="@string/cafe">
        </item>
        <item android:id="@+id/sandwich"
        android:title="@string/sandwich">
        </item>
      </menu>
</item>
<item android:id="@+id/info"
      android:title="@string/info_short"
      android:icon="@drawable/info">
 </item>

Then in my Activity Class I have:

// Initialise Menu.
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_services, menu);
    return true;
}

and:

// Create cases for each menu selection.
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.new_entry:
            createFood();
            return true;
        case R.id.search:
            return true;
        case R.id.cafe:
            Log.d(TAG, "In Cafe SubMenu");
            Toast.makeText(getApplicationContext(), TAG, Toast.LENGTH_SHORT);
            return true;
        case R.id.info:
            info();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

Each case in "onOptionsItemSelected" does what it is supposed to do, except "case R.id.cafe:" which is the submenu. Here it should post to the LogCat and a Toast for testing but does neither.

What am I missing?

Thanks

Upvotes: 2

Views: 237

Answers (2)

Anu
Anu

Reputation: 1914

I have tried your code and it is working perfectly. Only the Toast is not displaying when I select 'cafe'. That is because, you did not call show() in your code. Your code is

Toast.makeText(getApplicationContext(), TAG, Toast.LENGTH_SHORT);

to show the toast on run time it should call as follows

Toast.makeText(getApplicationContext(), TAG, Toast.LENGTH_SHORT).show();

:)

Upvotes: 2

joez
joez

Reputation: 1

I think you should remove following lines:

case R.id.search:
    return true;

Upvotes: 0

Related Questions