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.
Related
I know this :
android:screenOrientation="landscape"
But depending on which orientation you open the app, the way changes (even if it's landscape, there're 2 ways).
How can I force an app to open one way?
For clarity sake, I really want the volume button at the bottom of the tablet only.
Though I know it changes depending on which constructor I'm building my app for a single device, so I wonder if that is possible.
Try with this in onCreate() method of Activity
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
You can just lock the landscape orientation this way :
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Or if you want the reversed landscape orientation :
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
I believe you can find what you need here:
activity | Android Developer #android:screenOrientation
There are more options than landscape and portrait, I believe the ones helpful to you would be "reverseLandscape" and "sensorLandscape". Since the volume button differs from tablet to tablet, you may want to use "sensorLandscape" and ask the user turn their tablet to such orientation, just a suggestion.
Edit: Just saw you said you are building for one device, I guess "reverseLandscape" will do just fine.
Don't forget to put in the manifest file where the activity is defined!!!
Best regards
You can also add to manifest file this option:
android:screenOrientation="portrait"
or
android:screenOrientation="landscape"
Just a general question here. I've read that it is not possible to lock orientation to portrait or landscape in a relative layout. I developed the app already so I don't want to change my layout and have to delete everything in my layout and start over, but if I have to I will. So is there anyway to workaround this like turn auto rotate off on the device the app is installed? Please don't go research and waste your time to find a workaround because it isn't a necessity. So once again thanks for reading and any help is greatly appreciated. Bye!
You can define orinetation in AndroidManifest.xml
<activity android:name=".NameOfTheActivity"
android:screenOrientation="portrait"/>
I am trying to make a quiz app in which the questions are all displayed on the same activity. Everytime the screen is rotated, on create method is recalled and the quiz returns back to its first question, losing all the data regarding the other questions.
How can I prevent this ?
Thanks.
*ps: I don't want my application to work only at portrait or landscape mode but at both.
In your AndroidManifest, within your activity add:
android:configChanges="orientation"
So something like:
<activity android:name="MyActivity"
android:configChanges="orientation" />
This will tell the app that you want to handle changes by orientation yourself. You can do nothing with it, or catch the corresponding method in your app.
You can add this to your activities entry:
android:configChanges="keyboardHidden|orientation"
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"
I have application which works only in landscape orientation . I set that in xml layout. When I'm starting application it works ok . But when the application is started and next if I lock the phone and then unlock, the application first 1-2 seconds is in portrait mode and then in landscape. Is it possible to skip theese 2 seconds?
Try defining it explicitly in your manifest. Add android:screenorientation="portrait" to each <activity> element.
Not to be a downer, I really hope you solve this. Just wanted to throw out there that I have noticed this sort of behavior(typically for me the apps are supposed to stay in portrait but they show landscape for about 2 seconds) on multiple applications on my Sprint HTC Hero, including the default home application and many of the default included apps (contacts, etc). I am beginning to suspect it's an android problem. Again, feel free to disagree with me, but I just wanted to say that I have noticed this on a lot of apps & you're not alone here :/ However, there are some apps that I have not noticed it on, so either a) I am just getting lucky or b) there is a correct solution
Execute this in your onCreate(), just before setcontentview:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
I use this method in my apps and I never notice the orientation changing. It's locked solid into the requested orientation.
To be provocative, actually, what he needs to put in each activity element in his manifest is:
android:screenOrientation="landscape"
and not "portrait"
:P
I hope this help; put these on every activity-element in your Android Manifest-file.
android:configChanges="orientation"
android:screenOrientation="landscape"
Of course it is landscape; my brain was not the quickest this time.