I want to get the phone orientation but keep the screen orientation to portrait. So no matter the user turns the phone to landscape or portrait, the view stays the same, but I can get whether it is turned to landscape or portrait.
Setting the activity to android:screenOrientation="portrait" will fix both but I wouldn't be able to detect the phone orientation via
public void onConfigurationChanged(Configuration newConfig) { switch (newConfig.orientation) { case Configuration.ORIENTATION_PORTRAIT: Toast.makeText(this, "Portrait", Toast.LENGTH_SHORT).show(); break; case Configuration.ORIENTATION_LANDSCAPE: Toast.makeText(this, "Landscape", Toast.LENGTH_SHORT).show(); break; default: break; } }
Has anyone an idea how to fix that?
Select the Start button, then type settings. Select Settings > System > Display, and choose a screen orientation from the drop-down list next to Display orientation.
Use Rotation Control to force screen orientation of Android devices. Once you've installed, open the app and just tap to turn it on. You can even start it when you restart your phone. This will help you when you don't want to open the again and again after every reboot.
To adjust the screen rotation settings: Swipe down from the top of the screen to open the Quick settings panel. Look for the screen orientation icon. Depending on your settings, you may need to look for the Portrait, Landscape, or Auto Rotate icon.
When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.
Here is a multi-purpose class for easily managing screen orientation changes:
public class OrientationManager extends OrientationEventListener { public enum ScreenOrientation { REVERSED_LANDSCAPE, LANDSCAPE, PORTRAIT, REVERSED_PORTRAIT } public ScreenOrientation screenOrientation; private OrientationListener listener; public OrientationManager(Context context, int rate, OrientationListener listener) { super(context, rate); setListener(listener); } public OrientationManager(Context context, int rate) { super(context, rate); } public OrientationManager(Context context) { super(context); } @Override public void onOrientationChanged(int orientation) { if (orientation == -1){ return; } ScreenOrientation newOrientation; if (orientation >= 60 && orientation <= 140){ newOrientation = ScreenOrientation.REVERSED_LANDSCAPE; } else if (orientation >= 140 && orientation <= 220) { newOrientation = ScreenOrientation.REVERSED_PORTRAIT; } else if (orientation >= 220 && orientation <= 300) { newOrientation = ScreenOrientation.LANDSCAPE; } else { newOrientation = ScreenOrientation.PORTRAIT; } if(newOrientation != screenOrientation){ screenOrientation = newOrientation; if(listener != null){ listener.onOrientationChange(screenOrientation); } } } public void setListener(OrientationListener listener){ this.listener = listener; } public ScreenOrientation getScreenOrientation(){ return screenOrientation; } public interface OrientationListener { public void onOrientationChange(ScreenOrientation screenOrientation); } }
This is way more simple, reusable, and you can also get REVERSE_LANDSCAPE and REVERSE_PORTRAIT orientations.
You must implement OrientationListener in order to get notified only when an orientation change occurs.
Don't forget to call orientationManager.enable() to begin orientation tracking, and then calling orientationManager.disable() (this two methods are inherited from OrientationEventListener class)
UPDATE: use case example
MyFragment extends Fragment implements OrientationListener { ... @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); orientationManager = new OrientationManager(getActivity(), SensorManager.SENSOR_DELAY_NORMAL, this); orientationManager.enable(); } @Override public void onOrientationChange(ScreenOrientation screenOrientation) { switch(screenOrientation){ case PORTRAIT: case REVERSED_PORTRAIT: MainActivityBase.getInstance().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); break; case REVERSED_LANDSCAPE: MainActivityBase.getInstance().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); break; case LANDSCAPE: MainActivityBase.getInstance().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); break; } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With