How to load a specific layout of an activity in android


Keywords:android 


Question: 

I am working on an augment reality android app. I developed the app in unity using vuforia and then imported it in android studio as i wanted to design it in android studio. The activity name in generated project structure is UnityPlayerActivity which has no layout in the project. The activity is passed to UnityPlayerclass and the layout is generated. This is some code of my UnityPlayerActivity activity.

protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code

// Setup activity layout
@Override protected void onCreate(Bundle savedInstanceState)
{
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);

    getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia play happy

    mUnityPlayer = new UnityPlayer(this);
    setContentView(mUnityPlayer);
    mUnityPlayer.requestFocus();
}

as it uses that layout how can i design that activity by referencing it.

Sample screenshot of that app


2 Answers: 

It is not possible to do this by referencing the object(Java does not support call by reference).

You need to define a new function in UnityPlayer class that generates a layout and returns the LayoutParams object.

Then use the Layout inflator. Something like this:

LayoutParams mParams=mUnityPlayer.layoutGenerator() //some function to generate LayoutParams
LayoutInflator.getLayoutInflator().inflate(LayoutParams);
 

Finally i found an easy solution to my problem. I displayed the unity scenes in a subview using a framelayout and now i can customize the UI easily. Check here and here to see more information.