Lock screen orientation (Android) [duplicate] - android

This question already has answers here:
How do I disable orientation change on Android?
(13 answers)
Closed 3 years ago.
I'm writing an android application that uses tabs with different contents (activities).
In one of these activities, I would like to lock the screen orientation to "Landscape"-mode,
but in the other activities, I want the normal orientation (according to sensor).
What I'm doing now is that I'm calling
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
when I switch to the landscape mode activity, and
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
when I switch back to the other activities. However, this doesn't seem to work,
the whole application locks up. What is the normal approach to this problem?

In the Manifest, you can set the screenOrientation to landscape. It would look something like this in the XML:
<activity android:name="MyActivity"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation|screenSize">
...
</activity>
Where MyActivity is the one you want to stay in landscape.
The android:configChanges=... line prevents onResume(), onPause() from being called when the screen is rotated. Without this line, the rotation will stay as you requested but the calls will still be made.
Note: keyboardHidden and orientation are required for < Android 3.2 (API level 13), and all three options are required 3.2 or above, not just orientation.

I had a similar problem.
When I entered
<activity android:name="MyActivity" android:screenOrientation="landscape"></activity>
In the manifest file this caused that activity to display in landscape. However when I returned to previous activities they displayed in lanscape even though they were set to portrait. However by adding
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
immediately after the OnCreate section of the target activity resolved the problem. So I now use both methods.

inside the Android manifest file of your project, find the activity declaration of whose you want to fix the orientation and add the following piece of code ,
android:screenOrientation="landscape"
for landscape orientation and for portrait add the following code,
android:screenOrientation="portrait"

Related

How to fix the orientation of the page to Landscape in Application Craft?

I have a adobe form which I need to incorporate in my mobile app which I created in application craft, But since the form is a little wide, opening it in Landscape solves my purpose. This is the reason I need the page to be fixed in Landscape while other pages will rotate according to the phone movement.
The only thing I have got is that there is a phone gap property but it sets the orientation of the complete app and not a page. So is there any way to do it?
In the manifest, set this for all your activities:
<activity android:name=".YourActivity"
android:configChanges="orientation"
android:screenOrientation="landscape"/>
Let me explain:
With android:configChanges="orientation" you tell Android that you will be responsible of the changes of orientation.
android:screenOrientation="landscape" you set the default orientation mode.

Screen Orientation Handling

In my application, most of the activities has fixed layout - portrait.
So I've mentioned in manifest:
<activity
android:name="com.example.activity5"
android:screenOrientation="portrait" />
But in one or two activities I've to show landscape layout also, in such a way that by default the activity opens in portrait mode. But if user tilts the phone to left/right it changes to landscape. (also if user rotates the phone to upside down, activity should not go to portrait mode).
That is, basically, I want orientation change in 3-way. Default(Potrait) & Left-Right(Landscape).
So, what changes do I need to do in my code & xmls?
Should I choose "sensorPortrait" OR "sensorLandscape"
Should I use android:configChanges="orientation"
I tried few steps, but they are throwing null pointer exception.
I dont know what I'm missing.
P.S. Both orientation have different layouts.
Thank You
I think you want to do this:
1) Just remove android:screenOrientation="portrait", and
2) In your code, in onCreate(), add this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
--(UNRELATED) SIDE NOTE--
To force landscape use this:
<activity android:name="com.example.activity5"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden" />
Write to Main.class(OnCreate):
setContentView(R.layout.activity_main);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Can a live wallpaper lock the screen in portrait mode?

Can a live wallpaper lock the screen in portrait mode? And if so, how?
I should mention that I have seen two supposed answers to this question on SO, one appeared inordinately complicated, I didn't understand it all and the answer was not accepted by the original poster. The second answer didn't work for me.
A third answer involving the use of:
android:screenOrientatin = "portrait" or "landscape"
has been suggested, but it is not clear exactly where this should go in the manifest.
EDIT: have tried putting android:screenOrientation="portrait" in many different places in the manifest, and none of them worked.
EDIT: another answer was to rotate your bitmaps and handle a rotation by just drawing everything sideways - but this looks very ugly because, as you rotate you phone, the OS instigates a rotation animation - which means that you get a horrid jumping effect as you turn the phone.
I'm beginning to suspect that the true answer is simply "no".
Did you try setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); ?
In your AndroidManifest.xml you should have something like:
<application
android:icon="#drawable/app_icon"
android:label="#string/nuboLogin"
android:name=".LoginApplication"
android:debuggable="true">
<activity
android:name=".WallPaperActivity"
android:label="#string/wallPaper"
android:windowSoftInputMode="adjustPan"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden">
</activity>
This should make sure that your Activity runs in portrait mode. If you prefer landscape, you can easily guess what you should modify
Android application restarts the activity when the orientation changes. You can either use
android:configChanges in your manifest. The activity is shut down and restarted by default, when a configuration change occurs at runtime, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.
use android:screenOrientatin = "portrait" or "landscape" it will force the app to run in the mode you specify. However it will not prevent the activity from being shut down and restarted.

Ensure screen orientation is fixed to portrait and NEVER changes

I would like my Android app to instantiate it's home screen activity one time only. I am managing the back stack appropriately to achieve this but have just discovered an orientation issue when the app starts up.
Visually this orientation change only shows itself on the emulator. (probably runs too fast to be observed on a device).
Here's what happens :: -->
activity.onCreate()
activity.onDestroy()
activity.onCreate()
This sequence makes sense and is caused by the change in orientation. What does not make sense (to me) is that it happens at all because I have done the following to prevent an orientation change :: -->
AndroidManifest.xml contains
android:screenOrientation="portrait"
for all my activities and in the home screen activity onCreate() method, I'm calling
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
So why do I get an orientation change?
Add android:screenOrientation="portrait" in your manifest file where you declare your activity like this
<activity android:name=".yourActivity"
....
android:screenOrientation="portrait"/>
if you want to do using java code
try
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
before you setContentView for your activity in onCreate()
see here

Android force Horizontal (landscape) layout [duplicate]

This question already has answers here:
Force "portrait" orientation mode
(12 answers)
Closed 2 years ago.
I'm pretty close to finished with my first game for Android, and I've come across a problem that's so simple I'm sure I'll feel stupid for not knowing how to solve it, but how can I force the app to stay in a Horizontal layout? Right now, if you turn the phone (emulator) it flips the graphics and squeezes them. I want the game to start horizontally and stay that way regardless of how the user turns the phone.
Thank you.
Open the AndroidManifest.xml and add the following android:screenOrientation="landscape"
e.g.
<activity android:name=".ActivtyName"
android:screenOrientation="landscape"
>
To do this at the Activity level, in case you want to change it dynamically or allow the user to select the orientation, use setRequestedOrientation in your Activity's onCreate method.
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
etc...
In the AndroidManifest file, try add android:screenOrientation="landscape" to the activity's attribute.
e.g.
<activity android:name=".myMainActivity"
android:screenOrientation="landscape">
Or, you could use a R.styleable file and set the Orientation settings there. More info
I think you should be able to set
android:screenOrientation="landscape"
on your Activity in the manifest. It doesn't look like there's an Application wide setting, so you'll probably need to do it for each Activity separately.
Here is an easier way might help someone having same problem and dont want to deal with xml. You can go to Application Nodes select your activity and select screen orientation. See the image attached for clarity.

Categories

Resources