My app has 3 screens, and all of them must be portrait-only in smartphones and both orientation for tablets.
Unitl now I was using the following code in OnCreate:
if (!getResources().getBoolean(R.bool.isTablet)) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
However if I hold the smartphone in landscape mode and launch the application it will show in landscape for a few seconds then rotate to portrait.
Many answers here suggest locking orientation via manifest, but that would lock the orientation in tablets, which is not desired.
I would be very grateful for any information that leads me to a solution. Thanks.
You could lock it in the Manifest and then unlock it in code if it's a tablet.
It may not matter for your case, but I've read that the Nexus 7 doesn't report itself as a tablet using the method you do. I'm not sure that's accurate though, never having tried it myself.
Related
Testing my application on my Galaxy S2 I have a problem whenever the screen is turned off and back on. My application is locked into landscape by the manifest. The lock screen forces the application into some weird orientation where it is rendering portrait but displaying it was landscape (as in the right 2/5 of my screen is black and the portrait rendering goes off the bottom). The application is also not responsive in this state. If I quickly hit home (or back) and recall the application it works perfect.
As well, if I use a different lock screen (tried with PowerAmp) it works flawlessly.
So can anyone help me understand why this is screwing up and how to fix the orientation whenever this happens?
I can avoid this by avoiding the lock screen through a flag (window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD)) but I don't have any other reason to avoid a lock screen and would like to know what is happening.
This sound like a problem that is unique to the S2. Have you tried your app on the emulator or any other devices. It may just be that the S2 implements some part of android incorrectly.
To handle the orientation problem in your application try to use this for your activity like this
<activity android:configchanges="orientation|keyboardHidden" />
in your Android Manfiest file.
I have been working on a Android project, that is intended to be compatible with both the Android phones and tablets. On the tablet the app is working great and looks good too. However, on the phone we have been running into issues both programmatically and layout wise (working with a 10.1 inch screen is a lot easier than a 4 inch screen). To solve some of these problems we have decided to deactivate screen orientation but only for the phone version, atleast temporarily.
The question is simple, how do I deactivate the screen orientation for Android phone, while keeping it active for the Android tablets? I know I can do this in the manifest file, however, this will also lock the orientation for the tablet I believe.
You can do it from application as well.
Lock screen orientation (Android)
http://developer.android.com/reference/android/app/Activity.html#setRequestedOrientation(int)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
And attributes can be found here:
http://developer.android.com/reference/android/R.attr.html#screenOrientation
To detect, if Android device is Tablet or Phone, you should use this solution (different SO Q&A), https://stackoverflow.com/a/9308284/492624
First off all, try to prevent doing this. I chose todo it because it was an easy fix for a complex problem. But really, try to prevent having todo this.
If you really want todo it, use the following code.
In your activity create a method:
private boolean isTablet() {
return (this.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
And then in your onCreate do the following:
if (!isTablet()) {
// stop screen rotation on phones because <explain>
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
Its seem like, not impossible if you handling this at runtime, by getting screen size and then make RunTimeConfigurationChanges, Then may it will be help you.
Try this Handling Runtime Changes.
And let me know if you get success on it..
Thanks.
One method is , create separate activities for phone version and tablet version. And then fix their orientations in the android manifest file. Another method is to check the device that's being currently used and fix the orientation in the activity itself using the code ,
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Creating separate activities is the better way as its easy to maintain the tab and phone versions.
My app has to support landscape/portrait mode only for tablets, phones will only support portrait mode. What is the best way to implement this?
Options that I am considering:
a) using two apk files, one for phones and one for tablets. The one for phone will lock landscape mode in the manifest
2) using one apk file. Detect that device is a phone, lock landscape mode inside each activity.
any advice will be appreciated.
Regards
You can use a SuperClass for activities and implement this https://stackoverflow.com/a/9629127/710162 on, for example, onCreate().
I don't know if any of you have seen this before:
I have an application which has the Portrait orientation locked for every activity. However, in the HTC Pro Touch phone (and maybe this happens on other phones with physical keyboards) the application rotates to landscape mode for a few seconds and then returns to portait mode, that happens while navigating on the app. Of course this shouldn't happen but I'm not sure why it does that.
I am locking the portrait mode by calling setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) in the create method of my super class.
While I was writting this I realized that the locking instruction is called after the super.create(..), could that make a difference? (Testing takes some time if not I would know right now). If not what else could I do to avoid this auto-rotation?
Thanks in advance, I hope someone knows.
You should set the attribute in your manifest.
<activity android:name="bla.blah.bla" android:screenOrientation="landscape" />
Unless you want to change the orientation later. But that is probably the best way to do it.
I have an android application that I want to always be running in landscape mode.
I have the following code implemented to keep the app in landscape mode all the time:
inside my activity in the Application Manifest
android:screenOrientation="landscape"
This seems to launch and keep the application in landscape view for the whole duration of the application. Perfect!
However, if I have the app running on my HTC Aria (Android 2.1) and the phone locks, if I unlock the phone, I see the application for probably half a second and it's in portrait orientation and then quickly switches back to landscape mode. It is quite frustrating because all of my views are jumbled around and it looks unprofessional as you can imagine. This happens in both the emulator and on my real phone.
Does anyone know how to stop the application from temporarily rotating when the phone is unlocked?
Additions:
I have tried overriding onConfigurationChanged() but with no success.
I have also tried putting setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); into the onResume() but the app still shows as the incorrect orientation for a split second when the phone is unlocked.
Try setting the orientation in the manifest file. it might just work.
After looking at many other apps that are made for landscape orientation only, I can see that their apps don't freeze their orientation after standby either. I have a feeling that this is a limitation of the phone, and that all apps will switch to a portrait orientation when the phone is locked. If all apps have this "problem" then I think it is acceptable not to fix.
Please post if you have found out there is a way to stop the forced portrait orientation.
the app still shows as the incorrect orientation for a split second when the phone is unlocked.
Did you move setContentView(R.layout.main) to the onResume, AFTER where you put your orientation code? I would try setting the orientation, then the setContentView()