Android force orientation one way only - android

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"

Related

Device orientation lock causes problems

I got an app that is locked in landscape orientation. Now, when I change the devices settings to set its orientation locked to portrait, some problems occur.
After a bit of testing, it seems that for each Activity that is started, the onCreate is called twice.
First time for portrait orientation, second time as an orientationChange with orientation set to landscape. The first orientation is wrong, and probably has something to do with the device being locked to portrait.
In the app, the orientation is forced to landscape programmatically (using setRequestedOrientation), and I prefer to keep it that way, if possible, for various reasons.
Any ideas about how to avoid this behavior?
Thanks in advance!
Ok, I found a solution myself.
After a lot of searching and digging in the code, the solution was fairly simple.
All I had to do was add:
android:configChanges="orientation|screenSize"
... to all my Activities in the manifest file.
This tells the app that I will handle configuration changes myself in the code. Instead of doing this, I didn't add any handlers for these config changes. This way, my Activities aren't recreated when the orientation change happens at the creation of those Activities. I can do it this way, because my app is locked in a certain orientation.
This post came closest to my problem, but it's solution didn't work for me:
Android: set activity orientation BEFORE onCreate, but not in manifest (HDMI plugged in issue)
This post gave me the answer:
http://www.acnenomor.com/1053172p1/oncreate-were-called-twice-after-implementing-setrequestedorientation
Try setting up the orientation directly in the manifest instead of programmatically. Under your activity tag in AndroidManifest.xml add android:screenOrientation="landscape".
This should force the activity to be directly created in the correct orientation.

Set landscape orientation without locking sensor orientation

I'm working on an application that has an inline video player. I need to reproduce the fullscreen behavior of Youtube.
When the user goes to landscape rotating the device I catch it via onConfigurationChanged and make the corresponding layout modifications.
However, I also have a button to set fullscreen. I need this button to go to landscpe (ergo, fullscreen) but don't lock the orientation change via sensor.
Here is what I'm currently doing on onConfigurationChanged:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (this.isLandscape(newConfig.orientation)){
this.goToFullScreenMode();
}else{
this.goToInlineMode();
}
}
And here is what I'm doing in the button but failing because it locks the Activity in landscape:
this.getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
What I need is to go to landscape mode but allow the user to change back to portrait with the sensor, like Youtube does.
I can do it by just locking in landscape until the device is horizontal (by reading directly from the sensor) and then unlock the orientation again. I don't think this is the most "correct" way of doing it.
Greetings from the futuristic year of 2016.
The paths of life took me back to this same predicament on another Android application and I found out that the REAL answer to the problem was to simply add this...
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
...right after setting the orientation to the desired setting.
Here lies my own previous answer
Due to a lack of answers and because I was not abale to solve this in
an elegant way I finally ended using an OrientationListener to get the
raw degrees of the device.
With that I was able to release the user's orientation configuration
once the device was positioned like the actual layout.
If someone is interested in having more info about it just let me
know.
Create a layout-land directory in res and put the landscape version of your layout XML file in that directory.
or else put this line in your activity tag in manifest file
android:screenOrientation="landscape";
Try this..
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Or in your manifest
<activity
android:name="Activity"
android:screenOrientation="landscape" />
Put this in ur Manifest for your Respective activity
<activity
android:name="YourActivityName goes here"
android:screenOrientation="landscape" />

Device Orientation Change doesnt change layout

I have alternative layouts in my layout-normal-land and layout-normal-port folders and they are correctly invoked by the system according to if I hold the device in land or port at start. My problem is, when I rotate the device AFTER I have launched the app, it tries to somehow adapt the already displayed view to the new situation, creating a mess.
How can I tell the system it should switch to the alternate layout during execution?
Have been experimenting around and found that when I dont have android:configChanges="keyboardHidden|orientation" , oncreate gets called again, which gives the correct layout, but its not what I want to have. I dont think this is normal, is it? Maybe a question of bug in Android (2.3.3.)?
By including
android:configChanges="orientation"
in your manifest you are saying you want to handle orientation changes yourself. You should remove it if you want the system to handle it for you.
The automatic handling works extremely well. You should only override it if you have a specific reason for doing so.
In normal operation (without the above manifest entry), an orientation switch causes the current activity to be closed and then re-opened in its new orientation reloading all resources and layouts from the currently active resource folders. The process follows what is known as the "Activity Lifecycle".
If you include the above manifest entry, you are saying, "I will handle all changes myself. Do not close my activity" so it is then your responsibility to remove all unwanted layouts from the activity and replace them with the layouts you now require for the current orientation.
Do you already use an OrientationListener?
If no:
http://developer.android.com/reference/android/view/OrientationEventListener.html#onOrientationChanged(int)
the listener should detect orientation changes.
Then you call setContentView (R.layout.name_of_layout) in your Activity class.
Hope this helps.
do you have android:configChanges="orientation" in your manifest on that activity? That will prevent android from automatically changing your layout.
May be obvious, but:
Within the res folder make sure you have the folders labelled "layout" and "layout-land".
Portrait and landscape .xml files must have the same filename.
I have run into the same problem, and I did not have android:configChanges="orientation" in my manifest.
However I did have [Activity(ConfigurationChanges = ConfigChanges.Orientation)] in my mainactivity. That seemed to be another way to override the automatic orientation handling.

Android orientation issue

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.

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