Android Emulator switching landscape to portrait problem.

Coders forum. Discuss techniques, swap code snippets, ask for help etc. relating to all forms of software development.
Post Reply
User avatar
RageMatrix
Baaaaaaa!
Baaaaaaa!
Posts: 5515
Joined: Fri May 10, 2002 12:52 pm 0
XBL Gamertag: Jon256
Location: East Sussex

Android Emulator switching landscape to portrait problem.

Post by RageMatrix »

I am half-following along with the "Hello, Android" book (3rd edition) by Ed Burnette. I have added a portrait GUI layout main.xml under "res/layout/main.xml" and a landscape GUI layout under "res/layout-land/main.xml" as described in the book. When using my app under the SDK emulator, I can switch my GUI from portrait to landscape by pressing CTRL-F11. However, if I press this again, the GUI is stuck in landscape mode. I have no other code other than: -

Code: Select all

package com.thenode.mobile.podgrab;

import android.app.Activity;
import android.os.Bundle;

public class PodGrabActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
What do I have to do to make my GUI switch back to portrait mode from landscape? Am I missing something or is this a bug in the emulator? The book makes no mention of this problem. Any advice?

I use LinuxQuestions.org a lot for Linux stuff and they've recently opened AndroidQuestions.org too - but my posts never seem to show up there, so I'm posting here :D
XBox Gamertag: Jon256
Wii Code: 4849 1090 9042 6725
"Wrapped in metal, wrapped in ivy"
The Official UNIX/Linux Thread
User avatar
Darkflame
Sheepie Whore
Sheepie Whore
Posts: 18236
Joined: Mon Mar 31, 2003 11:06 am 0
Location: Not the UK

Re: Android Emulator switching landscape to portrait problem

Post by Darkflame »

Dont you have a phone to test stuff on? I find its quicker and easier debugging on the real hardware. (any android phone would do and a normal usb connection)
User avatar
RageMatrix
Baaaaaaa!
Baaaaaaa!
Posts: 5515
Joined: Fri May 10, 2002 12:52 pm 0
XBL Gamertag: Jon256
Location: East Sussex

Re: Android Emulator switching landscape to portrait problem

Post by RageMatrix »

Tried it out on my Nexus S, it ignored the landscape layout completely which was a slightly different result than I was expecting since the emulator will change to landscape but not change back :) Anyway, it turns out this entry in the Android manifest file is the culprit: -

android:configChanges="orientation"

This ignores the /res/layout-land directory completely and only uses the one in /res/layout only.
If you create a different XML for landscape orientation, don't use the

Code: Select all

android:configChanges="orientation" 
tag for that Activity or add a new overridden event method. It's there to stop the Activity from restarting apparently, so also from reloading the XML layout which you usually do in onCreate(). Instead, onConfigurationChanged(newConfig) is called. So you can do:

Code: Select all

@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
}
This wil reload the layout from the layout-land directory if available. Thanks for mentioning that in the "Hello, Android" book, Ed! :-/
XBox Gamertag: Jon256
Wii Code: 4849 1090 9042 6725
"Wrapped in metal, wrapped in ivy"
The Official UNIX/Linux Thread
User avatar
sack
Baaaaaaa!
Baaaaaaa!
Posts: 7077
Joined: Fri May 10, 2002 4:13 pm 0
XBL Gamertag: evilgopher
PSN: sixFiveOhTwo
Location: Rijswijk, The Netherlands
Contact:

Re: Android Emulator switching landscape to portrait problem

Post by sack »

Speaking of Android...

does anybody know of a way of using the orientation stuff to figure out which way is down in Android (2.1)? I managed to get some code together to listen to the various sensors and spit them out onto the screen, but I haven't managed to connect the dots and figure out which way is down (although it was getting a bit late when I was playing with this).

Ideally what I'm after is having a GL context that is locked in orientation, so turning the device has no effect on the screen layout, and to be able to generate a unit vector pointing downwards that I can use to set gravity in a game.

Any pointers where to look for this kind of stuff?
BillyT
Baa!
Baa!
Posts: 77
Joined: Tue Mar 24, 2009 1:17 pm 0

Re: Android Emulator switching landscape to portrait problem

Post by BillyT »

To lock to a particular view you would add this to your manifest file:

android:screenOrientation="landscape" or android:screenOrientation="portrait" depending on what you want.

As for which way is up, there is a problem with all that because it depends on what the real default orientation of the device is. Some devices can think landscape and the sensors are configured around that.

I cannot remember where the article is (Android dev site) but it goes in depth on how to detect which way the sensor is.
Post Reply