hellogoodbyeokgo
hellogoodbyeokgo

Reputation: 41

Android - MenuItem onOptionsItemSelected method not working but returning no errors

I am relatively new to android development and have been trying to learn about MenuItem's but haven't had much success. I am having trouble calling a method when a menuitem is clicked on. Here is my code:

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    String selectedMenuIdString = (String) item.getTitleCondensed();

    if (selectedMenuIdString.equals("about")) {
        doThis(item);
        return true;
    }
    return true;
}

public void doThis(MenuItem item) {
    Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
}

It worked once a few days back but it wont now and I have no idea why. Could anyone help?

<item
    android:id="@+id/search"
    android:orderInCategory="100"
    android:title="@string/search"
    android:titleCondensed="search"
    />
 <item
    android:id="@+id/products"
    android:orderInCategory="100"
    android:title="@string/products"/>
<item
    android:id="@+id/contact_us"
    android:orderInCategory="100"
    android:title="@string/contactus"/>
<item
    android:id="@+id/about"
    android:orderInCategory="100"
    android:title="@string/about"/>

And my strings:

<string name="search">Search</string>
<string name="products">Products</string>
<string name="contactus">Contact Us</string>
<string name="about">About</string>

Upvotes: 1

Views: 2257

Answers (3)

Mohammed Aouf Zouag
Mohammed Aouf Zouag

Reputation: 17132

Do it this way, it's much easier:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getId(); // Retrieve the id of the selected menu item

    switch(id) {
        case R.id.about: // "@+id/about" is the id of your menu item (menu layout)
            doThis(item);
            return true;
            break;
        case ...
    }
    return super.onOptionsItemSelected(item);
}

No listeners, no extra complexity.

Upvotes: 4

Kostas Drak
Kostas Drak

Reputation: 3260

You say

String selectedMenuIdString = (String) item.getTitleCondensed();

    if (selectedMenuIdString.equals("about")) {

and the about menu item as it seems here does not have a condensedTitle

<item
    android:id="@+id/about"
    android:orderInCategory="100"
    android:title="@string/about"/>

So modify your code like:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch(item.getItemId()) {
        case R.id.about:{
            //do something here
            return true;
            break;
        }
        case R.id.search:{
            //do something here
            return true;
            break;
        }
        case R.id.contact_us:{
            //do something here
            return true;
            break;
        }
        case R.id.products:{
            //do something here
            return true;
            break;
        }
    }
    return super.onOptionsItemSelected(item);
}

Upvotes: 1

Falco Winkler
Falco Winkler

Reputation: 1190

Always compare the items id not the title or something else, like in the other answer.

An other nice way to do it: Try handling the click events through an OnMenuItemClickListener, in the onPrepareOptionsMenu function. Sample code below:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

       MenuItem prefs = menu.findItem(R.id.prefs); // your item identifier
       prefs.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                doThis();
                return false;
            }
        });
}

Upvotes: 1

Related Questions