detect changed orientation Android

42 views
Skip to first unread message

avazqu...@gmail.com

unread,
Aug 28, 2017, 11:54:04 AM8/28/17
to PlayN
Hi All!!
I am testing the changed orientation in a simple application. At the moment I'm doing the tests on android but I'm also going to do it on iOS.

My application has a button that executes the orientation change. So far everything is correct the orientation change works but new view does not work correctly. I think this is because it keeps the previous window size and so it does not look right.

I can detect when the orientation changed
but I do not know if I can do something to configure the new screen size.


@Override
   
public void onConfigurationChanged(Configuration newConfig)
   
{
       
super.onConfigurationChanged(newConfig);
        platform
().log().info("onConfigurationChanged");
       
       
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            platform
().log().info("landscape");
       
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            platform
().log().info("portrait");
       
}
   
}

Is there any way to fix this?

Thanks!
Regards!

Screenshot_2017-08-28-17-35-32.png
Screenshot_2017-08-28-17-35-22.png

Michael Bayne

unread,
Aug 28, 2017, 1:05:53 PM8/28/17
to pl...@googlegroups.com
PlayN should do the right thing when orientation changes. If you run the test application in playn/tests on an Android device, it properly handles an orientation change.

I don't know what you're doing to initiate an orientation change on a "button press". That seems weird. I initiate an orientation change via the emulator because the orientation of the device is something managed by the OS, not your app. So you should not have to trigger it with an in-app button press. Just use the button on the emulator that changes orientation.


--

---
You received this message because you are subscribed to the Google Groups "PlayN" group.
To unsubscribe from this group and stop receiving emails from it, send an email to playn+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Michael Bayne

unread,
Aug 28, 2017, 1:12:07 PM8/28/17
to pl...@googlegroups.com
Oh, probably the issue is that you're using TriplePlay UI and you need to tell it that the size of the screen changed, because it doesn't currently automatically detect that. I haven't wired up any sort of signal to communicate in a platform independent way that the screen size has changed.

The easiest thing would probably be for me to just add that, and if you're using PlayN and TriplePlay 2.1-SNAPSHOT, things will just start working. But if you want to stick to 2.0, then you can add code in your onConfigurationChanged to call a method in your app to let it know that the screen size changed, and then call setSize() with the new screen size on your Root UI element.


On Mon, Aug 28, 2017 at 8:54 AM, <avazqu...@gmail.com> wrote:

--

---
You received this message because you are subscribed to the Google Groups "PlayN" group.
To unsubscribe from this group and stop receiving emails from it, send an email to playn+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Bayne

unread,
Aug 28, 2017, 3:08:23 PM8/28/17
to pl...@googlegroups.com
This assumes you're using PlayN & TP 2.1-SNAPSHOT.

avazqu...@gmail.com

unread,
Aug 28, 2017, 4:17:28 PM8/28/17
to PlayN
Yes, you are right. I am using TriplePlay UI and playN (both maven repository (2.0)).
I'll do what you recommend. I will add code in the onConfigurationChanged  method.

The accion "button press" call the method 
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
to changed the orientation programmatically.

I have to check if only with PlayN the orientation change works because I think it also gave me the same error. I'll confirm it tomorrow.

Thanks!
To unsubscribe from this group and stop receiving emails from it, send an email to playn+un...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

avazqu...@gmail.com

unread,
Aug 29, 2017, 4:47:06 AM8/29/17
to PlayN

I changed the example and now when I hit the button I changed the orientation and opened a second view. 
The orientation changes correctly but I have the problem that the values are not updated until after the second view has been drawn.

The second view simply draws a background.

public ViewTwo(Platform plat)
{
super(plat, 33); // update our "simulation" 33ms (30 times per second)
plat.log().info("ViewTwo");
plat.log().info("View Size" + plat.graphics().viewSize.toString());
// create and add background image layer
Image bgImage = plat.assets().getImage("images/bg.png");
ImageLayer bgLayer = new ImageLayer(bgImage);
// scale the background to fill the screen
bgLayer.setSize(plat.graphics().viewSize);
rootLayer.add(bgLayer);
}

The log:

08-29 10:22:40.512 15219 15238 I playn   : ViewOne
---Push Button-----
08-29 10:22:42.999 15219 15219 I playn   : onConfigurationChanged
08-29 10:22:43.000 15219 15219 I playn   : Display is 598x360
08-29 10:22:43.000 15219 15219 I playn   : landscape
08-29 10:22:43.067 15219 15238 I playn   : ViewTwo
08-29 10:22:43.067 15219 15238 I playn   : View Size360.0x604.0
08-29 10:22:43.143 15219 15238 I playn   : viewPortChanged 1196x720 / 2.0 -> 598.0x360.0

As you can see in the log, the background is drawing it with the previous size.
Is there any way to fix this?

Thanks!

Michael Bayne

unread,
Aug 29, 2017, 11:02:42 AM8/29/17
to pl...@googlegroups.com
You can't just immediately react to the orientation change in the onConfigurationChanged method because PlayN hasn't been told by Android at that point about the new screen size. It happens later via a GLSurfaceView callback. When you see the log:

"viewPortChanged 1196x720 / 2.0 -> 598.0x360.0"

That's when PlayN is executing the GLSurfaceView callback telling it about the new screen size.

The new code I added sends orientation change notifications *after* that callback so it is safe to react to orientation notifications if you're listening for changes on Graphics.deviceOrient.

If you are handling onConfigurationChanged manually, you probably have to wait one frame before doing anything in response to the change to give GLSurfaceView a chance to dispatch its callback.

To unsubscribe from this group and stop receiving emails from it, send an email to playn+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

avazqu...@gmail.com

unread,
Aug 29, 2017, 1:00:24 PM8/29/17
to PlayN
If you are handling onConfigurationChanged manually, you probably have to wait one frame before doing anything in response to the change to give GLSurfaceView a chance to dispatch its callback.

OK I understand. I have implemented a timeout after I detect the size change but it does not work. How can I implement the wait until after the first frame and run the new view?

I have reviewed the iOs part to do the same thing but I see that the orientation is configured together with the platform.
Is there any method I can call to change the orientation the same way I'm doing with Android?

Thanks!  

Michael Bayne

unread,
Aug 29, 2017, 1:02:10 PM8/29/17
to pl...@googlegroups.com
Like I wrote before:
To unsubscribe from this group and stop receiving emails from it, send an email to playn+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Michael Bayne

unread,
Aug 29, 2017, 1:04:33 PM8/29/17
to pl...@googlegroups.com
As for forcing an orientation change on iOS, I have no idea. You can look to see if there are iOS APIs to do that and those will likely be wrapped by RoboVM, but they may not give the app control over that. The orientation of the device is a physical property of the device. It is either oriented one way or another. It doesn't make a lot of sense to allow the application to say "switch to this orientation" when the device is not in fact in that orientation.

On Tue, Aug 29, 2017 at 10:00 AM, <avazqu...@gmail.com> wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to playn+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

avazqu...@gmail.com

unread,
Aug 29, 2017, 2:55:47 PM8/29/17
to PlayN
Surely I'm wrong and it isn't done that way. 
Basically what I want is to be able to choose and fix a particular view (landscape or portrait) according to the needs that I have. Perhaps for this it isn't necessary to force a certain orientation ...
Is there any way to do what I am proposing?

Michael Bayne

unread,
Aug 29, 2017, 3:09:11 PM8/29/17
to pl...@googlegroups.com
The "right" thing to do would probably to create an app that only claims to ever run in portrait orientation, so the OS will never reorient it, and then to apply your own transform to the top-level scene graph node to rotate the screen based on when your app wants to be in one mode or another.

This is going to require some extra work on your part because this is not how PlayN or TriplePlay expects things to work, so you're going to be swimming upstream. But it will probably be the path of least surprises.


To unsubscribe from this group and stop receiving emails from it, send an email to playn+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

avazqu...@gmail.com

unread,
Aug 30, 2017, 4:15:44 PM8/30/17
to PlayN
OK here is some more detail of what I really want to achieve
The main window in my application will show a list of "elements". You can think of these as game levels.
I want some of the levels to run in portrait mode only, and some others in landscape mode only
My first idea was to programmatically lock the orientation but this has the problems you have described already
As you suggest I can force a 90º rotation manually but I would like to avoid this if possible

What about the following approach (Android only at the moment but I guess there would be something similar on iOS)
- Use three separate activities: 1/ One for the main list elements (game levels), 2/ one for "portrait mode" levels, and 3/ one for "landscape mode" levels
- Setup activities 2 and 3 so that they are always in portrait and landscape modes respectively (by specifying this in manifest.xml)

Would this work? Can I have multiple separate activities in a PlayN application? Anything else I should keep in mind?

Thanks!

Michael Bayne

unread,
Aug 30, 2017, 4:19:39 PM8/30/17
to pl...@googlegroups.com
There is no equivalent of "activities" on iOS (nor on any other platform aside from Android), so PlayN has no mechanism for using multiple activities in a single game. It would not even make sense. You'd have to create multiple PlayN games (i.e. applications) and switch between them, which would be utterly bizarre.

If you want to control the orientation, you're going to have to fix the orientation from the OS's perspective and manually rotate your UI as I suggested before.


To unsubscribe from this group and stop receiving emails from it, send an email to playn+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

avazqu...@gmail.com

unread,
Aug 31, 2017, 4:23:49 AM8/31/17
to PlayN
Finally. I have done an implementation as you suggest and it works correctly. 
Now I ask one thing:
Doing the way you suggested I lose the possibility of rotating 180º when I am in a landscape view.
Do you have any suggestions for solving this?

Thanks!!!

Brigt Vik

unread,
Aug 31, 2017, 5:03:09 AM8/31/17
to PlayN
As far as I can see, you can only choose to let the OS handle rotation or prevent it from doing so. As you want to manually rotate 90, you'll probably have to manually rotate 180 as well.

I suppose you could add a button, so the user can let your game know it should be turned over 180 degrees, but that is not the best user experience - and you may not have the real estate to spare for the button either. I don't know if you can access the device's current tilt somehow, and infer whether to turn your display around from that? I can't see anything in PlayN that could give you such events, so it may cost you quite a bit of work to find out how to do that. Also, you'd have to deal with some devices not having an accelerometer and thus unable to provide a tilt reading at all.
Reply all
Reply to author
Forward
0 new messages