SkillAgentSearch skills...

PowerMenu

:fire: Powerful and modernized popup menu with fully customizable animations.

Install / Use

/learn @skydoves/PowerMenu

README

<h1 align="center">PowerMenu</h1></br> <p align="center"> :fire: PowerMenu is a modernized and fully customizable popup menu, which can be displayed on top of layouts. <br> </p> </br> <p align="center"> <a href="https://devlibrary.withgoogle.com/products/android/repos/skydoves-powermenu"><img alt="Google" src="https://skydoves.github.io/badges/google-devlib.svg"/></a><br> <a href="https://opensource.org/licenses/Apache-2.0"><img alt="License" src="https://img.shields.io/badge/License-Apache%202.0-blue.svg"/></a> <a href="https://android-arsenal.com/api?level=15"><img alt="API" src="https://img.shields.io/badge/API-15%2B-brightgreen.svg?style=flat"/></a> <a href="https://github.com/skydoves/PowerMenu/actions"><img alt="CI" src="https://github.com/skydoves/PowerMenu/workflows/Android%20CI/badge.svg"/></a> <a href="https://androidweekly.net/issues/issue-326"><img alt="AndroidWeekly" src="https://skydoves.github.io/badges/android-weekly.svg"/></a> <a href="https://proandroiddev.com/building-a-beautiful-disney-android-application-1-material-motion-systems-34607eae2501"><img alt="Medium" src="https://skydoves.github.io/badges/Story-Medium.svg"/></a> <a href="https://github.com/skydoves"><img alt="Profile" src="https://skydoves.github.io/badges/skydoves.svg"/></a> <a href="https://medium.com/@skydoves/how-to-implement-modern-popup-in-android-3d51f4a40c56"><img alt="Javadoc" src="https://img.shields.io/badge/Javadoc-PowerMenu-yellow.svg"/></a> </p> </br> <p align="center"> <img src="https://user-images.githubusercontent.com/24237865/63956079-c0e0cb80-cac0-11e9-82ca-4397ca1f3750.gif" width="32%"/> <img src="https://user-images.githubusercontent.com/24237865/63956377-42385e00-cac1-11e9-9639-81eac4b7511f.jpg" width="32%"/> </p>

Download

Maven Central

I really appreciate that 🔥 PowerMenu has been used in more than 340,000+ projects all over the world. 🌎 <br>

screenshot1903218121

Gradle

And add the dependency below to your module's build.gradle file:

dependencies {
  implementation "com.github.skydoves:powermenu:2.2.4"
}

Table of Contents

1. PowerMenu <br> 2. Customizing Popup <br> 3. Preference<br> 4. Menu Effect <br> 5. Dialogs <br> 6. Anchor <br> 7. Background <br> 8. Avoid Memory leak <br> 9. Functions <br> 10. Lazy initialization in Kotlin <br>

Usage

Basic example

This is a basic example on a screenshot. Here is how to create PowerMenu using PowerMenu.Builder.

PowerMenu powerMenu = new PowerMenu.Builder(context)
          .addItemList(list) // list has "Novel", "Poetry", "Art"
          .addItem(new PowerMenuItem("Journals", false)) // add an item.
          .addItem(new PowerMenuItem("Travel", false)) // aad an item list.
          .setAnimation(MenuAnimation.SHOWUP_TOP_LEFT) // Animation start point (TOP | LEFT).
          .setMenuRadius(10f) // sets the corner radius.
          .setMenuShadow(10f) // sets the shadow.
          .setTextColor(ContextCompat.getColor(context, R.color.md_grey_800))
          .setTextGravity(Gravity.CENTER)
          .setTextTypeface(Typeface.create("sans-serif-medium", Typeface.BOLD))
          .setSelectedTextColor(Color.WHITE)
          .setMenuColor(Color.WHITE)
          .setSelectedMenuColor(ContextCompat.getColor(context, R.color.colorPrimary))
          .setOnMenuItemClickListener(onMenuItemClickListener)
          .build();

We can add an item or an item list using PowerMenuItem class. This is how to initialize PowerMenuItem.

new PowerMenuItem("Travel");
new PowerMenuItem("Poetery", false); // item name, isSelected (default is false).
new PowerMenuItem("Art", R.drawable.icon_art) // item name, item menu icon.
new PowerMenuItem("Travel", R.drawable.icon_travel, true) // item name, item menu icon, isSelected .

The first argument is an item title, and the other is selected status. <br> If isSelected is true, the item's text and the background color will be changed by settings like below.<br>

.setSelectedTextColor(Color.WHITE) // sets the color of the selected item text. 
.setSelectedMenuColor(ContextCompat.getColor(context, R.color.colorPrimary)) // sets the color of the selected menu item color.

OnMenuItemClickListener is for listening to the item click of the popup menu.

private OnMenuItemClickListener<PowerMenuItem> onMenuItemClickListener = new OnMenuItemClickListener<PowerMenuItem>() {
    @Override
    public void onItemClick(int position, PowerMenuItem item) {
        Toast.makeText(getBaseContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
        powerMenu.setSelectedPosition(position); // change selected item
        powerMenu.dismiss();
    }
};

After implementing the listener, we should set using setOnMenuItemClickListener method.

.setOnMenuItemClickListener(onMenuItemClickListener)

The last, show the popup! Various show & dismiss methods.

powerMenu.showAsDropDown(view); // view is an anchor

Customizing Popup

We can customize item styles using CustomPowerMenu and your customized adapter. <br> Here is how to customize the popup item that has an icon. <br>

custom0 gif0 <br>

Firstly, we should create our item model class.

public class IconPowerMenuItem {
    private Drawable icon;
    private String title;

    public IconPowerMenuItem(Drawable icon, String title) {
        this.icon = icon;
        this.title = title;
    }
 // --- skipped setter and getter methods
}

And we should create our customized XML layout and an adapter. <br> Custom Adapter should extend MenuBaseAdapter<YOUR_ITEM_MODEL_CLASS>.

public class IconMenuAdapter extends MenuBaseAdapter<IconPowerMenuItem> {

    @Override
    public View getView(int index, View view, ViewGroup viewGroup) {
        final Context context = viewGroup.getContext();

        if(view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.item_icon_menu, viewGroup, false);
        }

        IconPowerMenuItem item = (IconPowerMenuItem) getItem(index);
        final ImageView icon = view.findViewById(R.id.item_icon);
        icon.setImageDrawable(item.getIcon());
        final TextView title = view.findViewById(R.id.item_title);
        title.setText(item.getTitle());
        return super.getView(index, view, viewGroup);
    }
}

The last, create the CustomPowerMenu with the onMenuItemClickListener.

CustomPowerMenu customPowerMenu = new CustomPowerMenu.Builder<>(context, new IconMenuAdapter())
       .addItem(new IconPowerMenuItem(ContextCompat.getDrawable(context, R.drawable.ic_wechat), "WeChat"))
       .addItem(new IconPowerMenuItem(ContextCompat.getDrawable(context, R.drawable.ic_facebook), "Facebook"))
       .addItem(new IconPowerMenuItem(ContextCompat.getDrawable(context, R.drawable.ic_twitter), "Twitter"))
       .addItem(new IconPowerMenuItem(ContextCompat.getDrawable(context, R.drawable.ic_line), "Line"))
       .setOnMenuItemClickListener(onIconMenuItemClickListener)
       .setAnimation(MenuAnimation.SHOWUP_TOP_RIGHT)
       .setMenuRadius(10f)
       .setMenuShadow(10f)
       .build();
private OnMenuItemClickListener<IconPowerMenuItem> onIconMenuItemClickListener = new OnMenuItemClickListener<IconPowerMenuItem>() {
    @Override
    public void onItemClick(int position, IconPowerMenuItem item) {
        Toast.makeText(getBaseContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
        iconMenu.dismiss();
    }
};

Preference

PowerMenu supports saving of the last selected menu and recovering as lifecycle.<br> Here is how to save and recover selected menu.

return new PowerMenu.Builder(context)
    // saves the position automatically when the menu is selected.
    // If we set the same preference name on the other PowerMenus, they will share the saving position.
   .setPreferenceName("HamburgerPowerMenu")

    // invokes the listener automatically that has the saved position arguments along the lifecycle rule.
    // lifecycle rules should be ON_CREATE, ON_START or ON_RESUME.
    // in the below codes, the onMenuClickListener will be invoked when onCreate lifecycle.
   .setLifecycleOwner(lifecycleOwner)
   .setInitializeRule(Lifecycle.Event.ON_CREATE, 0) // Lifecycle.Event and default position.
   --- skips ---

Here are the methods related to preference.

.getPreferenceName() // gets the preference name of PowerMenu.
.getPreferencePosition(int defaultPosition) // gets the saved preference position from the SharedPreferences.
.setPreferencePosition(int defaultPosition) // sets the preference position name for persistence manually.
.clearPreference() // clears the preference name of PowerMenu.

Menu Effect

We can give two ty

View on GitHub
GitHub Stars1.2k
CategoryDevelopment
Updated18d ago
Forks175

Languages

Java

Security Score

100/100

Audited on Mar 11, 2026

No findings