Material-components-android: [Menu] ListPopupWindow doesn't respect the Elevation overlay

Created on 11 Sep 2019  路  8Comments  路  Source: material-components/material-components-android

Description

ListPopupWindow doesn't use any dynamic elevation overlay. Instead of this, it uses a absolute 11% alpha overlay. So if the parent view already has elevation, it doesn't respect that elevation.

Screenshot_20190911-081823

Expected behaviour

ListPopupWindow should use MaterialShapeDrawable as background. But ListPopupWindow doesn't have any exposed getElevation or setElevation method. So we are unable to get the Elevation value and can't implement proper background for listPopupWindow.

bug

Most helpful comment

Unfortunately ListPopupWindow is not part of the MDC-Android library and we can't theme its default background to a MaterialShapeDrawable in XML, so at this time we can't update it to support absolute elevation overlays.

For now I'm wondering if you can create a MaterialShapeDrawable with absolute elevation factored in:

    ElevationOverlayProvider elevationOverlayProvider = new ElevationOverlayProvider(context);
    MaterialShapeDrawable background = MaterialShapeDrawable.createWithElevationOverlay(context, menuElevation);
    background.setParentAbsoluteElevation(elevationOverlayProvider.getParentAbsoluteElevation(menu));

And set that as the ListPopupBackground via ListPopupBackground#setBackgroundDrawable (you may need to wrap the MaterialShapeDrawable in an InsetDrawable and set some corners on the MaterialShapeDrawable).

Screenshot_20191218-202031
Thanks a lot. I am now quite satisfied by the result. I extended ListPopupWindow and set MaterialShapeDrawable as background.

All 8 comments

Unfortunately ListPopupWindow is not part of the MDC-Android library and we can't theme its default background to a MaterialShapeDrawable in XML, so at this time we can't update it to support absolute elevation overlays.

For now I'm wondering if you can create a MaterialShapeDrawable with absolute elevation factored in:

    ElevationOverlayProvider elevationOverlayProvider = new ElevationOverlayProvider(context);
    MaterialShapeDrawable background = MaterialShapeDrawable.createWithElevationOverlay(context, menuElevation);
    background.setParentAbsoluteElevation(elevationOverlayProvider.getParentAbsoluteElevation(menu));

And set that as the ListPopupBackground via ListPopupBackground#setBackgroundDrawable (you may need to wrap the MaterialShapeDrawable in an InsetDrawable and set some corners on the MaterialShapeDrawable).

Unfortunately ListPopupWindow is not part of the MDC-Android library and we can't theme its default background to a MaterialShapeDrawable in XML, so at this time we can't update it to support absolute elevation overlays.

For now I'm wondering if you can create a MaterialShapeDrawable with absolute elevation factored in:

    ElevationOverlayProvider elevationOverlayProvider = new ElevationOverlayProvider(context);
    MaterialShapeDrawable background = MaterialShapeDrawable.createWithElevationOverlay(context, menuElevation);
    background.setParentAbsoluteElevation(elevationOverlayProvider.getParentAbsoluteElevation(menu));

And set that as the ListPopupBackground via ListPopupBackground#setBackgroundDrawable (you may need to wrap the MaterialShapeDrawable in an InsetDrawable and set some corners on the MaterialShapeDrawable).

Screenshot_20191218-202031
Thanks a lot. I am now quite satisfied by the result. I extended ListPopupWindow and set MaterialShapeDrawable as background.

Another alternative is to use ColorDrawable with color provided by MaterialOverlayProvider,
eg: popupListWindow.setBackgroundDrawable(new ColorDrawable(elevatedColor));

@Sourav-21 Is there a reason for not exposing the setElevation(float) method of the PopupWindow mPopup; member in ListPopupWindow? Are there concerns around older APIs that might not support it?

@cristiannancu the setElevation method of PopupWindow is already exposed. But there is no setElevation inside ListPopupWindow. But you can set elevation vai style res xml.

Yes that's what I meant - expose it in ListPopupWindow. I only mentioned the mPopup member because that's the View being used by ListPopupWindow.

@Sourav-21 the question still stands: is it possible to add the setter&getter for elevation to ListPopupWindow?

public void setElevation(float elevation) {
        mPopup.setElevation(elevation);
}

public float getElevation() {
        return mPopup.getElevation();
}

@cristiannancu yes. Possible.
I don't know why there is no setElevation method in listPopupWindow.

I think the material design team should rewrite listPopupWindow from scratch.

@cristiannancu In my custom listPopupWindow class, I changed lot of things. I completely ditched listView and embraced RecyclerView.

Was this page helpful?
0 / 5 - 0 ratings