How do I get the CURRENT orientation (ActivityInfo.SCREEN_ORIENTATION_*) of an Android device?

I ended up using the following solution: private int getScreenOrientation() { int rotation = getWindowManager().getDefaultDisplay().getRotation(); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int width = dm.widthPixels; int height = dm.heightPixels; int orientation; // if the device’s natural orientation is portrait: if ((rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) && height > width || (rotation == Surface.ROTATION_90 … Read more

Flutter: How to set and lock screen orientation on-demand

First import the services package: import ‘package:flutter/services.dart’; This will give you access to the SystemChrome class, which “Controls specific aspects of the operating system’s graphical interface and how it interacts with the application.” When you load the Widget, do something like this: @override void initState(){ super.initState(); SystemChrome.setPreferredOrientations([ DeviceOrientation.landscapeRight, DeviceOrientation.landscapeLeft, ]); } then when I leave … Read more

shouldAutorotateToInterfaceOrientation is not working in iOS 6

EDIT: This is happening because Apple has changed the way of managing the Orientation of UIViewController. In iOS6 orientation handles differently. In iOS6 shouldAutorotateToInterfaceOrientation method is deprecated. View controllers (such as UINavigationController) do not consult their children to determine whether they should autorotate. By default, an app and a view controller’s supported interface orientations are … Read more

Android: Temporarily disable orientation changes in an Activity

As explained by Chris in his self-answer, calling setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); and then setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); really works like charm… on real devices ! Don’t think that it’s broken when testing on the emulator, the ctrl+F11 shortcut ALWAYS change the screen orientation, without emulating sensors moves. EDIT: this was not the best possible answer. As explained in the comments, … Read more

Android: allow portrait and landscape for tablets, but force portrait on phone?

Here’s a good way using resources and size qualifiers. Put this bool resource in res/values as bools.xml or whatever (file names don’t matter here): <?xml version=”1.0″ encoding=”utf-8″?> <resources> <bool name=”portrait_only”>true</bool> </resources> Put this one in res/values-sw600dp and res/values-xlarge: <?xml version=”1.0″ encoding=”utf-8″?> <resources> <bool name=”portrait_only”>false</bool> </resources> See this supplemental answer for help adding these directories and … Read more

Change Screen Orientation programmatically using a Button

Yes it is implementable! ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); ActivityInfo.SCREEN_ORIENTATION_PORTRAIT setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); ActivityInfo http://developer.android.com/reference/android/content/pm/ActivityInfo.html Refer the link: Button buttonSetPortrait = (Button)findViewById(R.id.setPortrait); Button buttonSetLandscape = (Button)findViewById(R.id.setLandscape); buttonSetPortrait.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View arg0) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } }); buttonSetLandscape.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View arg0) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } }); http://android-er.blogspot.in/2011/08/set-screen-orientation-programmatically.html

How to prevent custom views from losing state across screen orientation changes

I think this is a much simpler version. Bundle is a built-in type which implements Parcelable public class CustomView extends View { private int stuff; // stuff @Override public Parcelable onSaveInstanceState() { Bundle bundle = new Bundle(); bundle.putParcelable(“superState”, super.onSaveInstanceState()); bundle.putInt(“stuff”, this.stuff); // … save stuff return bundle; } @Override public void onRestoreInstanceState(Parcelable state) { if … Read more