Saturday, January 30, 2016

Android Popup Menu Example

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    android:background="#c5cec3"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click This text to change its color."
        android:textSize="50dp"
        android:textStyle="bold"
        android:fontFamily="sans-serif-condensed"
        />
</RelativeLayout>
res/menu/textview_popup_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    <item
        android:id="@+id/red"
        android:title="Red"
        android:icon="@drawable/ic_palette_red_36dp"
        />
    <item
        android:id="@+id/green"
        android:title="Green"
        android:icon="@drawable/ic_palette_green_36dp"
        />
    <item
        android:id="@+id/blue"
        android:title="Blue"
        android:icon="@drawable/ic_palette_blue_36dp"
        />
</menu>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private RelativeLayout mRelativeLayout;
    private TextView mTextView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();

        // Get the activity
        mActivity = MainActivity.this;

        // Get the widgets reference from XML layout
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mTextView = (TextView) findViewById(R.id.tv);

        // Set a click listener for the text view
        mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
                    public PopupMenu (Context context, View anchor)
                        Constructor to create a new popup menu with an anchor view.

                    Parameters
                        context : Context the popup menu is running in, through which it can access
                                  the current theme, resources, etc.
                        anchor : Anchor view for this popup. The popup will appear below the anchor
                                 if there is room, or above it if there is not.
                */
                // Initialize a new instance of popup menu
                PopupMenu popupMenu = new PopupMenu(mContext,mTextView);

                /*
                    public MenuInflater getMenuInflater ()

                    Returns
                        a MenuInflater that can be used to inflate menu items from XML into
                        the menu returned by getMenu().
                */
                /*
                    public void inflate (int menuRes)
                        Inflate a menu resource into this PopupMenu. This is equivalent to calling
                        popupMenu.getMenuInflater().inflate(menuRes, popupMenu.getMenu()).

                    Parameters
                        menuRes : Menu resource to inflate
                */
                // Inflate the popup menu
                popupMenu.getMenuInflater().inflate(R.menu.textview_popup_menu,popupMenu.getMenu());

                /*
                    public void setOnMenuItemClickListener (PopupMenu.OnMenuItemClickListener listener)
                        Set a listener that will be notified when the user selects an item from the menu.

                    Parameters
                        listener : Listener to notify
                */
                /*
                    public abstract boolean onMenuItemClick (MenuItem item)
                        This method will be invoked when a menu item is clicked if the item itself
                        did not already handle the event.

                    Parameters
                        item : MenuItem that was clicked
                    Returns
                        true : if the event was handled, false otherwise.
                */
                // Set a click listener for menu item click
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        switch(menuItem.getItemId()){
                            // Handle the non group menu items here
                            case R.id.red:
                                // Set the text color to red
                                mTextView.setTextColor(Color.RED);
                                return true;
                            case R.id.green:
                                // Set the text color to green
                                mTextView.setTextColor(Color.GREEN);
                                return true;
                            case R.id.blue:
                                // Set the text color to blue
                                mTextView.setTextColor(Color.BLUE);
                                return true;
                            default:
                                return false;
                        }
                    }
                });

                // Finally, show the popup menu
                popupMenu.show();
            }
        });
    }
}