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:
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;
}
}
getThemeReference
method. – Merited