1

I have a recylerview with a option button in every item. On button click a popup menu is shown. In popup menu there must be a checkbox and a delete button as item. First menu item is checkbox. I have used following code.

<?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"
  xmlns:tools="http://schemas.android.com/tools"
>
<item
    android:id="@+id/action_watch"
    android:checkable="true"
    android:title="Watch"
    app:showAsAction="always"
    />

<item
    android:id="@+id/action_delete"
    android:icon="@drawable/ic_action_delete"
    android:title="Remove"
    app:showAsAction="withText|always"/>
</menu>

Problem is checkbox appears right of its text. I want it in left, but i can't. please help. TIA

ARiF
  • 1,079
  • 10
  • 23

2 Answers2

1

With above way not possible because Left section already allocated to title and icon.

If you want to display popup with checkbox then you can use Alertdialog with checkbox using adapter.

Or

You can use listview inside popup view.

ViramP
  • 1,659
  • 11
  • 11
1

I found a way. In case if anyone needs, i am giving it as a answer. First, set an unchecked checkbox as the icon of that menu item.

<?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"
  xmlns:tools="http://schemas.android.com/tools"
  app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >
    <item
        android:id="@+id/action_checkable"
        android:title="Check"
        app:showAsAction="always"
        android:icon="@drawable/ic_unchecked_checkbox" />
</menu>

Then forcefully show the menu icon and title using this:

try {
    Class<?> classPopupMenu = Class.forName(popupMenu.getClass().getName());
    Field mPopup = classPopupMenu.getDeclaredField("mPopup");
    mPopup.setAccessible(true);
    Object menuPopupHelper = mPopup.get(popupMenu);
    Class<?> classPopupHelper = Class.forName(menuPopupHelper
                .getClass().getName());
    Method setForceIcons = classPopupHelper.getMethod("setForceShowIcon", boolean.class);
    setForceIcons.invoke(menuPopupHelper, true);
} catch (Exception e) {
    e.printStackTrace();
}

Then change the icon and color of the menu while click on that icon. Keep track of check state using a boolean isSelected. Change the icon and it's color using this:

if(isSelected) {
    MenuItem checkable = (MenuItem) popupMenu.getMenu().findItem(R.id.action_checkable);
    Drawable checkableDrawable = getResources().getDrawable(R.drawable.ic_checked_checkbox);
    checkableDrawable.setColorFilter(getResources().getColor(R.color.primaryColor), PorterDuff.Mode.SRC_ATOP);
    checkable.setIcon(checkableDrawable);
}
ARiF
  • 1,079
  • 10
  • 23