Is it possible to prevent the screen from rotating on Android?

Asked 2 years ago, Updated 2 years ago, 132 views

Device: HTC Desire

I'm developing an app. It's hard to design the landscape mode, so I'd like to make it possible only for the portrate mode.

<application 
  android:debuggable="true"
  android:icon="@drawable/icon" 
  android:label="@string/app_name"
  android:screenOrientation="portrait">

So I added it to the manifest file, but it doesn't work on my device. So I looked up other sites

<application 
  android:debuggable="true"
  android:icon="@drawable/icon" 
  android:label="@string/app_name"
  android:configChanges="orientation"       
  android:screenOrientation="portrait">

You do that in the manifest file and put the code below in the activity class.

public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

Even if you do this, if you flip the device, it goes horizontally and vertically. How do I fix it?

android screen orientation landscape portrait

2022-09-22 22:28

1 Answers

Instead of putting screen Orientation in the element of the application, put it in the element of activity. And put configChanges like the code below.

<activity
   android:screenOrientation="portrait"
   android:configChanges="orientation|keyboardHidden">
</activity>


2022-09-22 22:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.