Set view to always be at bottom of camera preview?

177 views
Skip to first unread message

Steve

unread,
Jun 8, 2022, 10:39:06 AM6/8/22
to Android CameraX Discussion Group
I'd like to set the position of the TextView to **always** be at the bottom of the camera preview.

This is not the case with the layout below. When the user changes the aspect the ratio of the camera preview (ex: from 16:9 to 4:3), the TextView remains in the same screen position and is no longer at the bottom of the preview.

How do I set the position of the TextView to **always** be at the bottom (left) of the camera preview?

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.coordinatorlayout.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.activities.MainActivity">
   
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/root"
            android:background="@color/black"
            tools:context=".ui.activities.MainActivity">
   
            <FrameLayout
                android:id="@+id/main_frame"
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:paddingTop="8dp">
   
                <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="H,9:16"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintEnd_toEndOf="parent" />
   
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
   
                        app:layout_constraintBottom_toBottomOf="@+id/preview"
   
                        android:textColor="#ffffffff"
                        android:text="Text Test" />
   
                </androidx.constraintlayout.widget.ConstraintLayout>
            </FrameLayout>
        </RelativeLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

Charcoal Chen

unread,
Jun 10, 2022, 4:01:22 AM6/10/22
to Android CameraX Discussion Group, Steve
Hi,

I guess the problem is caused by that the PreviewView is always set to a fixed ratio but not changed along with the real preview content ratio:


    app:layout_constraintDimensionRatio="H,9:16"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"

The TextView is put to bottom of the PreviewView by the ConstraintLayout:

    app:layout_constraintBottom_toBottomOf="@+id/preview"

If the view dimension of PreviewView is not changed along with the real preview content ratio, the position of the TextView won't change correspondingly.

Maybe you can try to change the PreviewView ratio dynamically according to the aspect ratio of camera preview to fix the problem:

    app:layout_constraintDimensionRatio="H,9:16"
Reply all
Reply to author
Forward
0 new messages