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.
Related
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" />
I am new to Orientation in Android. I have worked only for portrait mode so far, now I am working for both orientations.
I am using this method in manifest file for orientation :---
<activity android:name=".Activity_name"
android:configChanges="orientation|keyboardHidden">
The Problem is :-
I have two layout Folders for this - one is layout & other is layout-land for both orientations.
Now I run my app sometimes it takes layout xml file on both orientation & some times layout xml file & layout-land xml file both works .
How to resolve this problem?
& If i use this code :--
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
setContentView(R.layout.login_landscape);
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.login);
}
}
Then orientation problem gets resolved but Activity is not created again, so my code works only one time . My Button is not working now.
2. My second question is that For Both XML File How to manage Code in Config Method
I want to know the complete concept of Orientation .
Answer will be highly Appreciated.
Thanks & Warm Regards,
Deepanker Chaudhary
layout is same as layout-land for portrait orientation it is layout-port folder. There is no need for you handle the layout file for orientation changes. All you need is to save the data on orientation change and restore it when displaying it. You can use the same file name for both the orientations. You can get some help from here for orientation handling. check, also this.
Please make sure below point :
android:configChanges
Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, 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.
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.
I am wondering how to switch off the orientation sensor completely in my android app for certain activities.
Here is what I struggle with. Let's say I set the orientation in the onCreate (or onStart/onResume) method of an activity dynamically with the following command.
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
What I experience is that screen is appearing on the display noticeable faster if I hold the device in a manner that would result in landscape mode according to the orientation sensor.
So what it feels like is as if the activity first wants to set the layout in the orientation as the sensor would suggest and thereafter it switches according to the setRequestedOrientation method. I even sometimes see this on the screen as described in a related post android - unexpected brief orientation change at switch of activity.
Seems like I am too late with the setRequestedOrientation call. So I am wondering how to switch off the the orientation sensor completely to avoid a setting of the orientation prior to the setRequestedOrientation call. Or as alternative, how to call setRequestedOrientation in time. As I need this feature dynamically as decided during runtime (preference settings) I cant set the orientation fixed in the manifest.
Thanks a lot in advance for any hint around this issue.
martin
Edit:
I forgot to mention, the manifest contains
android:configChanges="keyboardHidden|orientation|screenSize"
The performance impact happens because android kills your activity and then starts a new instance with the new configurations.
a good solution to this would be to handle configuration changes manually.
by
adding android:configChanges="orientation" to your activity element in the manifest file
Overriding onConfigurationChanged method in your activity to do the layout
another solution would be having tow activities , one works only in landscape the other in portrait by specifying orientation attribute in manifest file, and lunch the suitable one.
This is how you can set orientation at runtime as follows
int currentOrientation = getRequestedOrientation();
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
I have created two folders layout and layout-land with two xml files, one for portrait and the other for landscape. Both of the xmls work but here is the problem.
My first screen is a login screen and my second screen is a main screen. If I login in portrait and then turn my phone landscape at the main screen. The layout will landscape turn but it uses the portrait xml for the main screen.
The same error occurs if I start in landscape and try to move to portrait later on.
It seems like whatever layout I do for my main then that's the layout that will be used for the rest of the app. Is there anyway to go around this?
Also. I'm already using android:configChanges="orientation" in my manifest for the activities.
If you are using android:configChanges="orientation", then you can override onConfigurationChanged to inflate the new layout after a configuration change.
#Override
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(...);
}
Make sure you have a good reason for preventing the Activity from being recreated on an orientation change... and most importantly don't just do it because orientation changes are crashing your app. Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications.
Using android:configChanges="orientation" means you will be handling the orientation change in code. If you want it to automatically switch layouts, you shouldn't have that there.
Make sure both xml files present in two different folders have same name.