Question

Is there a way to set the content of a NativeActivity to a component created in Java (such as a FrameLayout, ImageView, etc)? I have a need to use a NativeActivity (for getting touchpad input on Xperia Play), but I also need to be able to view components created in the Java code.

I've tried overriding the onCreate command and calling setContentView there, but although that doesn't throw any errors, it doesn't do anything either (apparently because the content view is already set by the native code). Is there some sort of window id or something that I could pass to the native code to tell it what the content view should be, or can this somehow be set up in the XML to set the NativeActivity's content view to a component that I can modify from Java?

Let me know if you need any more information.

Was it helpful?

Solution

I'll post the answer here for others with the same problem, since I did eventually figure out a method that works.

In the onCreate() method, if you are setting any particular window flags (FEATURE_NO_TITLE, FLAG_FULLSCREEN, FLAG_KEEP_SCREEN_ON, etc) do those before you call super.onCreate() (or they'll be ignored). Then wherever you would normally put this:

setContentView( whatever );

Do this instead:

getWindow().takeSurface( null );
getWindow().setContentView( whatever );

That's the basic way to get the content to be controlled from the Java side. Any place in your code where you deal with the content, use "getWindow()" instead of "this".

Some other things to keep in mind, are that the normal onKey and onTouch methods will not be called (their native equivalents will be instead), so if you need to be able to process input on the Java side, you'll have to set up some JNI linkage to send the information from native to Java. I believe everything else is included in the official XPeria Play touchpad sample code (the additions to the AndroidManifest.xml and what-not).

If you want to look at my project for reference, it is open-source and can be found at:

http://www.paulscode.com/forum/index.php?topic=75.msg1540#msg1540

Just click the "source code" link next to "XPeria Play Touch-Pad Example". It may not be all that useful to you though, because it is a rather large project and may be difficult to find what you are looking for. If you have any problems, post a question on my forum there or email me, and I'll be happy to help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top