Stop camera preview from resizing

Viewed 221

I have a camera preview with a TextView at the bottom of it that looks like this:

enter image description here

Code:

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/preview_container"
            android:layout_height="match_parent"
            android:layout_width="match_parent">

            <androidx.camera.view.PreviewView
                android:id="@+id/preview"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintDimensionRatio="W,4:3"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"/>

            <TextView
                android:id="@+id/test"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintBottom_toBottomOf="@+id/preview"
                app:layout_constraintStart_toStartOf="@id/preview"
                android:text="TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST" />

        </androidx.constraintlayout.widget.ConstraintLayout>

The above is perfect in portrait mode. But, when the device is rotated, I want the TextView (or any other views) to be at the "new bottom" of the preview. Here's my failed attempt at it:

I simply add the android:rotation values programmatically after rotation. The TextView is successfully moved to the "new bottom" but the problem is that the camera preview changed even though I want it to remain intact.

enter image description here

Failed attempt code:

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/preview_container"
            android:layout_height="match_parent"
            android:layout_width="match_parent"

            android:rotation="90"> <!-- new -->

            <androidx.camera.view.PreviewView
                android:id="@+id/preview"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintDimensionRatio="W,4:3"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"

                android:rotation="-90"/> <!-- new -->

            <TextView
                android:id="@+id/test"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintBottom_toBottomOf="@+id/preview"
                app:layout_constraintStart_toStartOf="@id/preview"
                android:text="TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST" />

        </androidx.constraintlayout.widget.ConstraintLayout>

What should I do instead?

1 Answers

Have you tried to use android:orientation="horizontal"? I would also pay attention to the documentation:

You can constrain a widget on the horizontal and vertical axis:

Horizontal Axis: left, right, start and end sides

Vertical Axis: top, bottom sides and text baseline

Android Docs

Related