PopupWindow background sometimes gets transparent and purple
Asked Answered
M

1

7

Here's how I create a PopupWindow:

private static PopupWindow createPopup(FragmentActivity activity, View view)
{
    PopupWindow popup = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
    popup.setOutsideTouchable(true);
    popup.setFocusable(true);
    popup.setBackgroundDrawable(new ColorDrawable(Tools.getThemeReference(activity, R.attr.main_background_color)));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        popup.setElevation(Tools.convertDpToPixel(8, activity));
    PopupWindowCompat.setOverlapAnchor(popup, true);

    return popup;
}

main_background_color is a solid color, white or black, depending on the theme. Sometimes following happens:

enter image description here

How can I avoid this? It happens in the emulator with android 6 SOMETIMES only for example... Normally, the PopupWindow background works as expected though...

Edit

Additionally, here's my getThemeReference method:

public static int getThemeReference(Context context, int attribute)
{
    TypedValue typeValue = new TypedValue();
    context.getTheme().resolveAttribute(attribute, typeValue, false);
    if (typeValue.type == TypedValue.TYPE_REFERENCE)
    {
        int ref = typeValue.data;
        return ref;
    }
    else
    {
        return -1;
    }
}

EDIT 2 - this may solve the problem: using getThemeColor instead of getThemeReference

public static int getThemeColor(Context context, int attribute)
{
    TypedValue typeValue = new TypedValue();
    context.getTheme().resolveAttribute(attribute, typeValue, true);
    if (typeValue.type >= TypedValue.TYPE_FIRST_COLOR_INT && typeValue.type <= TypedValue.TYPE_LAST_COLOR_INT)
    {
        int color = typeValue.data;
        return color;
    }
    else
    {
        return -1;
    }
}
Mease answered 27/6, 2016 at 9:23 Comment(2)
Please post your getThemeReference method.Merited
Done. Still, I don't believe the reason for the problem lies in there, as this would mean, that this problem happens always, but it's only happening rarely (and until now, I only saw it on android 6)Mease
M
5

Thanks for the update, I asked you to show the method because I actually use the same kind of thing in my apps to retrieve color attributes, but our methods are a bit different.

Here is mine:

public static int getThemeColor(Context context, int attributeId) {
    TypedValue typedValue = new TypedValue();

    TypedArray a = context.obtainStyledAttributes(typedValue.data, new int[] { attributeId });
    int color = a.getColor(0, 0);

    a.recycle();

    return color;
}

Even though I can't be sure it is indeed the problem, there is something wrong with your comments. The call new ColorDrawable() is expecting a color, not a reference. I sometimes also did this mistake in the past and also got weird colors, because the system was trying to generate a color with a reference ID. Have you tried a real color like red to see if your method really works?

I would replace anyway your method with mine because it guarantees you to retrieve a color.

Merited answered 30/6, 2016 at 10:16 Comment(5)
So you think (I may do it wrong, that's true), that the same device and system, creates different colors with my method? This happens while running my app. You open the popup, it's fine, you rotate the device, reopen thepopup, it's purple (it changes colors unpredicatably, most of the time the color is correct)Mease
Btw, my R.attr.main_background_color is a reference... It's not a color... I have a second method, with the same name as yours ;-). It should work as well, I just posted it in my question as well... It may really be because I'm pass a color reference instead of a totally resolved color...Mease
@Mease As I said, not sure it would make any difference, but it is worth the try. In the rest of the code you provided, I don't see anything else that could be related to this problem.Merited
At least, the hint that ColorDrawable expects a color and no reference is still correct, so this may be the problem... I'll try it and look, if the problem is gone afterwardsMease
Until now, this really seems to have fixed the problemMease

© 2022 - 2024 — McMap. All rights reserved.