Home > Enterprise >  Set view to be at bottom of camera preview?
Set view to be at bottom of camera preview?

Time:06-09

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 my 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 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>

CodePudding user response:

Sorry i dont have enough reputation for comment so i will try answer directly

This problem occurs because you set your textview bottom to bottom with camera preview while use ConstraintLayout. I can give you two option for solve this,

first change textview from app:layout_constraintBottom_toBottomOf to app:layout_constraintTop_toBottomOf.

Second try for wrap it with linearlayout and set orientation to vertical.

CodePudding user response:

Try this:

<?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">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@ id/root"
    android:background="@color/black"
    android:orientation="vertical"
    tools:context=".ui.activities.MainActivity">

    <FrameLayout
        android:id="@ id/main_frame"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="1"
        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" />
        </androidx.constraintlayout.widget.ConstraintLayout>


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_gravity="bottom"

            android:textColor="#ffffffff"
            android:text="Text Test" />
    </FrameLayout>
   
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
  • Related