How to Add Popup Menu in Android?

Android Popup menu is really a good way to make your Android application more elegant and create a great UI.  And we already discuss some menu library for Android application like ;

Now let’s focus on PowerMenu Android library which is a library that let you implement Android popup menu so easily. Without going much more start with implementation.

Demo

Android Popup Menu PowerMenu Example
Android Popup Menu PowerMenu Example

This is a basic example on a screenshot. You can build PowerMenu(Android Popup Menu) using Builder.

PowerMenu powerMenu = new PowerMenu.Builder(context)
.addItemList(list) // list has "Novel", "Poerty", "Art"
.addItem(new PowerMenuItem("Journals", false))
.addItem(new PowerMenuItem("Travel", false))
.setAnimation(MenuAnimation.SHOWUP_TOP_LEFT) // Animation start point (TOP | LEFT)
.setMenuRadius(10f)
.setMenuShadow(10f)
.setTextColor(context.getResources().getColor(R.color.md_grey_800))
.setSelectedTextColor(Color.WHITE)
.setMenuColor(Color.WHITE)
.setSelectedMenuColor(context.getResources().getColor(R.color.colorPrimary))
.setOnMenuItemClickListener(onMenuItemClickListener)
.build();

You can add items or item List using PowerMenuItem class. This is how to initialize PowerMenuItem.

PowerMenuItem powerMenuItem = new PowerMenuItem(“Travel”, true);

At first, argument is item Title, and the other is setting selected status. If true, the item’s text or background colour is changed by your settings like below:

.setSelectedTextColor(Color.WHITE)

.setSelectedMenuColor(context.getResources().getColor(R.color.colorPrimary))

You can listen to item click.

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.setSelected(position); // change selected item

powerMenu.dismiss();

}

};

and the last, show popup

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

or

powerMenu.showAsDropDown(view, (int)xOffset, (int)yOffset);

Source

Social Networks:

Twitter

Facebook

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s