title: How to give a fragment the appearance of a dialog box?
body: <p>I have an Android game that's currently on the market and am in the process if implementing a turn-based multiplayer mode.  Unfortunately a large amount of unique activities are used within the game and Google Play Game Services require the game to work in a single activity using fragments.  For this reason I'm converting the project over to such a system (with android-support-v4).</p>
  
  <p>I've written an abstraction layer which has helped the process and, as the game is already thoroughly tested in its current state, am hoping to avoid a major rewrite to change it from multiple activities to multiple fragments.  The game is working properly but I am having difficulties with the appearance of some of the fragments.</p>
  
  <p>The main problem I'm facing is with the display of activities that inherited from <strong>android:Theme.Dialog</strong>.  I have a number of activities that display in floating dialog-style boxes but these are activities and not dialogs.  In the original code I had the following defined within styles.xml -</p>
  
  <pre><code>&lt;style name="MyDialogStyle" parent="android:Theme.Dialog"&gt;
      &lt;item name="android:windowNoTitle"&gt;true&lt;/item&gt;
      &lt;item name="android:windowBackground"&gt;@drawable/my_border_graphic&lt;/item&gt;
      &lt;item name="android:textSize"&gt;18sp&lt;/item&gt;
      &lt;item name="android:windowAnimationStyle"&gt;@style/MyDialogAnimations&lt;/item&gt;
  &lt;/style&gt;
  </code></pre>
  
  <p>The activities that used this layout were defined in the manifest as -</p>
  
  <pre><code>&lt;activity
      android:name="com.package.name.myactivity"
      android:configChanges="keyboardHidden|orientation|screenSize"
      android:screenOrientation="landscape"
      android:theme="@style/MyDialogStyle" &gt;
  &lt;/activity&gt;
  </code></pre>
  
  <p>Starting this activity results in a dialog style display exactly as would be expected from the style xml.  So far I have been unable to recreate this style for any of the fragments in my new code.  I have tried to use the style as follows -</p>
  
  <pre><code>@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.MyDialogStyle);
      LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
      final View v = localInflater.inflate(R.layout.my_layout, container, false);
      ...
  </code></pre>
  
  <p>But the resulting fragment appears in the top left of the screen.  Also, if it contains text that is very long, the fragment version uses the full screen width whereas the activity one wraps it in shortly lines.</p>
  
  <p>Obviously I can play around with gravity settings to display things more centrally, but I don't see why I'm unable to use the style to control these factors as I did with an Activity.</p>
  
  <p>Is it possible to display the fragment in a dialog style floating box?  If so, what changes do I need to make?  I am aware I can achieve the desired appearance by using a DialogFragment, calling setStyle in the onCreate and displaying it using show(...) but I want to keep it as a standard Fragment so it uses the same (or more similar) lifestyle to the previous activity method.</p>
  
  <p><em>[I've now realised I'm not going to get the same onPause/onResume lifecycle events in the dialog's hosting fragment - the dialog fragments are added rather than replacing the current fragment so they're not going to be called]</em></p>
  
  <p>Here's a small testbed I set up for playing around with the fragment code -</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends FragmentActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          getSupportFragmentManager().beginTransaction().add(R.id.container, new TestFragment()).addToBackStack(null).commit();
      }
  
      public static class TestFragment extends DialogFragment {
  
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
              final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.MyDialog);
              LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
              final View v = localInflater.inflate(R.layout.test_dialog, container, false);
              return v;
          }
      }
  }
  </code></pre>
  
  <p><strong>activity_main.xml</strong></p>
  
  <pre><code>&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/container"
      android:layout_width="match_parent"
      android:layout_height="match_parent" &gt;
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p><strong>test_dialog.xml</strong></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" &gt;
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST! TEST!" /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>styles.xml</strong></p>
  
  <pre><code>&lt;resources&gt;
      &lt;style name="MyDialog" parent="@android:style/Theme.Dialog"&gt;
          &lt;item name="android:windowNoTitle"&gt;true&lt;/item&gt;
      &lt;/style&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>And this is the resultant output (looking nothing like a dialog) -</p>
  
  <p><img src="https://i.stack.imgur.com/E9CaC.png" alt="Screenshot"></p>
  
  <p>Any suggestions would be very much appreciated.</p>