Set landscape orientation without locking sensor orientation - android

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" />

Related

Android force orientation one way only

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"

setRequestedOrientation for landscape loads portrait Layout

I am developing an app that needs to run strictly in portrait mode in smart phones and strictly in landscape mode in tablets. I am using the following code to set the orientation manually in onCreate method of my login activity:
if (getResources().getBoolean(R.bool.portrait_only)) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
Where the bool value comes false in Tablets. However the orientation is changed to landscape; still the layout file is picked from "layout_large" folder instead of "layout_large_land" folder. Hence my view looks stretched.
I have tried this link.
But it doesn't help.!
Any help is appreciated.
Thanks in Advance.
I had the same problem. Make sure you're NOT setting android:configChanges="orientation" for your activity in AndroidManifest.
I'm guessing the problem with that is that setRequestedOrientation potentially triggers an orientation config change, HOWEVER by using android:configChanges="orientation" this config change is not registered by the system.
Alternatively, if you want to keep the Manifest setting, you would have to implement your own recreation mechanic for this particular configuration change.
It may look something like this:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(getResources().getBoolean(R.bool.portrait_only))
recreate();
}
I have used a folder like this for Tab & mobile
layout folder is for mobile and layout-sw720dp for Tabs. 720 dp is width which tells 720+ dp will use that folder for layout.
Being late in this thread but will help in the future SO. In my case I was programmatically changing the orientation of the screen by calling
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
OR
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
depending on the user's action. I have also created a separate layout for portrait and landscape but every time on changing the screen from portrait to landscape, it always loading the portrait screen in landscape.
In AndroidManifest.xml, I've set this
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
So, To solve this problem, Need to remove the orientation from android:configChanges then it works. This is what android documentation Link mentioned
The "orientation" value prevents restarts when the screen orientation
changes.

Android screen orientation detection without layout change

I want to detect layout change in my activity while keeping the widgets where they are. Is there any way to do this. I tried setOrientation(LinearLayout.HORIZONTAL) and it doesn't seem to work. Any ideas?
Try to add android:configChanges="orientation" in your manifest file in the activity and you may be able to get the orientation in the onResume method of your activity by using
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
//WHAT TO DO IN LANDSCAPE
} else {
//WHAT TO DO IN PORTRAIT
}
As a lower level solution, you could use one of the Sensors, to detect the orientation/tilt of the device. If you don't want layout changes you can constraint your activity to portrait or landscape only in the manifest...
You can play with the app Elixir to see what values a specific sensor returns.

App using libgdx restarts whenever device rotates

I made an app using accelerometer but problem is whenever it detects rotation of the screen, it goes to main menu.
I used following code to disable rotation but it still detects the rotation action and goes to mainmenu although it does not become landscape mode anymore.
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
How can I prevent this rotation detection?
Put android:screenOrientation="portrait" inside your activity tag in the AndroidManifest.xml file. That attribute defines that this specific activity should always be run in the portrait mode and thus Android will not try to change screen orientation on rotation.
You will also want to add
android:configChanges="keyboard|keyboardHidden|orientation"
in order to prevent restarts when the keyboard appears or is hidden.
If you use this, you can even remove android:screenOrientation="portrait" if you want your game to work in both landscape and portrait mode. See the libgdx wiki: https://code.google.com/p/libgdx/wiki/ApplicationConfiguration#The_.xml_File
Kimi answer is correct, but if you want to run your game also on the Amazon device, then you should add the additional value "screenSize". So result would be:android:configChanges="keyboard|keyboardHidden|orientation|screenSize"

How to make an application ignore screen orientation change?

Is there a way to make an application completely ignore a screen orientation change?
It is possible, quite easily, to override the default behavior and forbid a screen orientation change when the keyboard is open/closed.
Modifying the manifest
Open the manifest, switch to the Application tab and select the desired Activity you wish to override for the orientation change behavior.
Within Attributes you need to change two fields:
Screen orientation: select either portrait or landscape - whichever is desired. This will be the default layout.
Select events for Config changes you wish to override:
In this case these are keyboardHidden and orientation.
Modifying the Activity implementation
Now you need to override a single function within desired Activity.
Just add the function below to your Activity's class.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
This is the default implementation if using the Source->Override/Implement Methods
menu option.
That's it! Now your orientation will always be kept.
Remember that this setting is per Activity - so you need to repeat this step for each Activity you wish to forbid the orientation change!
(Based on SDK 1.1)
You can make the same change in code with the following line (called in an activity):
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Once you make this call, your application will stay in landscape (or portrait) mode. You can use the same call (with a different ActivityInfo enum) to make it sensitive to the orientation shifting again.
There's a full DevX article on the topic in Developing Orientation-Aware Android Applications.
(WARNING: since I've posted this link DevX has put up a registration wall.)
If you are setting either by AndroidManifest.xml or with the setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); you are going to run into issues with tablets. Their natural/default orientation is landscape.
If you truly want to completely ignore screen orientation changes I would use this setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); value. I talk more about it in Stack Overflow question Android natural sensor orientation help.
Here is the xml:
<activity
android:name=".MyActivity"
android:screenOrientation="nosensor"
android:configChanges="orientation|keyboardHidden|keyboard"/>
You can define your activity in the AndroidManifest.xml file like this:
<activity
android:name=".MyActivity"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden|keyboard"/>`
In this case you should set the property for each activity. I didn't find an inline solution for all applications.
Add this to the activity:
android:configChanges="orientation|screenSize"
<activity android:screenOrientation="portrait"></activity>
You want to read the current orientation and keep it this way throughout all the activity's lifetime, so what I did is the following, at the end of onCreate:
// choose an orientation and stay in it
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
else if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Categories

Resources