title: Android: AlertDialog with custom view and rounded corners
body: <p>I'm trying to build an <code>AlertDialog</code> with a custom <code>view</code> (no title or footer) and rounded corners. I've seen a lot of posts about how to do that and I tried a lot of things, but I can't build it like I want to.</p>
  
  <p>This is my goal:</p>
  
  <p><img src="https://i.stack.imgur.com/myv0s.png" alt="enter image description here"></p>
  
  <p>I created a <code>drawable</code> for the <code>dialog</code> called dialog_background.xml:</p>
  
  <pre><code>&lt;shape xmlns:android="http://schemas.android.com/apk/res/android" &gt;
  
      &lt;solid
          android:color="#FFAAAAAA" /&gt;
  
      &lt;stroke
          android:width="2dp"
          android:color="#FF000000" /&gt;
  
      &lt;corners android:radius="20dp" /&gt;
  &lt;/shape&gt;
  </code></pre>
  
  <p>And I added a <code>style</code> to use it:</p>
  
  <pre><code>&lt;style name="MyDialog" parent="@android:style/Theme.Dialog"&gt;
      &lt;item name="android:background"&gt;@drawable/dialog_background&lt;/item&gt;
  &lt;/style&gt;
  </code></pre>
  
  <p>The <code>layout</code> of my custom <code>view</code> is gonna have two buttons. Right now I show you an empty <code>LinearLayout</code> to make it simple. This is playdialog.xml:</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      style="@style/MyDialog"
       &gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>To build the <code>Dialog</code> I use a <code>DialogFragment</code>. This is its <code>onCreateDialog</code> function:</p>
  
  <pre><code>public Dialog onCreateDialog(Bundle savedInstanceState) {
      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
      LayoutInflater inflater = getActivity().getLayoutInflater();
  
      builder.setView(inflater.inflate(R.layout.playdialog, null));
      return builder.create();
  }
  </code></pre>
  
  <p>Ok, if I use the code like that I get this:</p>
  
  <p><img src="https://i.stack.imgur.com/rUs90.png" alt="enter image description here"></p>
  
  <p>I tried to set the <code>dialog</code> background to transparent modifying my <code>DialogFragment</code> code:</p>
  
  <pre><code>public Dialog onCreateDialog(Bundle savedInstanceState) {
      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
      LayoutInflater inflater = getActivity().getLayoutInflater();
  
      builder.setView(inflater.inflate(R.layout.playdialog, null));
  
      **NEW**        
  
      Dialog d = builder.create();
      d.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
      return d; 
  }
  </code></pre>
  
  <p>The result is exactly the same, so then I realized that that white rectangle under my <code>dialog</code> <strong>is from my custom <code>view</code>, not from the <code>dialog</code></strong>. I'm already setting my <code>view</code>'s background to dialog_background.xml, so I can't set it to transparent too or I loose the corners, color, etc. </p>
  
  <p>Then I decided to modify the <code>dialog</code>'s background using dialog_background.xml and have my view have a solid color as background. </p>
  
  <p>In my custom <code>view</code> <code>layout</code> (playdialog.xml) I replaced style="@style/MyDialog" with:</p>
  
  <pre><code>android:background="#FFAAAAAA"
  </code></pre>
  
  <p>And then in my <code>DialogFragment</code> I used this code:</p>
  
  <pre><code>public Dialog onCreateDialog(Bundle savedInstanceState) {
      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
      LayoutInflater inflater = getActivity().getLayoutInflater();
  
      builder.setView(inflater.inflate(R.layout.playdialog, null));
  
      **NEW**        
  
      Dialog d = builder.create();
      d.getWindow().setBackgroundDrawableResource(R.drawable.dialog_background);
      return d; 
  }
  </code></pre>
  
  <p>This is what I get:</p>
  
  <p><img src="https://i.stack.imgur.com/TZtEi.png" alt="enter image description here"></p>
  
  <p>It's almost what I wanted, but you can see my custom <code>view</code> borders, so it's not good enough. At this point I didn't know what else could I do.</p>
  
  <p>Anyone knows how can I solve it?</p>
  
  <p>Thanks!</p>