Prevent screen rotation on Android
Add
android:screenOrientation="portrait"
or
android:screenOrientation="landscape"
to the <activity>
element/s in
the manifest and you're done.
How can I disable landscape mode in Android?
Add android:screenOrientation="portrait"
to the activity in the AndroidManifest.xml. For example:
<activity android:name=".SomeActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" />
Since this has become a super-popular answer, I feel very guilty as forcing portrait is rarely the right solution to the problems it's frequently applied to.
The major caveats with forced portrait:
- This does not absolve you of having to think about activity
lifecycle events or properly saving/restoring state. There are plenty of
things besides app rotation that can trigger an activity
destruction/recreation, including unavoidable things like multitasking. There are no shortcuts; learn to use bundles andretainInstance
fragments. - Keep in mind that unlike the fairly uniform iPhone experience, there are some devices where portrait is not the clearly popular orientation. When users are on devices with hardware keyboards or game pads a la the Nvidia Shield, on Chromebooks, on foldables, or on Samsung DeX, forcing portrait can make your app experience either limiting or a giant usability hassle. If your app doesn't have a strong UX argument that would lead to a negative experience for supporting other orientations, you should probably not force landscape. I'm talking about things like "this is a cash register app for one specific model of tablet always used in a fixed hardware dock."
So most apps should just let the phone sensors, software, and physical configuration make their own decision about how the user wants to interact with your app. A few cases you may still want to think about, though, if you're not happy with the default behavior of sensor
orientation in your use case:
- If your main concern is accidental orientation changes mid-activity that you think the device's sensors and software won't cope with well (for example, in a tilt-based game) consider supporting landscape and portrait, but using
nosensor
for the orientation. This forces landscape on most tablets and portrait on most phones, but I still wouldn't recommend this for most "normal" apps (some users just like to type in the landscape softkeyboard on their phones, and many tablet users read in portrait - and you should let them). - If you still need to force portrait for some reason,
sensorPortrait
may be better thanportrait
for Android 2.3 (Gingerbread) and later; this allows for upside-down portrait, which is quite common in tablet usage.
Turn off auto rotation in fragment
In your Fragment
call inside onResume
to lock to portrait:
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
then in onPause
to unlock orientation:
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
OBS! For the sake, use if(getActivity != null)
before using this methods.
How to prevent screen rotation with android development in delphi xe5 Firemonkey
Add FMX.Platform
to your uses
clause, and get rid of the redundant call to GetPlatformService()
:
uses
..., FMX.Platform;
procedure TLogin.FormCreate(Sender: TObject);
var
ScreenService: IFMXScreenService;
OrientSet: TScreenOrientations;
begin
if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, IInterface(ScreenService)) then
begin
OrientSet := [TScreenOrientation.soLandscape];
ScreenService.SetScreenOrientation(OrientSet);
end;
end;
Android how to prevent screen rotation for specific dialog fragment
Set the below tags in DialogFragment
class where you want to lock screen rotation
@Override public void onResume() {
super.onResume();
//lock screen to portrait
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
@Override public void onPause() {
super.onPause();
//set rotation to sensor dependent
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
}
Disable screen rotation after started?
Please refer Below Link
orientation lock in android programmatically
You can maintain your preference to stay in particular landscape/protrait mode
How to disable rotating to landscape mode?
Just as kalyanpvs has said, the configChanges
are under activity
:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="com.neundorfer.microraphandheld.v1.ParentActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
Related Topics
How to Know the Mtu Size of Android Smartphone
Getcontactsfromfirebase() Method Return an Empty List
R Cannot Be Resolved - Android Error
Is Quitting an Application Frowned Upon
Android 8: Cleartext Http Traffic Not Permitted
How to Programmatically Take a Screenshot on Android
Ship an Application With a Database
Aligning Textviews on the Left and Right Edges in Android Layout
Java.Io.Ioexception: Permission Denied Cannot Create File
React Native: Java_Home Is Not Set and No 'Java' Command Could Be Found in Your Path
Open Facebook Page in Facebook App (If Installed) on Android
Access Ro.Serialno from Native in Android 8
Why Does My Button Not Showing in the Layout
Send Data from Activity to Fragment in Android
Why Is the Android Emulator So Slow? How to Speed Up the Android Emulator
How to Get the Current Gps Location Programmatically in Android