How to Display Icons in a Popupmenu

PopupMenu with icons

I would implement it otherwise:

Create a PopUpWindow layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llSortChangePopup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/sort_popup_background"
android:orientation="vertical" >

<TextView
android:id="@+id/tvDistance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/distance"
android:layout_weight="1.0"
android:layout_marginLeft="20dp"
android:paddingTop="5dp"
android:gravity="center_vertical"
android:textColor="@color/my_darker_gray" />

<ImageView
android:layout_marginLeft="11dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/sort_popup_devider"
android:contentDescription="@drawable/sort_popup_devider"/>

<TextView
android:id="@+id/tvPriority"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/priority"
android:layout_weight="1.0"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:clickable="true"
android:onClick="popupSortOnClick"
android:textColor="@color/my_black" />

<ImageView
android:layout_marginLeft="11dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/sort_popup_devider"
android:contentDescription="@drawable/sort_popup_devider"/>

<TextView
android:id="@+id/tvTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/time"
android:layout_weight="1.0"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:clickable="true"
android:onClick="popupSortOnClick"
android:textColor="@color/my_black" />

<ImageView
android:layout_marginLeft="11dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/sort_popup_devider"
android:contentDescription="@drawable/sort_popup_devider"/>

<TextView
android:id="@+id/tvStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/status"
android:layout_weight="1.0"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:textColor="@color/my_black"
android:clickable="true"
android:onClick="popupSortOnClick"
android:paddingBottom="10dp"/>

</LinearLayout>

and also create the PopUpWindow in your Activity:

    // The method that displays the popup.
private void showStatusPopup(final Activity context, Point p) {

// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.llStatusChangePopup);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.status_popup_layout, null);

// Creating the PopupWindow
changeStatusPopUp = new PopupWindow(context);
changeStatusPopUp.setContentView(layout);
changeStatusPopUp.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
changeStatusPopUp.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
changeStatusPopUp.setFocusable(true);

// Some offset to align the popup a bit to the left, and a bit down, relative to button's position.
int OFFSET_X = -20;
int OFFSET_Y = 50;

//Clear the default translucent background
changeStatusPopUp.setBackgroundDrawable(new BitmapDrawable());

// Displaying the popup at the specified location, + offsets.
changeStatusPopUp.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
}

finally pop it up onClick of a button or anything else:

 imTaskStatusButton.setOnClickListener(new OnClickListener() 
{
public void onClick(View v)
{
int[] location = new int[2];
currentRowId = position;
currentRow = v;
// Get the x, y location and store it in the location[] array
// location[0] = x, location[1] = y.
v.getLocationOnScreen(location);

//Initialize the Point with x, and y positions
point = new Point();
point.x = location[0];
point.y = location[1];
showStatusPopup(TasksListActivity.this, point);
}
});

A Good example for PopUpWindow:

http://androidresearch.wordpress.com/2012/05/06/how-to-create-popups-in-android/

Is it possible to display icons in a PopupMenu?

If you're willing to be a bit adventurous, look at Google's source code for PopupMenu. Create your own class i.e. MyPopupMenu that is the same as Google's PopupMenu class, but make one slight change.

In PopupMenu's constructor:

public MyPopupMenu(Context context, View anchor) {
// TODO Theme?
mContext = context;
mMenu = new MenuBuilder(context);
mMenu.setCallback(this);
mAnchor = anchor;
mPopup = new MenuPopupHelper(context, mMenu, anchor);
mPopup.setCallback(this);
mPopup.setForceShowIcon(true); //ADD THIS LINE

}

use the method setForceShowIcon to force it to show the icon. You can also just expose a public method to set this flag as well depending on your needs.

How to show icons along with text in PopupMenu in android?

The simple answer is you can't. You can use a similar widget, ListPopWindow, which uses an adapter to draw its content, giving you the flexibility you need.

Edit. For the width problem you have to call setContentWidth. You can easily iterate on the adapter's dataset in order to calculate the max width, and use this value as parameter for setContentWidth

PopUp menu with icons

By default the PopupMenu doesn't show Icons next to your title.

What you need to do is either create your own PopupMenu implementation and force the use of icons by overriding the default constructor :

 public CustomPopupMenu(Context context, View anchor) {
...
mPopup.setForceShowIcon(true);
}

OR

Just before showing your PopupMenu, use reflection to invoke the desired method

        //... initialization of your PopupMenu
Object menuHelper;
Class[] argTypes;
try {
Field fMenuHelper = PopupMenu.class.getDeclaredField("mPopup");
fMenuHelper.setAccessible(true);
menuHelper = fMenuHelper.get(popupMenu);
argTypes = new Class[]{boolean.class};
menuHelper.getClass().getDeclaredMethod("setForceShowIcon", argTypes).invoke(menuHelper, true);
} catch (Exception e) {
// Possible exceptions are NoSuchMethodError and NoSuchFieldError
//
// In either case, an exception indicates something is wrong with the reflection code, or the
// structure of the PopupMenu class or its dependencies has changed.
//
// These exceptions should never happen since we're shipping the AppCompat library in our own apk,
// but in the case that they do, we simply can't force icons to display, so log the error and
// show the menu normally.
}
popUpMenu.show();

PopupMenu Icon doesn't show

I found a solution in this link and apply it to my code.

PopupMenu popup = new PopupMenu(mContext, view);
try {
Field[] fields = popup.getClass().getDeclaredFields();
for (Field field : fields) {
if ("mPopup".equals(field.getName())) {
field.setAccessible(true);
Object menuPopupHelper = field.get(popup);
Class<?> classPopupHelper = Class.forName(menuPopupHelper.getClass().getName());
Method setForceIcons = classPopupHelper.getMethod("setForceShowIcon", boolean.class);
setForceIcons.invoke(menuPopupHelper, true);
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}

How to show Icon in drop down menu of PopupMenu

I discovered from my research that to show drop-down popup menu with menu item icon takes deep process. And the process is not clean.
However, i have found a solution. Creating sub menu seems to be the only lee-way to get what we want. And I encourage anyone seeking to create menu icon to follow this way I founded for now while we wait further improvement from google.

<?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/action_more"
android:icon="@android:drawable/ic_menu_more"
android:orderInCategory="1"
android:title="More Options »"
app:showAsAction="always">

<menu>

<item
android:id="@+id/settings_menu"
android:title="Settings"
android:icon="@drawable/ic_settings"/>

<item
android:id="@+id/manual_menu"
android:title="User Manual"
android:icon="@drawable/ic_developer" />

<item
android:id="@+id/about_menu"
android:title="About"
android:icon="@drawable/ic_about"/>

</menu>
</item>

</menu>

Android Toolbar Popup menu not showing icons

I found this solution: https://stackoverflow.com/a/30337653/197127. Basically, overriding a method and it does not break the device menu button or the overflow.
Thanks to all.

How to add icon/image along with popup menu in flutter?

You can use ListTile as child or Row.

PopupMenuItem<String>(
value: choice,
child: ListTile(
leading: Icon(Icons.work), // your icon
title: Text(choice),
),
)


Related Topics



Leave a reply



Submit