Question

I'm Displaying a video in my application ..i want to force the activity to open in portrait mode , later the user can switch between orientation (Landscape and portrait ).

Can i do that?

Était-ce utile?

La solution

Yes you can do that. You have several options to do that. I'll get you some examples..

Here is one programmatically:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)

And with this you can set it in the manifest:

<activity 
android:name=".MyActivity" 
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden|keyboard"/>

But please search for similar questions next time, this question was asked many times before.

EDIT:

Just set

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

from the point you want the sensor to take over the setting again.

Autres conseils

try this

public class Orientation extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //---change to landscape mode---
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
}

To change to portrait mode, use the ActivityInfo.SCREEN_ORIENTATION_PORTRAIT constant:


        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

This might useful to you..

You can runtime force activity for specific orientation.

// For Landscpe        
myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);


//For Portrait
myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

And later, you can switch to any of these two.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top