title: Android fragment show as dialogfragment or usual fragment
body: <p>What I am trying to achieve is to have a fragment that on tablet it shows as a <code>DialogFragment</code>, while on smartphone it would be shown as a regular fragment. I am aware there is already a <a href="https://stackoverflow.com/questions/7593415/show-fragment-as-a-dialog-or-as-a-usual-activity">similar post</a>, but I am not able to make that work - apply the style to fragment.</p>
  
  <p>To show things top down, MainActivity.java:</p>
  
  <pre><code>public class MainActivity extends ActionBarActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          if (item.getItemId() == R.id.action_next) {
              decideToNext();
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
      private void decideToNext() {
          String device = getString(R.string.device);
          if ("normal".equalsIgnoreCase(device)) {
              Intent intent = new Intent(this, DetailedActivity.class);
              startActivity(intent);
          } else if ("large".equalsIgnoreCase(device)) {
              Log.d("SOME_TAG", "Yes, I am seeing this line on tablet only");
              DetailedFragment fragment = DetailedFragment.newInstance();
              getSupportFragmentManager().beginTransaction().add(fragment, "MAGIC_TAG").commit();
          }
      }
  
  }
  </code></pre>
  
  <p>DetailedActivity is nothing much:</p>
  
  <pre><code>public class DetailedActivity extends ActionBarActivity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.detailed_activity);
      }
  }
  </code></pre>
  
  <p>its layout:</p>
  
  <pre><code>&lt;fragment xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/root_container"
      android:name="com.myapps.sampleandroid.DetailedFragment"
      android:layout_width="match_parent"
      android:layout_height="match_parent" /&gt;
  </code></pre>
  
  <p>and the interesting <code>DetailedFragment</code>:</p>
  
  <pre><code>public class DetailedFragment extends Fragment {
  
      public static DetailedFragment newInstance() {
          return new DetailedFragment();
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.MyDialogTheme);
          LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
          return localInflater.inflate(R.layout.detailed_fragment, container, false);
      }
  }
  </code></pre>
  
  <p>... and its layout:</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" &gt;
  
      &lt;TextView
          android:id="@+id/textView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Regular Text" /&gt;
  
      &lt;TextView
          android:id="@+id/textView2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Large Text"
          android:textAppearance="?android:attr/textAppearanceLarge" /&gt;
  
      &lt;Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Button Dummy" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>In <code>onCreateView</code> I've tried to set the custom styling but it doesn't seem to work for tablet.</p>
  
  <p><strong>Styling</strong></p>
  
  <p>res/values/styles.xml contains:</p>
  
  <pre><code>&lt;resources&gt;
  
      &lt;style name="AppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar"&gt;
      &lt;/style&gt;
  
      &lt;style name="MyDialogTheme" /&gt;
  
  &lt;/resources&gt;
  </code></pre>
  
  <p>while res/values-large/styles.xml:</p>
  
  <pre><code>&lt;resources&gt;
      &lt;!-- Is there anything I should add here? --&gt;
      &lt;style name="MyDialogTheme" parent="@android:style/Theme.Dialog"/&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>I've nested <code>MyDialogTheme</code> from <code>Theme.Dialog</code>, but it doesn't seem to help.</p>
  
  <p>On smartphone when tapping on "NEXT" action bar menu item I am seeing the detailed activity (snapshot from an SG2):
  <img src="https://i.stack.imgur.com/1U1v9.png" alt="snapshot from an SG2"></p>
  
  <p>while tapping on the same "NEXT" menu item from the tablet it doesn't do anything (except viewing the message on Logcat: <code>Yes, I am seeing this line</code>).
  <img src="https://i.stack.imgur.com/uIrxD.png" alt="enter image description here"></p>
  
  <p>What should I add more in styles.xml or in code in order to see <code>DetailedFragment</code> as a dialog for tablet?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I've tried the solution <em>Little Child</em> proposed (to have a <code>DialogFragment</code> contain my initial fragment and show it). So, I've added a <code>WrapperDetailedFragment</code>:</p>
  
  <pre><code>public class WraperDetailedFragment extends DialogFragment {
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          return inflater.inflate(R.layout.wrap_detailed_fragment, container, false);
      }
  }
  </code></pre>
  
  <p>its layout:</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/root_container_dialog"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" &gt;
  
      &lt;fragment
          android:id="@+id/wrapped_fragment_id"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          class="com.myapps.sampleandroid.DetailedFragment" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>The code from MainActivity changes to:</p>
  
  <pre><code>private void decideToNext() {
      String device = getString(R.string.device);
      if ("normal".equalsIgnoreCase(device)) {
          Intent intent = new Intent(this, DetailedActivity.class);
          startActivity(intent);
      } else if ("large".equalsIgnoreCase(device)) {
          Log.d("SOME_TAG", "Yes, I am seeing this line ...");
          WraperDetailedFragment fragment = new WraperDetailedFragment();
          fragment.show(getSupportFragmentManager(), "MAGICAL_TAG");
      }
  }
  </code></pre>
  
  <p>but when I'm trying to add this DialogFragment I am getting the following crash:</p>
  
  <pre><code>android.view.InflateException: Binary XML file line #8: Error inflating class fragment
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
      at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
      at com.myapps.sampleandroid.WraperDetailedFragment.onCreateView(WraperDetailedFragment.java:12)
      at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
      at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:304)
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
      at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
      at com.myapps.sampleandroid.WraperDetailedFragment.onCreateView(WraperDetailedFragment.java:12)
      at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
      at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
      at android.os.Handler.handleCallback(Handler.java:615)
      at android.os.Handler.dispatchMessage(Handler.java:92)
      at android.os.Looper.loop(Looper.java:137)
      at android.app.ActivityThread.main(ActivityThread.java:4745)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:511)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
      at dalvik.system.NativeStart.main(Native Method)
  Caused by: java.lang.IllegalArgumentException: Binary XML file line #8: Duplicate id 0x7f050047, tag null, or parent id 0x0 with another fragment for com.myapps.sampleandroid.DetailedFragment
      at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:290)
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
      ... 28 more
  </code></pre>