title: Make dialog width the same as custom positive button width
body: <p>I am using a custom layout for a dialog like this:</p>
  
  <pre><code>public Dialog onCreateDialog(Bundle savedInstanceState) {
  
      Builder builder = new Builder(getActivity());
      View dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_code_dialog, null);
      builder.setView(dialogView);
  
      builder.setPositiveButton("", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int id) {
              listener.onDialogPositiveClick(CodeDialogFragment.this);
          }
      });
      return builder.create();
  }
  </code></pre>
  
  <p>With this xml:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  tools:context="XXX.fragments.CodeDialogFragment"&gt;
  
      &lt;TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
      android:text="@string/code_text"
      android:padding="@dimen/medium_body_padding"
      android:id="@+id/code_text"
      /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>I am replacing the standard positive button with my styled one by overriding the onStart() method on the dialog fragment:</p>
  
  <pre><code>@Override
  public void onStart(){
      super.onStart();
      //replace default button with image
  
      Button button = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
      button.setBackground(ContextCompat.getDrawable(this.getActivity(), R.drawable.copy_and_open_btn));
  }
  </code></pre>
  
  <p>My problem is that my deisgn requires the dialog to be the same width as the button, but as the button is being overridden setting the width to <code>wrap_content</code> isn't working. How can I change the width of the dialog to match the button width after the button has been replaced?</p>
  
  <p>Update:</p>
  
  <p>I have managed to change the width of the dialog by adding this to the <code>onStart()</code> method:</p>
  
  <pre><code>    int width = button.getBackground().getIntrinsicWidth();
      WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
      lp.copyFrom(getDialog().getWindow().getAttributes());
      lp.width = width;
      getDialog().getWindow().setAttributes(lp);
  </code></pre>
  
  <p>This changes the width of the dialog, but shrinks the button to retain the padding. I can't work out where that padding is coming from though.</p>