Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blurred background behind dialog

I want dialog with blurred screen under it so I take "screenshot" of activity, blur it and set as background of dialog window as BitmapDrawable. Strange thing is that dialog is no more centered on screen and touch outside dialog don't dismiss it even if setCanceledOnTouchOutside(true) was called.

Question is: why this doesn't work? Respectively how to create dialog with blurred background?

public class BlurDialog extends DialogFragment {

public BlurDialog() {
}

public static BlurDialog newInstance() {
    return new BlurDialog();
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
            .setTitle("Title")
            .setMessage("Message")
            .setPositiveButton("OK", null)
            .setNegativeButton("Cancel", null)
            .create();
    alertDialog.setCanceledOnTouchOutside(true);


    View view = getActivity().getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    Bitmap b1 = view.getDrawingCache();

    Rect frame = new Rect();
    getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    int statusBarHeight = frame.top;
    final int width = getActivity().getWindow().getDecorView().getWidth();
    final int height = getActivity().getWindow().getDecorView().getHeight();

    Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height-statusBarHeight);

    //define this only once if blurring multiple times
    RenderScript rs = RenderScript.create(getActivity());

    //this will blur the bitmapOriginal with a radius of 8 and save it in bitmapOriginal
    final Allocation input = Allocation.createFromBitmap(rs, b); //use this constructor for best performance, because it uses USAGE_SHARED mode which reuses memory
    final Allocation output = Allocation.createTyped(rs, input.getType());
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setRadius(8f);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(b);

    alertDialog.getWindow().setBackgroundDrawable(new BitmapDrawable(getResources(), b));


    return alertDialog;
}
}

Screenshot

like image 876
viduka Avatar asked Sep 03 '14 10:09

viduka


2 Answers

This post is old, but for your information:

To create dialog with blurred background you can use this library :

https://github.com/tvbarthel/BlurDialogFragment

You can create a class that extends BlurDialogFragment and in the onCreateView method you can inflate your custom layout. See the example below:

public class CustomDialogFragment extends BlurDialogFragment {


@Override
protected boolean isActionBarBlurred() {
// Enable or disable the blur effect on the action bar.
// Disabled by default.
return true;
}

 @Override
 protected int getBlurRadius() {
// Allow to customize the blur radius factor.
return 7;
}

@Override
protected boolean isDimmingEnable() {
// Enable or disable the dimming effect.
// Disabled by default.
return false;
}


@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                     Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dialog_fragment_layout, container, 
false);


return v;
}

To show the dialog from your Activity:

FragmentManager fragmentManager = getFragmentManager();
CustomDialogFragment cdf = new CustomDialogFragment();
cdf.show(fragmentManager,"yourTag");

`

like image 190
fgueli Avatar answered Nov 18 '22 02:11

fgueli


Check this : https://stackoverflow.com/a/21278278/3891036

It works well for me.

OR

create a styles.xml

<style name="Theme.D1NoTitleDim" parent="android:style/Theme.Translucent">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:background">@android:color/transparent</item>        
</style>

And then on your dialog

dialog = new Dialog(context,styles);
like image 29
Yksh Avatar answered Nov 18 '22 00:11

Yksh