creating an options menu with quit application function

Posted by Domen Tomažin on Stack Overflow See other posts from Stack Overflow or by Domen Tomažin
Published on 2010-12-27T14:51:17Z Indexed on 2010/12/27 14:53 UTC
Read the original article Hit count: 244

Filed under:

Hello, I'm writing an application for android and I would like to insert an options menu, that would have only one function and that is to quit an application.

My options_menu.xml looks like this (I created a new folder under res called menu - just like the instructions said):


Than I added the following code to my java class file:

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.quit:
        finish();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }

I also defined the @string/quit in values - strings.xml file, but it still says that there is an error: C:\Users\Domen\workspace\Lovne Dobe1.1\res\menu\options_menu.xml:3: error: Error: No resource found that matches the given name (at 'title' with value '@string/quit')

So if anybody can help I would be very grateful.

© Stack Overflow or respective owner

Related posts about android