AlertDialog with Blur background in position center

[亡魂溺海] 提交于 2019-11-28 08:51:06

Note: This screen background can be anything you have on it, what it does is take a screenshot of it and blur that finally set it as the background of the alertDialog with blur effect

Problem was alertDialog attached with that blur background was always on the top and i couldn't change that.

I came up with this solution.

I had to use two alertDialogs one is for the blur effect(for the use of getWindow()) and it does not get display now(I removed its setContentView / can use transparent background)

The other one is the real alert dialog got display after the blur effect.

MainActivity

public class MainActivity extends AppCompatActivity {

    private  AlertDialog dialogWhichDisplayAlert;
    private  Dialog fakeDialogUseToGetWindowForBlurEffect;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }


    @Override
    public void onBackPressed() {


        fakeDialogUseToGetWindowForBlurEffect = new Dialog(MainActivity.this);
        //  fakeDialogUseToGetWindowForBlurEffect.setContentView(R.layout.fakealert); // removed the content so not visible



        new BlurAsyncTask().execute();

    }

    class BlurAsyncTask extends AsyncTask<Void, Integer, Bitmap> {


        protected Bitmap doInBackground(Void...arg0) {

            Bitmap map  = AppUtils.takeScreenShot(MainActivity.this);
            Bitmap fast = new BlurView().fastBlur(map, 10);
            return fast;
        }


        protected void onPostExecute(Bitmap result) {
            if (result != null){
                final Drawable draw=new BitmapDrawable(getResources(),result);
                Window window = fakeDialogUseToGetWindowForBlurEffect.getWindow();
                window.setBackgroundDrawable(draw);
                window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
                window.setGravity(Gravity.CENTER);
                fakeDialogUseToGetWindowForBlurEffect.show();


                // real one
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.AlertDialogCustom);
                builder.setTitle("Lets Blur");
                builder.setMessage("This is Blur Demo");
                builder.setCancelable(false);
                builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        fakeDialogUseToGetWindowForBlurEffect.dismiss();
                        dialog.cancel();
                    }
                });
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        fakeDialogUseToGetWindowForBlurEffect.dismiss();
                        dialog.cancel();
                    }
                });
                dialogWhichDisplayAlert = builder.create();

                // position real dialogWhichDisplayAlert using Gravity.CENTER;
                dialogWhichDisplayAlert.requestWindowFeature(Window.FEATURE_NO_TITLE);
                WindowManager.LayoutParams wmlp = dialogWhichDisplayAlert.getWindow().getAttributes();
                wmlp.gravity = Gravity.CENTER;
                dialogWhichDisplayAlert.show();

            }

        }
    }
}

[other classes used are same as the question]

style

<style name="AlertDialogCustom" parent="@android:style/Theme.Dialog">
        <item name="android:textColor">#00FF00</item>
        <item name="android:typeface">monospace</item>
        <item name="android:textSize">10sp</item>
    </style>

Out put

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!