'Dynamically adjust position of textview to always be at bottom of current camera preview

My layout is shown below. i want the textview to appear at the bottom of the camera preview. but this is not the case when the user changes the aspect the ratio of the preview, the textview remains in its place even when the camera preview has been changed by the user.

When the user changes the aspect ratio of the camera preview (ex: from 16:9 to 4:3), the textview is no longer at the bottom of the preview.

how do I make the textview dynamically adjust position to be at the bottom (left) of the current preview always?

<?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:id="@+id/text_view"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="center"
                    android:text="Hello world"
                    android:textSize="42sp"
                    android:textColor="@android:color/holo_green_dark"


                    app:layout_constraintBottom_toBottomOf="@+id/preview"
                    app:layout_constraintEnd_toEndOf="@id/preview"

                    android:layout_marginEnd="10sp"/>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source