How to change screen orientation programmatically?

<br />Configuration config = getResources().getConfiguration();

android.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();

switch(config.orientation) {
case Configuration.ORIENTATION_LANDSCAPE:
// Device is now in landscape mode.
MyLandscapeFragment fragment = new MyLandscapeFragment();
fragmentTransaction.replace(android.R.id.fragmentLayoutId, fragment);
break;
default:
// Device is now in portrait mode.
MyPortraitFragment fragment = new MyPortraitFragment();
fragmentTransaction.replace(android.R.id.fragmentLayoutId, fragment);
break;
}
fragmentTransaction.commit();

finally go to android manifest and make some changes for specified activity:

<activity android:name=".MyActivity" ... android:configChanges="orientation">

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s