title: Creating a custom dialog in Android
body: <p>I am trying to create a custom dialog in Android. But whatever I tried to do, I am not able to change the width of the dialog. It just remains the same. The following is the view that I am setting as the content for the dialog. </p>
  
  <pre><code>&lt;RelativeLayout  
  android:id="@+id/current_stats"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:visibility="visible"&gt;
  &lt;ImageView android:id="@+id/player_image" 
      android:src="@drawable/person"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"/&gt;
  &lt;TextView   
      android:id="@+id/player_name"
      android:layout_below="@id/player_image"
      android:layout_centerHorizontal="true"
      android:text="Raja Ram"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/&gt;
  &lt;ImageButton android:id="@+id/dialog_close"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_alignParentRight="true"
      android:background="#00000000"
      android:src="@drawable/close_button_selector"/&gt;
  &lt;ImageButton android:id="@+id/dialog_flip"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_alignParentLeft="true"
      android:background="#00000000"
      android:src="@drawable/rotate"/&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>As you can see, everywhere I am using wrap_content in this code sample. Also I tried the following options</p>
  
  <p>1) Setting a custom style while creating the Dialog like this. </p>
  
  <pre><code>dialog = new Dialog(this,R.style.Theme_Dialog);
  </code></pre>
  
  <p>And the style as follows</p>
  
  <pre><code>&lt;resources&gt;
  &lt;style name="Theme" parent="android:Theme"&gt;
  &lt;/style&gt;
  &lt;style name="Theme.Dialog"&gt;
      &lt;item name="android:layout_width"&gt;wrap_content&lt;/item&gt;
      &lt;item name="android:layout_height"&gt;wrap_content&lt;/item&gt;
      &lt;item name="android:windowIsFloating"&gt;true&lt;/item&gt;
      &lt;item name="android:windowContentOverlay"&gt;@null&lt;/item&gt;
      &lt;item name="android:windowAnimationStyle"&gt;@android:style/Animation.Dialog&lt;/item&gt;
      &lt;item name="android:windowSoftInputMode"&gt;stateUnspecified|adjustPan&lt;/item&gt;
  &lt;/style&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>2) Setting the parameters for the view in the onCreateDialog() like this</p>
  
  <pre><code>LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  v = inflater.inflate(R.layout.player_info, null, false);
  ViewGroup.LayoutParams p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
  dialog.setContentView(v,p);
  </code></pre>
  
  <p>3) I also tried to set the Window parameters like this in the onCreateDialog() method</p>
  
  <pre><code>WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
  params.width=WindowManager.LayoutParams.WRAP_CONTENT;
  dialog.getWindow().setAttributes(params);
  </code></pre>
  
  <p>But again no luck. Can someone help me out with this issue. Am I doing something wrong?? Also can you please suggest me how to set the x and y postions for the Dialog window?</p>
  
  <p>Thanks</p>