title: Remove Black background from Alert Dialog Box - Android
body: <p>I am working on an Android Application , Here I have created a custom dialog box , The dialog box was appearing on complete height of the device , but I wanted to leave margins from top and bottom , so I set margin using below code </p>
  
  <pre><code>  WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
      lp.copyFrom(alertDialog.getWindow().getAttributes());
      lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
      lp.height = Utility.convertDPtoPixel(400, screen);
  
      alertDialog.show();
      alertDialog.getWindow().setAttributes(lp);
  </code></pre>
  
  <p>Above code worked perfectly, but the dialog appears with black surroundings.</p>
  
  <p>Here is the screen shot:</p>
  
  <p><a href="https://i.stack.imgur.com/Al9xQ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Al9xQ.png" alt="Screen shot with black surroundings"></a> </p>
  
  <p>Now I want to remove these black surroundings from the dialog box.</p>
  
  <p>Here is the code of dialog box:</p>
  
  <pre><code>public void showDialog() {
      AlertDialog.Builder builder;
      final AlertDialog alertDialog;
      //final Dialog alertDialog;
      Context mContext;
      mContext = screen;
      LayoutInflater inflater = (LayoutInflater) LoginActivity.screen.getSystemService(LoginActivity.screen.LAYOUT_INFLATER_SERVICE);
      layout = inflater.inflate(R.layout.countrylist, null);
      LinearLayout rellayout = (LinearLayout) layout.findViewById(R.id.rellayout);
  
      RelativeLayout closeAcessCodeDialog = (RelativeLayout)layout.findViewById(R.id.closeAcessCodeDialog) ;
  
      ListView listview = (ListView) layout.findViewById(R.id.listview);
      listview.setAdapter(new CustomAdapter1(LoginActivity.screen, countryList));
  
      builder = new AlertDialog.Builder(LoginActivity.screen);
      builder.setView(layout);
      alertDialog = builder.create();
      alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
  
      WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
      lp.copyFrom(alertDialog.getWindow().getAttributes());
      lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
      lp.height = Utility.convertDPtoPixel(400, screen);
  
      alertDialog.show();
      alertDialog.getWindow().setAttributes(lp);
  
  
      listview.setOnItemClickListener(new OnItemClickListener() {
  
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view, int pos,
                                  long id) {
              countryName = idCountry.get(pos);
              System.out.println("Country code is: " + countryName);
              alertDialog.cancel();
              text.setText(countryList.get(pos));
          }
      });
  
      closeAcessCodeDialog.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View view) {
              alertDialog.dismiss();
          }
      });
  
  }
  </code></pre>
  
  <p><strong>countrylist.xml</strong></p>
  
  <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:cardBackgroundColor="#ffe264"
      app:cardCornerRadius="10dp"
      &gt;
  
      &lt;LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/rellayout"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          &gt;
  
          &lt;RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="60dp"
              android:layout_above="@+id/listview"
              android:gravity="center_vertical"
              &gt;
  
              &lt;TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Select your Country"
                  android:textColor="#000"
                  android:textSize="18sp"
                  android:textStyle="bold"
                  android:layout_centerInParent="true"
                  /&gt;
  
              &lt;RelativeLayout
                  android:id="@+id/closeAcessCodeDialog"
                  android:layout_width="60dp"
                  android:layout_height="60dp"
                  android:layout_alignParentRight="true"
                  android:gravity="center"&gt;
  
                  &lt;ImageButton
                      android:id="@+id/closeDialog"
                      android:layout_width="30dp"
                      android:layout_height="30dp"
                      android:background="@drawable/close_country_dialog" /&gt;
  
              &lt;/RelativeLayout&gt;
          &lt;/RelativeLayout&gt;
  
  
          &lt;ListView
              android:id="@+id/listview"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:cacheColorHint="@android:color/transparent"
              android:divider="#e5e5e5"
              android:background="@drawable/list_bacground"
              android:dividerHeight="1px" /&gt;
  
      &lt;/LinearLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p>I visited many similar links over stackoverflow , but no one was helpfull .</p>