7

I have an Android application using LinearLayout as main layout with a SurfaceView filled by camera preview. In this I inflate another LinearLayout with three Buttons and a custom TextView. I would like the camera preview to stay always in Landscape orientation and the overlay layout changing according to the device orientation.

I tried setting android:screenOrientation="landscape" in the manifest for the activity but then (of course) also the inflated layout stays always fixed, while not setting the android:screenOrientation property also the camera preview rotate, slowing down the app and showing strange form factors of the preview. Here the relevant code for the layout:

private void setupLayout()
{
    setContentView(R.layout.main);
    getWindow().setFormat(PixelFormat.UNKNOWN);

    // Release camera if owned by someone else
    if (camera != null)
        releaseCamera();

    surfaceView = (SurfaceView) findViewById(R.id.camerapreview);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    controlInflater = LayoutInflater.from(getBaseContext());
    View viewControl = controlInflater.inflate(R.layout.control, null);
    LayoutParams layoutParamsControl = new LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    this.addContentView(viewControl, layoutParamsControl);


    buttonGetCollectingData = (Button) findViewById(R.id.getcolldata);
    buttonGetCollectingData.setOnClickListener(new Button.OnClickListener()
    {
        public void onClick(View arg0)
        {
            ...
        }
    });

    btnBackHome = (Button) findViewById(R.id.btnBackHome);
    btnBackHome.setOnClickListener(new Button.OnClickListener()
    {
        public void onClick(View arg0)
        {
            ...
        }
    });

    autoFitTextViewMainMsg = (AutoFitTextView) findViewById(R.id.autoFitTextViewMainMsg);

    buttonTakePicture.setOnClickListener(new Button.OnClickListener()
    {

        public void onClick(View arg0)
        {
            ...
        }
    });
}

Any idea on how to accomplish this would be really appreciated!

Andre
  • 489
  • 1
  • 6
  • 17
  • I also had such problem in my app. This answer helped me a lot: http://stackoverflow.com/a/5183690/1552622 – makovkastar May 24 '13 at 11:49
  • @makovkastar I tried the code you suggested but it seems this is not what I'm looking for... – Andre May 24 '13 at 12:25
  • This codes doesn't re-creates activity during rotation and rotates only overlay items. In this case only icons are rotated but you can rotate all what you need. Or it isn't what you asked for? – makovkastar May 24 '13 at 12:28
  • @makovkastar I've seen I can get the orientation but this example is helpful to change the rotation of the single components (as you wrote), like the base android Camera application. Indeed I would like the main.xml to be always in landscape mode, while the control.xml layout to follow the device orientation, such that the buttons are always placed in the same position independently from the device orientation (ex. buttonTakePicture always in the bottom part, btnBackHome always in the top right corner, ...) – Andre May 24 '13 at 12:43

1 Answers1

8

You can make use of custom orientation event listener to get the orientation and set your UI as according.

First, create a class CustomOrientationEventListener

public abstract class CustomOrientationEventListener  extends OrientationEventListener {


private static final String TAG = "CustomOrientationEvent";
private int prevOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;
private Context context;
private final int ROTATION_O    = 1;
private final int ROTATION_90   = 2;
private final int ROTATION_180  = 3;
private final int ROTATION_270  = 4;
private int rotation = 0;

public CustomOrientationEventListener(Context context) {
    super(context);
    this.context = context;
}

@Override
public void onOrientationChanged(int orientation) {

    if (android.provider.Settings.System.getInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 0) // 0 = Auto Rotate Disabled
        return;
    int currentOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;
    if (orientation >= 340 || orientation < 20 && rotation != ROTATION_O) {
        currentOrientation = Surface.ROTATION_0;
        rotation = ROTATION_O;

    } else if (orientation >= 70 && orientation < 110 && rotation != ROTATION_90) {
        currentOrientation = Surface.ROTATION_90;
        rotation = ROTATION_90;

    } else if (orientation >= 160 && orientation < 200 && rotation != ROTATION_180) {
        currentOrientation = Surface.ROTATION_180;
        rotation = ROTATION_180;

    } else if (orientation >= 250 && orientation < 290 && rotation != ROTATION_270) {
        currentOrientation = Surface.ROTATION_270;
        rotation = ROTATION_270;
    }

    if (prevOrientation != currentOrientation && orientation != OrientationEventListener.ORIENTATION_UNKNOWN) {
            prevOrientation = currentOrientation;
            if (currentOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) {
                onSimpleOrientationChanged(rotation);
        }
    }
}

public abstract void onSimpleOrientationChanged(int orientation);


}

Then add below line to the Android manifest under your activity tag

  android:configChanges="orientation|keyboardHidden|screenSize"

Make sure that you do not add android:screenOrientation property in manifest for that activity.

Then use the CustomOrientationEventListener class inside onCreate of your camera activity

public class YourActivity extends AppCompatActivity {

  private CustomOrientationEventListener customOrientationEventListener;

  final int ROTATION_O    = 1;
  final int ROTATION_90   = 2;
  final int ROTATION_180  = 3;
  final int ROTATION_270  = 4;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

       .....

      customOrientationEventListener = new 
 CustomOrientationEventListener(getBaseContext()) {
          @Override
          public void onSimpleOrientationChanged(int orientation) {
              switch(orientation){
                  case ROTATION_O:
                      //rotate as on portrait
                 yourButton.animate().rotation(0).setDuration(500).start();
                      break;
                  case ROTATION_90:
                      //rotate as left on top
                 yourButton.animate().rotation(-90).setDuration(500).start();
                      break;
                  case ROTATION_270:
                      //rotate as right on top
                 yourButton.animate().rotation(90).setDuration(500).start();
                      break;
                  case ROTATION_180:
                      //rotate as upside down
                 yourButton.animate().rotation(180).setDuration(500).start();
                      break;

              }
          }
      };

  }

  @Override
  protected void onResume() {
      super.onResume();
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
      customOrientationEventListener.enable();
  }

  @Override
  protected void onPause() {
      super.onPause();
      customOrientationEventListener.disable();
  }

  @Override
  protected void onDestroy() {
      super.onDestroy();
      customOrientationEventListener.disable();
  }
}
Tom Saju
  • 141
  • 2
  • 7