Saturday, January 30, 2016

Android Popup Menu Example

MainActivity.java

package com.cfsuman.androidtutorials;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.PopupMenu;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private TextView mTextView;

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

        // Get the context
        mContext = this;

        // Get the widgets reference from XML layout
        mTextView = findViewById(R.id.textView);

        // Set a click listener for the text view
        mTextView.setOnClickListener(view -> {
            showPopupMenu();
        });
    }



    // Method to show a popup menu
    private void showPopupMenu(){
        // Initialize a new instance of popup menu
        PopupMenu popupMenu = new PopupMenu(mContext,mTextView);

        // Inflate the popup menu
        popupMenu.getMenuInflater().inflate(
                R.menu.textview_popup_menu,popupMenu.getMenu());


        // Set a click listener for menu item click
        popupMenu.setOnMenuItemClickListener(menuItem -> {
            int id = menuItem.getItemId();

            if (id == R.id.red){
                // Set the text color to red
                mTextView.setTextColor(Color.RED);
                return true;
            }else if (id == R.id.green){
                // Set the text color to green
                mTextView.setTextColor(Color.GREEN);
                return true;
            }else if (id == R.id.blue){
                // Set the text color to blue
                mTextView.setTextColor(Color.BLUE);
                return true;
            }else {
                return false;
            }
        });


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

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:padding="24dp">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:fontFamily="sans-serif"
        android:text="Click Me To Open A Popup Menu."
        android:textSize="50sp"
        android:textStyle="italic"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
res/menu/textview_popup_menu.xml

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