Saturday, November 02, 2013

Setting up the menu on an Android App.



As I am developing the Android App, I need to add a simple menu for popping up a dialog box to show the version of the application. As far as I know, the only menu that can be popped up is the Menu button. On a phone, this soft button is usually located on the bottom left hand corner.

I wanted to have a single menu item in the list. It is simple "About"

Firstly, I create a menu.xml and placed it in under /res/menu.

xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
   
    <item android:id="@+id/menu_about"
          android:title="About" />

</menu>

Secondly, I added the code that adds this menu. This code resides in onCreateOptionMenu() which is in MainActivity.java.


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_actions, menu);
        getMenuInflater().inflate(R.menu.menu, menu);
        super.onCreateOptionsMenu(menu);
        return true;
    }

In the snippet of code, I have also shown the code for the action bar. However, the line of the that creates the option menu the call to getMenuInflater(). Be sure that the ID of the xml file is referenced correctly.

When the menu button is clicked, a menu will pop up with only the "About" option displaying.

Thirdly, I created code to handle the event when the "About" option is selected. This is handled in the onOptionsItemSelected() method. So I have the following implementation.

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.action_search:
        Toast.makeText(getApplication(), "search is selected",
Toast.LENGTH_SHORT).show();
        
            openSearch();
                return true;
            case R.id.action_settings:
            
        Toast.makeText(getApplication(), "settings is selected",
Toast.LENGTH_SHORT).show();
        
                openSettings();
                return true;
                
            case R.id.menu_about:
                // Single menu item is selected do something
                // Ex: launching new activity/screen or show alert message
                //Toast.makeText(AndroidMenusActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
                
            String message = getString(R.string.app_name) + "\n" + getString(R.string.app_version);
            
    // pop up a very simple About dialog box.
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder
    .setMessage(message)
    .setPositiveButton("OK", null)
        .show();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

This implementation also includes actions for when items from the action bar is also selected.

So when the "About" is selected, a simple dialog box will pop up.

This code also abstract the strings to strings.xml so that it be changed easily. A snippet is shown below.


    <string name="app_name">BackgroundNoise</string>
    <string name="app_version">version 0.01</string>


No comments: