ZOFTINO.COM

Implementation of Options Menu on App Bar in Android Apps

Menus in any type of application help users see list of actions that user can do at application level, page level and element level. Androids provides menu API for implementing menus in android apps.

There are different types of menus such as menus that appear at top on menu bar, menus that get displayed on clicking an item and menu that are shown as pop up. Android provides framework that can be used to implement options menu, contextual menu and popup menu in android apps. In this article, I’ll show how to implement options menu in Android.

Android options menu

android options menu dynamic example

Android options menu is displayed on app bar. To create options menu, first you need to define menu. Menu can be defined in xml or in code. It is a best practice to separate menu definition from code. Creating menu items in xml makes it easy to test and enhance.

Root element in menu xml is Menu. Each item in menu can be defined using MenuItem. MenuItem has several attribute such as title, icon, showAsAction. Attribute showAsAction is used to control place of menu item on app bar. For more information about app bar, read app bar article.

After menu is defined, you need to create menu object in your activity class. Activity’s onCreateOptionsMenu call back method is used to inflate menu. This call back method gets called once when menu is displayed first time. If you need to change menu items dynamically, you can do that in onPrepareOptionsMenu call back method. To make the system call onPrepareOptionsMenu call back method, you need to call invalidateOptionsMenu method.

Menu item click events can be handled in onOptionsItemSelected method which gets called every time a menu item is clicked. This method is passed menu item object using which you can find out which menu item is clicked and provide appropriate behavior.

Below example shows how to add options menu and handle menu click events. It also shows how to dynamically change options menu, depending on the menu item clicked. You can update menu in onPrepareOptionsMenu call back method. In this example, additional menu items are added to menu when user clicks a particular menu item.

Activity

 package com.zoftino.menus;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class OptionsMenuActivity extends AppCompatActivity {
    private String subMenuType = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_options_menu);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.stores:
                showStore();
                return true;
            case R.id.coupons:
                showCoupons();
                return true;
            case R.id.cashback:
                return true;
            case R.id.deals:
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    @Override
    public boolean onPrepareOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        if("coupons".equals(subMenuType)){
            inflater.inflate(R.menu.coupons_sub_menu, menu);
        }
        return super.onPrepareOptionsMenu(menu);
    }
    public void invalidateOptionsMenu(String type){
        subMenuType = type;
        invalidateOptionsMenu();
    }
    public void showStore(){
        Intent i = new Intent();
        i.setClass(this, StoresActivity.class);
        startActivity(i);
    }
    public void showCoupons(){
        //call invalidate to update menu
        invalidateOptionsMenu("coupons");

        TextView tv = (TextView)this.findViewById(R.id.textView);
        tv.setText("");

        ListView lv =(ListView)this.findViewById(R.id.lstview);
        lv.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, StoresData.arrayOfCoupons));
    }
}
 

Activity layout xml

 


    

    

 

Menu xml

 

    
    
    
    

 

Dynamic menu xml