diff --git a/blurkit/src/main/java/io/alterac/blurkit/BlurLayout.java b/blurkit/src/main/java/io/alterac/blurkit/BlurLayout.java index 7d03c9e..aabf802 100644 --- a/blurkit/src/main/java/io/alterac/blurkit/BlurLayout.java +++ b/blurkit/src/main/java/io/alterac/blurkit/BlurLayout.java @@ -2,6 +2,7 @@ import android.app.Activity; import android.content.Context; +import android.content.ContextWrapper; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.Canvas; @@ -301,7 +302,7 @@ private Bitmap blur() { private View getActivityView() { Activity activity; try { - activity = (Activity) getContext(); + activity = getActivity(); } catch (ClassCastException e) { return null; } @@ -309,6 +310,20 @@ private View getActivityView() { return activity.getWindow().getDecorView().findViewById(android.R.id.content); } + /** + * Casts context to Activity if context is a View or not. + */ + private Activity getActivity() { + Context context = this.getContext(); + while (context instanceof ContextWrapper) { + if (context instanceof Activity) { + return (Activity) context; + } + context = ((ContextWrapper) context).getBaseContext(); + } + return null; + } + /** * Returns the position in screen. Left abstract to allow for specific implementations such as * caching behavior.