code: private int getLayoutDimension(final int dimension, final int margin, final int total) {
          if (dimension == Dialog.MATCH_PARENT) {
              return RelativeLayout.LayoutParams.MATCH_PARENT;
          } else if (dimension == Dialog.WRAP_CONTENT) {
              return RelativeLayout.LayoutParams.WRAP_CONTENT;
          } else {
              return Math.min(dimension, total - margin);
          }
      }
docs: Returns a dimension (width or height) of the dialog, depending of the margin of the
  corresponding orientation and the display space, which is available in total.
  
  @param dimension
  The dimension, which should be used, in pixels as an {@link Integer value}
  @param margin
  The margin in pixels as an {@link Integer} value
  @param total
  The display space, which is available in total, in pixels as an {@link Integer}
  value
  @return The dimension of the dialog as an {@link Integer} value