title: AlertDialog with Blur background in position center
body: <p><strong>Question</strong> </p>
  
  <p>Tried to blur (not DIM) the background of a <code>Dialog</code>.</p>
  
  <p>and <code>FLAG_BLUR_BEHIND</code> is <em>deprecated</em>. </p>
  
  <p>Approach is, take a screenshot form the currant view and blur it.Then <strong>attach to the background</strong>,When <code>AlertDialog</code> appears,  and it works.</p>
  
  <p>but <code>AlertDialog</code> is always in the Top <em>not</em> in the center.(check the image)</p>
  
  <p><code>WindowManager.LayoutParams</code> has no effect <em>after</em> attach .(before attach it to the background,alert <em>can</em> position to center, but after attaching the blur view to the background <code>AlertDialog</code> always go to the top )</p>
  
  <p>How can I make my alert center in the screen?</p>
  
  <p>below is code i tried,any guide will be appreciated ,</p>
  
  <p><em>Displayed alert when i press back.</em></p>
  
  <pre><code>     @Override
          public void onBackPressed() {
  
              AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.AlertDialogCustom);
              builder.setTitle("Lets Blur");
              builder.setMessage("This is Blur Demo");
              builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                      dialog.cancel();
                  }
              });
  
  
              dialog = builder.create();
  
  // this position alert in the CENTER
              dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
              WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();
              wmlp.gravity = Gravity.CENTER;
              dialog.show();
  
              new BlurAsyncTask().execute();
  
          }
  
          class BlurAsyncTask extends AsyncTask&lt;Void, Integer, Bitmap&gt; {
  
  
              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 = dialog.getWindow();
  // this position alert again in the TOP -- need to avoid that!
                      window.setBackgroundDrawable(draw);
                      window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
                      window.setGravity(Gravity.CENTER);
                      dialog.show();
  
                  }
  
              }
  </code></pre>
  
  <p>Other classes </p>
  
  <p><strong>AppUtils</strong> </p>
  
  <pre><code>public class AppUtils {
  
      public static Bitmap takeScreenShot(Activity activity) {
          View view = activity.getWindow().getDecorView();
          view.setDrawingCacheEnabled(true);
          view.buildDrawingCache();
  
  
          Bitmap b1 = view.getDrawingCache();
          Rect frame = new Rect();
          activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
          int statusBarHeight = frame.top;
  
          Display display = activity.getWindowManager().getDefaultDisplay();
          Point size = new Point();
          display.getSize(size);
          int width = size.x;
          int height = size.y;
  
  
          Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);
          view.destroyDrawingCache();
          return b;
      }
  }
  </code></pre>
  
  <hr>
  
  <p><strong>BlurView</strong> </p>
  
  <pre><code>public class BlurView {
  
      public Bitmap fastBlur(Bitmap sentBitmap, int radius) {
          Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
          if (radius &lt; 1) {
              return (null);
          }
          int w = bitmap.getWidth();
          int h = bitmap.getHeight();
          int[] pix = new int[w * h];
          Log.e("pix", w + " " + h + " " + pix.length);
          bitmap.getPixels(pix, 0, w, 0, 0, w, h);
          int wm = w - 1;
          int hm = h - 1;
          int wh = w * h;
          int div = radius + radius + 1;
          int r[] = new int[wh];
          int g[] = new int[wh];
          int b[] = new int[wh];
          int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;
          int vmin[] = new int[Math.max(w, h)];
          int divsum = (div + 1) &gt;&gt; 1;
          divsum *= divsum;
          int dv[] = new int[256 * divsum];
          for (i = 0; i &lt; 256 * divsum; i++) {
              dv[i] = (i / divsum);
          }
          yw = yi = 0;
          int[][] stack = new int[div][3];
          int stackpointer;
          int stackstart;
          int[] sir;
          int rbs;
          int r1 = radius + 1;
          int routsum, goutsum, boutsum;
          int rinsum, ginsum, binsum;
          for (y = 0; y &lt; h; y++) {
              rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
              for (i = -radius; i &lt;= radius; i++) {
                  p = pix[yi + Math.min(wm, Math.max(i, 0))];
                  sir = stack[i + radius];
                  sir[0] = (p &amp; 0xff0000) &gt;&gt; 16;
                  sir[1] = (p &amp; 0x00ff00) &gt;&gt; 8;
                  sir[2] = (p &amp; 0x0000ff);
                  rbs = r1 - Math.abs(i);
                  rsum += sir[0] * rbs;
                  gsum += sir[1] * rbs;
                  bsum += sir[2] * rbs;
                  if (i &gt; 0) {
                      rinsum += sir[0];
                      ginsum += sir[1];
                      binsum += sir[2];
                  } else {
                      routsum += sir[0];
                      goutsum += sir[1];
                      boutsum += sir[2];
                  }
              }
              stackpointer = radius;
              for (x = 0; x &lt; w; x++) {
                  r[yi] = dv[rsum];
                  g[yi] = dv[gsum];
                  b[yi] = dv[bsum];
                  rsum -= routsum;
                  gsum -= goutsum;
                  bsum -= boutsum;
                  stackstart = stackpointer - radius + div;
                  sir = stack[stackstart % div];
                  routsum -= sir[0];
                  goutsum -= sir[1];
                  boutsum -= sir[2];
                  if (y == 0) {
                      vmin[x] = Math.min(x + radius + 1, wm);
                  }
                  p = pix[yw + vmin[x]];
                  sir[0] = (p &amp; 0xff0000) &gt;&gt; 16;
                  sir[1] = (p &amp; 0x00ff00) &gt;&gt; 8;
                  sir[2] = (p &amp; 0x0000ff);
                  rinsum += sir[0];
                  ginsum += sir[1];
                  binsum += sir[2];
                  rsum += rinsum;
                  gsum += ginsum;
                  bsum += binsum;
                  stackpointer = (stackpointer + 1) % div;
                  sir = stack[(stackpointer) % div];
                  routsum += sir[0];
                  goutsum += sir[1];
                  boutsum += sir[2];
                  rinsum -= sir[0];
                  ginsum -= sir[1];
                  binsum -= sir[2];
                  yi++;
              }
              yw += w;
          }
          for (x = 0; x &lt; w; x++) {
              rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
              yp = -radius * w;
              for (i = -radius; i &lt;= radius; i++) {
                  yi = Math.max(0, yp) + x;
                  sir = stack[i + radius];
                  sir[0] = r[yi];
                  sir[1] = g[yi];
                  sir[2] = b[yi];
                  rbs = r1 - Math.abs(i);
                  rsum += r[yi] * rbs;
                  gsum += g[yi] * rbs;
                  bsum += b[yi] * rbs;
                  if (i &gt; 0) {
                      rinsum += sir[0];
                      ginsum += sir[1];
                      binsum += sir[2];
                  } else {
                      routsum += sir[0];
                      goutsum += sir[1];
                      boutsum += sir[2];
                  }
                  if (i &lt; hm) {
                      yp += w;
                  }
              }
              yi = x;
              stackpointer = radius;
              for (y = 0; y &lt; h; y++) {
                  // Preserve alpha channel: ( 0xff000000 &amp; pix[yi] )
                  pix[yi] = ( 0xff000000 &amp; pix[yi] ) | ( dv[rsum] &lt;&lt; 16 ) | ( dv[gsum] &lt;&lt; 8 ) | dv[bsum];
                  rsum -= routsum;
                  gsum -= goutsum;
                  bsum -= boutsum;
                  stackstart = stackpointer - radius + div;
                  sir = stack[stackstart % div];
                  routsum -= sir[0];
                  goutsum -= sir[1];
                  boutsum -= sir[2];
                  if (x == 0) {
                      vmin[y] = Math.min(y + r1, hm) * w;
                  }
                  p = x + vmin[y];
                  sir[0] = r[p];
                  sir[1] = g[p];
                  sir[2] = b[p];
                  rinsum += sir[0];
                  ginsum += sir[1];
                  binsum += sir[2];
                  rsum += rinsum;
                  gsum += ginsum;
                  bsum += binsum;
                  stackpointer = (stackpointer + 1) % div;
                  sir = stack[stackpointer];
                  routsum += sir[0];
                  goutsum += sir[1];
                  boutsum += sir[2];
                  rinsum -= sir[0];
                  ginsum -= sir[1];
                  binsum -= sir[2];
                  yi += w;
              }
          }
          Log.e("pix", w + " " + h + " " + pix.length);
          bitmap.setPixels(pix, 0, w, 0, 0, w, h);
          return (bitmap);
      }
  }
  </code></pre>
  
  <p>screen shot - this is the result i get- i want to make alert center but it stays on top
  <a href="https://i.stack.imgur.com/sc0ZE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sc0ZE.png" alt="this is the result i get- i want to make it center"></a></p>
  
  <p><strong>Edit : Answer's Concept</strong></p>
  
  <ol>
  <li>Take a screen shot</li>
  <li>Programmatically animate/blur/whatever effect to that screen shot</li>
  <li>Get currant window using a dialog witch does not have any content</li>
  <li>Attach screen shot with effect </li>
  <li>Display real view I wanted to display</li>
  </ol>
  
  <hr>
  
  <p><strong>Side Note:</strong> This can be used to attach any type of a background! blur.. dim..bright.. Just add the right method that accept a bitmap and return a bitmap with changes you want in <code>BlurView</code> class. I added a blur one.</p>