How to set content view of NativeActivity to component created in Java
Asked Answered
P

1

7

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.

Priming answered 19/10, 2011 at 0:44 Comment(1)
Did you resolve this issue, I am trying to do a similar thing. The documentation provided by Sony is terrible!Hermilahermina
P
12

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.

Priming answered 27/11, 2011 at 23:12 Comment(3)
Interesting approach. I managed to use my own layout within a NativeActivity. I still need to display what is render by a C++ library I am using (in this case Cocos2d-x 3.x). Is there a way to embed a SurfaceView or similar and bind it to the C++ code?Commonwealth
@AmitHooda the link is fine, but your IP may be blocked (my host has strict anti-spam measures in place)Priming
@Priming Please help me I am trying to do similar thingZenia

© 2022 - 2024 — McMap. All rights reserved.