title: Integrate Unity3d view into Android activity
body: <p>I'm currently working on a small AR app for Android and am facing the problem of integrating Unity3d into an activity. The requirements indicate that I need to be able to present some Android UI - e.g. menus and an action bar - and a camera view that will display a model created in Unity3d when the target is detected.</p>
  
  <p>I found a link that helped me a lot: <a href="http://forum.unity3d.com/threads/98315-Using-Unity-Android-In-a-Sub-View/page4?p=1477830&amp;viewfull=1#post1477830">Unity3d forums</a>. One of the users there asked the same question I have now but never got any proper answer -that's why I'm posting here.</p>
  
  <p><strong>Problem:</strong> </p>
  
  <p>I got a small Unity3d project that is essentially a white cube and am trying to display it in one of my Android activities. The model looks fine when activity doesn't have <code>setContentView()</code> in its <code>onCreate()</code> method but then I can't specify my layout in an XML file.</p>
  
  <p>When I do add the <code>setContentView()</code> method, I can see the cube but it's very small and there doesn't seem to be any way of actually making it change its size.</p>
  
  <p><strong>The XML file:</strong></p>
  
  <pre><code>&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/unityView"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" &gt;
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p><strong>1st version of the activity implementation:</strong></p>
  
  <pre><code>    public class HomeActivity extends UnityPlayerActivity {
  
          UnityPlayer unityPlayer;
  
          @Override
          protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          }
      }
  </code></pre>
  
  <p>And the resulting screenshot: <img src="https://i.stack.imgur.com/hw8zF.png" alt="enter image description here"></p>
  
  <p><strong>2nd version of the activity implementation:</strong></p>
  
  <pre><code>public class HomeActivity extends UnityPlayerActivity {
  
          UnityPlayer unityPlayer;
  
          @Override
          protected void onCreate(Bundle savedInstanceState) {
  
              super.onCreate(savedInstanceState);
  
              setContentView(R.layout.activity_home);
              unityPlayer = new UnityPlayer(this);
              int glesMode = unityPlayer.getSettings().getInt("gles_mode", 1);
              unityPlayer.init(glesMode, false);
  
              FrameLayout layout = (FrameLayout) findViewById(R.id.unityView);
              LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
              layout.addView(unityPlayer.getView(), 0, lp);
          }
  
      @Override
      public void onWindowFocusChanged(boolean hasFocus) {
  
          super.onWindowFocusChanged(hasFocus);
          unityPlayer.windowFocusChanged(hasFocus);
  
      }
  }
  </code></pre>
  
  <p>And the resulting screenshot: </p>
  
  <p><img src="https://i.stack.imgur.com/4NMQP.png" alt="enter image description here"></p>
  
  <p>Could anyone explain to me why that is and how to fix it?</p>