How to force change layouts in Android - android

I have my app's orientation forced via
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
but when I want to change layout to landscape one (when device is rotated - i get the info via OrientationEventListener) it just doesnt render it as landscape. Is there any way how to force it? Just like imagine holding your device in portait mode but the layout is landscape. but not achieved via
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
or
android:screenOrientation="landscape" in manifest
I want to have it forced to portait because I'm building a camera app and it messes up the camera everytime and I want to have it like the default camera has it - no change, just the button orientation. If that makes sense.

A lot of camera apps achieve this by rotating Views and it's a good way to do it because you don't have to deal with Activity lifecycle as well as camera lifecycle. They have a locked orientation but they use accelerometer to get device rotation and then rotate Views accordingly.
Camera API gives you ability to set camera rotation as well as picture orientation. Picture orientation should be set to zero because Camera API is unreliable with this.
Use accelerometer to determine real device rotation and set EXIF rotation of the picture to a correct one.

Related

Get android device orientation even when autoRotation is turned off

I have an android app in which the screen orientation is set to portrait in manifest file.
It uses a custom camera in b/w , to take a photo and I need to save the photo.
Before I save the image I have to rotate the image depending on how the user is holding the camera, so that the image gets saved in the right orientation.
So, is there any way by which I can get the device orientation even when my app is running in portrait mode and android "auto-rotation" feature is ON.
How can I achieve this?
You should do the rotation depending on the orientation information within the image, not depending on the device orientation.
Here is some more information on how to determine the rotation.
Since this does not help, have you tried hackpod's answer here:
getResources().getConfiguration().orientation
To get current device orientation try this:
getWindowManager().getDefaultDisplay().getRotation();
Simply check, the height and width of the image. If the height is less than your width, it means the user was holding the phone horizontally, so you have to rotate it.
I am currently using android orientation sensor to solve this problem and its working perfectly. Till someone gives a better working method , am accepting this as answer for other who have the same question.

WebView rotates even if manifest declares that activity will handle orientation changes

I have a WebView embedded in a Fragment. In the manifest file, I have declared that the activity will handle orientation changes:
android:configChanges="orientation|screenSize"
and in the Activity, I have over-ridden onConfigurationChanged() in order to capture the orientation.
I thought this means that we have to explicitly take care of any changes in the screen orientation. But what I see is that the screen is still rotated (although the activity is not re-created).
If I use the following line:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
it does prevent the screen from being rotated, but I don't get the rotation event.
So, in short, I don't want the system to rotate the screen, and at the same time, I want to get an event from the system that the orientation has changed from portrait to landscape.
Thanks,
Rajath
If you override onConfigurationChanged(Configuration newConfig), you should be able to handle the changes.
Thanks for editing the question, what you're after is now clear to me. I've got two suggestionsScreen orientation (i.e. portrait, landscape, reversePortrait, reverseLandscape, etc) just depends on orientation of the device in 3D space. So one idea is to capture the 3D orientation of the device yourself, which is the same information that the operating system uses to make the screen orientation decision. This means that you need to capture the accelerometer and the magnetic field sensor readings. One example of capturing that information is in my answer to Android Compass that can Compensate for Tilt and Pitch.Alternatively, you might try setting up a dummy activity that exists purely to capture the screen orientation information. That activity could sit on the activity stack behind your main activity. Although I'm not sure whether activities that aren't on top of the activity stack are notified of screen orientation changes.

Fastest way to update the activity for all the orientations

In my android Activity I need to update the camera preview orientation and the UI according to the device Orientation. And I need all orientations for my activity namelyPortrait,Landscape,Landscape reverseandportrait reverse`.
I use the Android API demo for the camera preview. I found that when I use fullSensor for orientation setting in my AndroidManifest.xml and call the setCameraDisplayOrientation() on surfaceChanged() method of the Preview class(I am referring to the CameraPreview.java in the API demo sources) I get the desired behavior.
But there is a big delay on each orientation switch. When I set the orientation to Landscape in the manifest the preview is smooth but the UI is not updating according to the device orientation. I need the orientation to be set to Landscape and the UI to change according to all the four orientations namely Portrait up, Landscape right, Landscape left and Portrait down. What is the optimal way of achieving the same with good performance?

Check a screen orientation when it's fixed by manifest file

I have an camera activity in my app, where user can make a photo. For some reasons it's fixed to landscape mode(by the screenOrientation in manifest). All works perfectly but I need to know - what screen mode on picture? User can rotate device and try to make a photo in portrait mode.
I've tried to use getRotation method but it returnes only 0 degrees, cause i'm set to portrait screenOrientation.
I want to ask - is there some ways to solve this problem?
Thanks for any help!
Use the orientation sensor, ain't as difficult as you might think.
http://www.workingfromhere.com/blog/2009/03/30/orientation-sensor-tips-in-android/
Android phone orientation overview including compass

Android activity rotation locked in manifest, but need to grab the orientation event

I am designing a application that uses the camera. I want it locked it landscape mode and just have the pictures(for example, a handle to change between camera and video) rotate. I don't want to launch onCreate() again. This is so the camera/video logo does not appear sideways on the screen.
I have put this in the manifest:
android:screenOrientation="landscape"
This keeps the screen from turning. However, I can't seem to figure out how I get the event that tells me when turn the images. I have seen camera apps do this.
Thanks!
If you've locked your activity using manifest, the only workable solution to track orientation I saw was using OrientationEventListener.
You'll need to convert degrees to correct orientation yourself. But that is relatively easy task.

Categories

Resources