Log in or register to post comments

Screen Orientation

March 9, 2012 - 8:57am #1

Hi everybody,
I have one question about the screen orientation.
I successfully implemented a touch-event + custom-dialog into my app. The custom-dialog pop up always in landscape-mode even if I hold my smartphone in portrait.

I know that the screen coordinate calculation reacts on the orientation of the smartphone. I also want to display the menu and my custom-dialog in portrait-mode if I hold my smartphone in portrait and the same in landscape. Normaly the orientation is set inside of the manifest, but i could not find anything and that means that the orientation must be flexible. What ever somewhere in the source code must be an orientation setting.

Robert

Re: Screen Orientation

March 14, 2012 - 3:39am #4

Hi Kim,
thank you for helping to find a solution.

My failure was it to change this value "setActivityPortraitMode(screenOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);"
to "setActivityPortraitMode(screenOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);".

To change the layout orientation at runtime I implemented for a first approach a button on top of the AR view.

These are my steps:

ImageTargets.java

private synchronized void updateApplicationStatus(int appStatus)
    {
...
        addContentView(mGlView, new LayoutParams(
                              LayoutParams.FILL_PARENT,
                              LayoutParams.FILL_PARENT));
        layoutInflate();
...
}
--------------------------------------------------------------------------
public void layoutInflate(){
    	LinearLayout linear = new LinearLayout(this);
             Button btnInfo = new Button(this);
             btnInfo.setText("More Information");
             btnInfo.setId(10);
             btnInfo.setBackgroundResource(R.drawable.icon_vuforia);
             btnInfo.setOnClickListener(this);
             Button btnChangeOrient = new Button(this);
             btnChangeOrient.setText("Change Orientation");
             btnChangeOrient.setOnClickListener(this);
             btnChangeOrient.setId(11);
             linear.addView(btnChangeOrient);
             linear.addView(btnInfo);
             this.addContentView(linear, new LayoutParams   (LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));    
    }
--------------------------------------------------------------------------
public void onClick(View v) {
		switch (v.getId()) {
                          ...
		case 11:
		if(screenOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
		screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
		// Apply screen orientation
		setRequestedOrientation(screenOrientation);
		setActivityPortraitMode(screenOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		}else{
		screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
		// Apply screen orientation
		setRequestedOrientation(screenOrientation);
		setActivityPortraitMode(screenOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		}
		break;
		}
	}   

For an automatically purpose I tried the following. It's fragile and needs some more improvements. Maybe someone has a better idea to solve this!

public class ImageTargets extends Activity implements OnClickListener
{
...
	int screenOrientation;
	OrientationEventListener orientationListener;
	boolean alreadyChangedOrientation = false;
--------------------------------------------------------------------------
protected void onResume(){
...
orientationListener = new OrientationEventListener(this,			SensorManager.SENSOR_DELAY_NORMAL) {
	            @Override
		public void onOrientationChanged(int orientation) {
                            if (orientationListener.canDetectOrientation()) {
		    if(orientation > 240) { 
		      if (orientation < 280) {
		        if(alreadyChangedOrientation){
		        Log.i(TAG,"Rotation Set Landscape"); 
		        screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
		        // Apply screen orientation
		        setRequestedOrientation(screenOrientation);
		        setActivityPortraitMode(screenOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		         alreadyChangedOrientation = false;
		        }
		      } 
	                 } else { 
		        if(!alreadyChangedOrientation){
		        Log.i(TAG,"Rotation Set Portrait"); 
		        screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
		        // Apply screen orientation
	                     setRequestedOrientation(screenOrientation);
		        setActivityPortraitMode(screenOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		        alreadyChangedOrientation = true;
		       }
		     } 
		   } 
	             }
		};
		orientationListener.enable();
...
}
--------------------------------------------------------------------------
   /** Called when the system is about to start resuming a previous activity.*/
    protected void onPause()
    {
 ...
        orientationListener.disable();
    }

I'm still interested for the native solution!

BTW I have a Nexus One with Android 2.3.6

Re: Screen Orientation

March 13, 2012 - 5:06pm #3

Hmm, that should have worked. What device/OS version are you testing with? If you try using portrait with one of the unmodified samples does it work?

BTW I'm trying to find out how to properly do autorotation with the native SDK, but I'm not sure it's supported. Autorotation is supported with the Unity extension.

- Kim

Re: Screen Orientation

March 13, 2012 - 9:21am #2

I found this code snipped:

Quote:

int screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;

// Apply screen orientation
setRequestedOrientation(screenOrientation);

// Pass on screen orientation info to native code
setActivityPortraitMode(screenOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

If I change the screenOrientation to "ActivityInfo.SCREEN_ORIENTATION_PORTRAIT" the screen dimension don't work correctly anymore .
Is there another way to make this part flexible?

Log in or register to post comments