7

I am building a camera app which makes use of the camera preview. In order to use the full screen for the preview, I locked the activity with

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Now I have following problem: whenever I render a form or I have to show a dialog, it always shows up in landscape mode, even when the user is using his/her device in portrait mode. I tried to rotate edittext views by using transform animations, and they are correctly rotated, but still I am not able to focus them any more, and the keyboard always shows up in landscape too :(

Do you know ANY way of finding a way round here?

IMPORTANT: detecting orientation is NOT the issue here. The question is: how do I rotate the forms/views/viewgroups and keyboard in a way that they are still usable.

I know that it has to be possible somehow: samsung's camera app (which comes with the galaxy S3) is able to rotate views without rotating the preview, so it has to be possible! see:

http://www.letsgomobile.org/images/reviews/0186/galaxy-s3-camera.jpg

http://cdn2.mos.techradar.futurecdn.net//art/mobile_phones/Samsung/GalaxyS3/Galaxy%20Fire/Samsung_Galaxy_S3_25-580-100.JPG

stoefln
  • 14,498
  • 18
  • 79
  • 138
  • Have you tried starting new activity as a dialog (with android:theme="@android:style/Theme.Dialog" in activity description) instead of using the actual dialog? I suspect that this fake dialog should react to orientation changes in the usual manner as you are locking only your camera activity in landscape orientation. Unfortunately I cannot try it myself at the moment. – Audrius Dec 20 '12 at 18:24
  • no haven't tried yet. would be great if that works... – stoefln Dec 20 '12 at 21:18
  • does not work- just tried it: dialog activity is displayed in landscape too, no matter how I am rotating the device – stoefln Dec 27 '12 at 07:43
  • How do you show the preview of the camera? Maybe you should not lock the orientation but instead find a way to always use the full screen for the preview while the rest of the app can move around...? – Matthieu Dec 27 '12 at 14:59
  • thought about that too. I am using a SurfaceHolder to attach the preview: mSurfaceView = new SurfaceView(context); addView(mSurfaceView); mSurfaceView.getHolder().addCallback(this); And then, in surfaceCreated() Callback: camera.setPreviewDisplay(holder); – stoefln Dec 27 '12 at 15:06

6 Answers6

2

By setting this property

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

You are locking your screen to LANDSCAPE only, after this even if user is holding device in portrait mode, it won't be rotated to portrait mode. You can do one thing as per my understanding of your problem.

android:configChanges="orientation"

In menifest file add this above property to your Activity. And in Activity class override onConfigurationChange method, Where you can handle orientation changes according to your need, instead of locking Activity to Landscape.

If i understood your problem in correct way then it may help you.

Suresh
  • 7,785
  • 1
  • 16
  • 28
  • I know that I can do that. But, as I already wrote, I didn't manage to render a rotated edittext form (or button) which is still working, and the keyboard always shows up in landscape mode. – stoefln Dec 24 '12 at 16:48
2

The Samsung Camera Application is using an Orientation change Listener with the orientation sensor. If the user block the device rotation from settings the rotation still happen.

You will need a create your full custom widgets, so you cannot use build in Dialog, you have to show a CustomDialog ....

The basic idea is to use OrientationEventListener, and SensorManager. On the Device Rotation you have to updates your UI and rotate views with the new orientation. Here is a tutorial on using OrientationEventListener How to use OrientationEventListener On most camera app, the activity is still blocked on LANDSCAPE. All camera application do this trick to ease the handling of the SurfaceView size change with the rotation.

Anis BEN NSIR
  • 2,555
  • 20
  • 29
  • I already implemented the OrientationEventListener- that's not the issue here... However, so what you are saying is that I can't use the standard ScrollView, Edittext and other stuff? Do I really have to write my own? The samsung widgets (at least the scrollviews) look like standard android views. And do you think there is any chance to rotate the keyboard? – stoefln Dec 27 '12 at 15:01
  • You have to implement both orientation (layout_portrait and layout land). after that when the orientation cames to 0, you have to set portrait, and when the rotation change to (270 +-20) you have to remove the views and inflate the landscape, and attach it. – Anis BEN NSIR Dec 27 '12 at 21:42
  • the keybord will still on Land, other wise you have to handle Orientation using Activity onChange and register as sensor. – Anis BEN NSIR Jan 01 '13 at 20:14
0

You have to override behaviour of the activity by rotating your device, you have to add orientationChange in your mafinest and then add listener for this event and manually rotate your views.

And if you use this method all dialog etc (system views) will appear in a right way.

Check this: http://developer.android.com/reference/android/app/Activity.html#ConfigurationChanges

And this: How to handle screen orientation change when progress dialog and background thread active? (second answer i think)

Community
  • 1
  • 1
dilix
  • 3,761
  • 3
  • 31
  • 55
  • dont get it. what do you mean by "You have to override behaviour of the activity by rotating your device"? – stoefln Dec 18 '12 at 15:16
  • If you said in your manifest that you will handle configChanges:orientaion - you will be able to override onConfigurationChanged(Configuration) or something like this and in this method youo can manually rotate only neccesarry views – dilix Dec 19 '12 at 07:37
  • yes, but as i already wrote, rotating the edittexts or the keyboard programmatically does not work. do you know how to do that? – stoefln Dec 19 '12 at 08:07
0

you better try to use another activity as a dialog and set its properties (orientation and theme) in menifest and use it.

Adnan Amjad
  • 2,553
  • 1
  • 20
  • 29
  • does not work- just tried it: dialog activity is displayed in landscape too, no matter how I am rotating the device – stoefln Dec 27 '12 at 07:42
0

In your Manifest file give the below properties to your camera preview activity android:configChanges="keyboardHidden|orientation" for more read this

<activity
        android:name=".Your_Activity_Name"
        android:configChanges="keyboardHidden|orientation"
        android:label="@string/app_name" >
        <intent-filter>
            <!-- Your filter attributes if any. -->
        </intent-filter>
    </activity>

And in your activity class override onConfigurationChanged & do the needful(as of your need) in concern screen orientation mode(e.g., landscape or portrait) like this_

@Override
 public void onConfigurationChanged(Configuration newConfig) {
     super.onConfigurationChanged(newConfig);

     // Checks the orientation of the screen
     if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
         Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        //Your code to do the other things in landscape mode...

     } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
         Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
       //Your code to do the other things in portrait mode...

     }
 }

I hope this will help you to get rid of your problem.

Rupesh Yadav
  • 12,096
  • 4
  • 53
  • 70
  • cool- didn't know that its possible to rotate a Toast message. However, I still don't know how to rotate form elements – stoefln Dec 27 '12 at 07:38
  • android will take care of this when screen orientation change. it will call `onConfigurationChanged` every time when screen orientation changes. – Rupesh Yadav Dec 27 '12 at 07:47
  • no, it will not take care of rotating, since I call setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); in order to lock the camera preview to landscape -> android will lock the whole activity – stoefln Dec 27 '12 at 07:52
  • yes, you are right it will not called be`coz you already have requested orientation. [this](http://developer.android.com/guide/topics/graphics/hardware-accel.html) may help you. – Rupesh Yadav Dec 27 '12 at 09:28
  • How should that help me? Could you elaborate on that? As already said: I know how to capture the orientation, I just dont know how to rotate views/forms and keyboard. – stoefln Dec 27 '12 at 15:10
0

afaik: The rotation is achieved by catching the orientation events and inflating the screen with the new layout according to that orientation.

  1. inflate in landscape
  2. rotation occurs
  3. inflate the parts of the screen you want to change i.e for example we inflate the holder view for the buttons with the new layout for the portrait buttons instead of landscape.

You must have your layout cut into parts you add together on runtime.

Another way to achieve this is by using a surface views for camera preview for instance and using another layout to hold the surface and to add on top of it with framelayout

MaTriXy
  • 1,659
  • 1
  • 20
  • 27
  • I don't want the app to re-inflate. Just think of images which are shot in 45° mode- you certainly dont want to rebuild the whole interface each time the user rotates his/her phone between landscape and portrait – stoefln Dec 27 '12 at 15:09