I am using ConstraintLayout that contains child view.
I want this child view centered but keeping aspect ratio.
Layout (landscape) variant:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/previewParent"
tools:context=".PeakerActivity">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="0px"
android:layout_height="0px"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintDimensionRatio="16:9"
android:background="@color/black"
android:gravity="center"
android:id="@+id/flexbox2">
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.constraintlayout.widget.ConstraintLayout>
Adding android:gravity="center" has no effect, child view is still not centered.
Edit: Added image, as you see, left purple side is greater than right side.

Try this, with
app:layout_constraintHorizontal_biaslayout attribute by keeping it half (0.5).Bias
The default when encountering such opposite constraints is to center the widget; but you can tweak the positioning to favor one side over another using the bias attributes: