0

I have a Radio Button group in Popup Menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:id="@+id/mode_group"
        android:checkableBehavior="single">
        <item android:id="@+id/red"
            android:title="Red" />
        <item android:id="@+id/white"
            android:title="White" />
        <item android:id="@+id/blue"
            android:title="Blue" />
    </group>
</menu>

It shows the icon to the right of the title:

popup menu screenshot

Is it possible to configure it to show the icon on the left side?

TofferJ
  • 4,678
  • 1
  • 37
  • 49
lannyf
  • 9,865
  • 12
  • 70
  • 152
  • check [this](https://stackoverflow.com/questions/49901594/how-to-change-the-direction-of-a-popup-menu-item-from-right-to-left) – Mohammed Alaa Feb 06 '20 at 21:21
  • @MohammedAlaa, thx, it does put the radio button at left, but text does not follow button immediately, so the button is left aligned and text is right aligned with big gap in between. like [bt xxxx space xxxx Red], Looking for some thing like [bt . Red xxxx space xxxx ]. – lannyf Feb 07 '20 at 13:38
  • is this like what you want to implement [image](https://imgur.com/r60iceP) – Mohammed Alaa Feb 07 '20 at 17:48
  • yes, but the `android:supportsRtl="true"` . pushes button at left and text at far right – lannyf Feb 07 '20 at 18:49

1 Answers1

1

You can use reflection to achieve this

java code

 Context contextWrapper = new ContextThemeWrapper(MainActivity.this, R.style.PopupStyle);
 PopupMenu popup = new PopupMenu(contextWrapper,v);

           /*  The below code in try catch is responsible to display icons*/

           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();
           }
           popup.getMenuInflater().inflate(R.menu.pop_up, popup.getMenu());

           popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
               public boolean onMenuItemClick(MenuItem item) {
                   Toast.makeText(MainActivity.this,
                           "Clicked popup menu item " + item.getTitle(),
                           Toast.LENGTH_SHORT).show();
                   return true;
               }
           });

popup.show();

pop_up.xml

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

and if you want to change the menu style

in styles.xml

<style name="PopupStyle" parent="Widget.AppCompat.PopupMenu">
   <item name="android:textColorPrimary">@color/colorAccent</item>
</style>

or if you want to create custom layout check link1, link2

Mohammed Alaa
  • 3,140
  • 2
  • 19
  • 21