How to set android:screenOrientation="portrait" for Camera - android

I am using Samsung Galaxy Note to run a phonegap application.
When I take picture, I can see that screen rotates just before entering camera.
How can I disable this?
I have tried to force portrait orientation on both, the main thread & camera activity,
but those do not seem to work:
<application android:icon="#drawable/icon" android:label="#string/app_name"
android:debuggable="true">
<activity android:name=".App" android:windowSoftInputMode="adjustPan" android:screenOrientation="portrait"
android:label="#string/app_name" android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.phonegap.DroidGap" android:label="#string/app_name"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
</intent-filter>
</activity>
<activity android:name="com.android.camera.Camera"
android:screenOrientation="portrait">
</activity>
</application>

You need to add the permission on the manifest:
<uses-feature android:name="android.hardware.screen.portrait" />
Doc android of balise uses-feature
For example, if your app requires portrait orientation, you should declare <uses-feature android:name="android.hardware.screen.portrait"/> so that only devices that support portrait orientation (whether always or by user choice) can install your app. If your application supports both orientations, then you don't need to declare either.

How do you take picture? If you are sending intent to fire up camera application you lose control about orientation changes. Also note, that before going to sleep and locking screen launcher forces your application into portrait with an butt-kick regardles of the settings or overriding methods. Most probably you can not and behaviour on other devices could be different.

Use android:configChanges="orientation|keyboardHidden" where you are setting screen as portrait.

There are 2 things here that should be taken into account. First, this
android:screenOrientation="landscape"
has no effect if in the java code there is something like this:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Then, there is this other method that could be bothering you:
mCamera.setDisplayOrientation(90);
This would rotate your camera preview.
Hope this helps!

Related

Preventing an App from changing to Portrait Orientation when the Device is unlocked

I'm developing a game that runs in a wrapped webview for Android devices. Everything is working as expected--in this case, I have locked my view to landscape orientation, and prevented the device from restarting the activity if the phone is rotated.
However, if I put my device to sleep and then unlock it, I see my application in portrait mode for a moment, until my device re-orients to landscape. This behavior makes sense, since a phone is naturally used in portrait orientation, but I don't want my activity to switch from landscape.
If I start any other game, by which I mean those presumable written in native code, that is locked into landscape mode, and then lock and unlock my phone, the game doesn't do the same quickly-switch-from-portrait-to-landscape dance.
What am I missing?
In MainActivity I have
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE)
in several places, including onConfigurationChanged, onWindowFocusChanged, onPause, and onResume.
Here is how my activity is defined in my AndroidManifest
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:hardwareAccelerated="true"
android:screenOrientation="sensorLandscape"
android:configChanges="orientation|screenSize"
android:launchMode="singleTask"
android:alwaysRetainTaskState="true"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Thanks for any help/advise!
-J.L.
You forgot to remove sensor from the screenOrientation tag. Try this:
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:hardwareAccelerated="true"
android:screenOrientation="landscape"
android:configChanges="orientation|screenSize"
android:launchMode="singleTask"
android:alwaysRetainTaskState="true"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
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()

How to keep android rotation landscape always

I want to keep landscape mode always.
I Lock screen to portrait mode on Nexus7, I set android:screenOrientation="sensorLandscape" and android:configChanges="orientation|keyboardHidden"in AndroidManifest.xml, when I go to other activity and back to this activity, then the screen will rotate portrait and rotate to landscape again (Didn't call onCreate method again), but I don't want to rotate the screen in any situations, how to fix it?
I think this is the answere you're looking for:
stackoverflow.com/a/2730894/2249774
For All your Activities in the manifest set
android:screenOrientation="sensorLandscape" android:configChanges="orientation|keyboardHidden"
Example :-
<activity android:name="com.example.test.testActivity" android:label="#string/app_name" android:screenOrientation="sensorLandscape" android:configChanges="orientation|keyboardHidden"">
<intent-filter>
<action android:name="com.example.test.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Repeat this for all activities used in the manifest..
Try editing your AndroidManifest.xml to look like this
<activity
android:name=".YOURACTIVITYNAME"
android:screenOrientation="landscape" />
In your AndroidManifest file, try typing in the following code:
android:configChanges="keyboard|orientation|keyboardHidden|screenSize"
android:screenOrientation="landscape"
example:
<activity
android:name="com.gaspar.slinfilipino.Quiz"
android:label="#string/title_activity_quiz"
android:configChanges="keyboard|orientation|keyboardHidden|screenSize"
android:screenOrientation="landscape"
android:parentActivityName="com.gaspar.slinfilipino.SignLanguageMenu" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.gaspar.slinfilipino.SignLanguageMenu" />
</activity>

How to Lock Android App's Orientation to Portrait in Phones and Landscape in Tablets?

I am developing an Android app whose orientation I don't want changed to landscape mode when the user rotates the device. Also, I want the locked orientation to be portrait mode on phones and landscape mode on tablets. Can this be achieved, if yes how? Thanks.
You just have to define the property below inside the activity element in your AndroidManifest.xml file. It will restrict your orientation to portrait.
android:screenOrientation="portrait"
Example:
<activity
android:name="com.example.demo_spinner.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
</activity>
if you want this to apply to the whole app define the property below inside the application tag like so:
<application>
android:screenOrientation="sensorPortrait"
</application>
Additionaly, as per Eduard Luca's comment below, you can also use screenOrientation="sensorPortrait" if you want to enable rotation by 180 degrees.
You have to add the android:screenOrientation="portrait" directive in your AndroidManifest.xml. This is to be done in your <activity> tag.
In addition, the Android Developers guide states that :
[...] you should also explicitly declare that your application requires
either portrait or landscape orientation with the
element. For example, <uses-feature android:name="android.hardware.screen.portrait" />.
I can see you have accepted an answer which doesn't solve your problem entirely:
android:screenOrientation="portrait"
This will force your app to be portrait on both phones and tablets.
You can have the app forced in the device's "preferred" orientation by using
android:screenOrientation="nosensor"
This will lead to forcing your app to portrait on most phones phones and landscape on tablets.
There are many phones with keypads which were designed for landscape mode. Forcing your app to portrait can make it almost unusable on such devices. Android is recently migrating to other types of devices as well. It is best to just let the device choose the preferred orientation.
It might be.. you have to identify it is tablet or phone by programmatically...
First check device is phone or tablet
Determine if the device is a smartphone or tablet?
Tablet or Phone - Android
Then......
if(isTablet)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}else
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
<activity android:name=".yourActivity"
android:screenOrientation="portrait" ... />
add to main activity and add
android:configChanges="keyboardHidden"
to keep your program from changing mode when keyboard is called.
Set the Screen orientation to portrait in Manifest file under the activity Tag.
Here the example
You need to enter in every Activity
Add The Following Lines in Activity
for portrait
android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity"
for landscape
android:screenOrientation="landscape"
tools:ignore="LockedOrientationActivity"
Here The Example of MainActivity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="org.thcb.app">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity2"
android:screenOrientation="landscape"
tools:ignore="LockedOrientationActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Set the Screen orientation to portrait in Manifest file under the activity Tag.
android:screenOrientation="locked"
in <application> for all app
in <activity> for actual activity
I would like to add to Bhavesh's answer. The problem is that if users keep the phone in landscape mode and run the app it will first go to landscape mode since it's based on sensor in manifest and will then immediately switch to portrait mode in phones because of the code in onCreate. To solve this problem below approach worked for me.
1 Declare locked orientation in manifest for activity android:screenOrientation="locked"
<activity
android:name=".overview.OverviewActivity"
android:screenOrientation="locked" />
2 Check for tablet or phone in actiivty or base activity
override fun onCreate(savedInstanceState: Bundle?) {
//check if the device is a tablet or a phone and set the orientation accordingly
handleOrientationConfiguration()
super.onCreate(savedInstanceState)
}
/**
* This function has to be called before anything else in order to inform the system about
* expected orientation configuration based on if it is a phone or a tablet
*/
private fun handleOrientationConfiguration() {
requestedOrientation = if (UIUtils.isTablet(this).not()) {
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
} else {
ActivityInfo.SCREEN_ORIENTATION_SENSOR
}
}
UIUtils.kt
fun isTablet(context: Context): Boolean {
return context.resources.configuration.smallestScreenWidthDp >= 600
}
That's it, it will launch the app in the locked mode so that if you are on phone it will be always portraited and if you are on a tablet it will rotate based on the orientation of the device. This way it will eliminate the issue on phones where it switches between landscape and portrait at the start.
Read more here about the locked mode below
https://developer.android.com/guide/topics/manifest/activity-element
Just Add:
android:screenOrientation="portrait"
in "AndroidManifest.xml" :
<activity
android:screenOrientation="portrait"
android:name=".MainActivity"
android:label="#string/app_name">
</activity>

configChanges not recognized by TabHost?

I have a pretty big problem with my TabHost. Although I have declared all of my Activities (Including my TabHost Activity) to ignore orientation and keyboardHidden, if I flip my Phone (Android 4.0) It still recreates the activity.
Here is a shortened version of my Manifest:
<application
android:icon="#drawable/icon"
android:label="#string/app_name" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".TabHost"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity1"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name" />
<activity
android:name=".Activity2"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name" />
<activity
android:name=".Activity3"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name" />
</application>
I don't think that the code of the activities would help?!
If it would however, let me know and I'll post it too.
Kind regards
if you are trying to stop the rotation of the screen use following code in your AndroidManifest.xml for each activity.
android:screenOrientation="nosensor"
UPDATE
According to API when using android:configChanges="orientation|keyboardHidden"
"orientation"
The screen orientation has changed — the user has rotated the device.
Note: If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it also changes when a device switches between portrait and landscape orientations.
so, if you are targeting API level 13 or higher you gotta specify screen size also.

Force an Android activity to always use landscape mode

I am using the Android VNC viewer on my HTC G1. But for some reason, that application is always in landscape mode despite my G1 is in portrait mode. Since the Android VNC viewer is open source, I would like know how is it possible hard code an activity to be 'landscape'. I would like to change it to respect the phone orientation.
Looking at the AndroidManifest.xml (link), on line 9:
<activity android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden" android:name="VncCanvasActivity">
This line specifies the screenOrientation as landscape, but author goes further in overriding any screen orientation changes with configChanges="orientation|keyboardHidden". This points to a overridden function in VncCanvasActivity.java.
If you look at VncCanvasActivity, on line 109 is the overrided function:
#Override
public void onConfigurationChanged(Configuration newConfig) {
// ignore orientation/keyboard change
super.onConfigurationChanged(newConfig);
}
The author specifically put a comment to ignore any keyboard or orientation changes.
If you want to change this, you can go back to the AndroidManifest.xml file shown above, and change the line to:
<activity android:screenOrientation="sensor" android:name="VncCanvasActivity">
This should change the program to switch from portrait to landscape when the user rotates the device.
This may work, but might mess up how the GUI looks, depending on how the layout were created. You will have to account for that. Also, depending on how the activities are coded, you may notice that when screen orientation is changed, the values that were filled into any input boxes disappear. This also may have to be handled.
You can set the same data in your java code as well.
myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Other values on ActivityInfo will let you set it back to sensor driven or locked portrait. Personally, I like to set it to something in the Manifest as suggested in another answer to this question and then change it later using the above call in the Android SDK if there's a need.
In my OnCreate(Bundle), I generally do the following:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
You can specify the orientation of an activity in the manifest. See here.
<activity android:allowTaskReparenting=["true" | "false"]
...
android:screenOrientation=["unspecified" | "user" | "behind" |
"landscape" | "portrait" |
"sensor" | "nosensor"]
...
"adjustResize", "adjustPan"] >
In the manifest:
<activity android:name=".YourActivity"
android:screenOrientation="portrait"
android:configChanges="orientation|screenSize">
In your activity:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.your_activity_layout);
The following is the code which I used to display all activity in landscape mode:
<activity android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden"
android:name="abcActivty"/>
A quick and simple solution is for the AndroidManifest.xml file, add the following for each activity that you wish to force to landscape mode:
android:screenOrientation="landscape"
This works for Xamarin.Android. In OnCreate()
RequestedOrientation = Android.Content.PM.ScreenOrientation.Landscape;
That's it!! Long waiting for this fix.
I've an old Android issue about double-start an activity that required (programmatically) landscape mode: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
Now Android make Landscape mode on start.
Arslan,
why do you want to force orientation pro grammatically, though there's already a way in manifest
<activity android:name=".youractivityName" android:screenOrientation="portrait" />
Add The Following Lines in Activity
You need to enter in every Activity
for landscape
android:screenOrientation="landscape"
tools:ignore="LockedOrientationActivity"
for portrait
android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity"
Here The Example of MainActivity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="org.thcb.app">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:screenOrientation="landscape"
tools:ignore="LockedOrientationActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity2"
android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Doing it in code is is IMO wrong and even more so if you put it into the onCreate. Do it in the manifest and the "system" knows the orientation from the startup of the app. And this type of meta or top level "guidance" SHOULD be in the manifest. If you want to prove it to yourself set a break in the Activity's onCreate. If you do it in code there it will be called twice : it starts up in Portrait mode then is switched to Landscape. This does not happen if you do it in the manifest.
For Android 4.0 (Ice Cream Sandwich) and later, I needed to add these, besides the landscape value.
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
Using only keyboardHidden|orientation would still result in memory leaks and recreation of my activities when pressing the power button.
Use the ActivityInfo (android.content.pm.ActivityInfo) in your onCreate method before calling setLayout method like this
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
use Only
android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity"
Press CTRL+F11 to rotate the screen.

Categories

Resources