I have an ImageView where I need to place a TextView on top of on a specific location on the ImageView like this drawing
I have managed to do this, somewhat using the following xml:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/background"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:src="@drawable/imageBg"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test text"
android:textColor="#ff0000"
android:layout_marginRight="200dp"
android:layout_marginTop="205dp"
app:layout_constraintLeft_toLeftOf="@id/background"
app:layout_constraintRight_toRightOf="@id/background"
app:layout_constraintTop_toTopOf="@id/background" />
</androidx.constraintlayout.widget.ConstraintLayout>
This works fine on my one emulator test device, but as I expected when trying on other emulator devices, it doesn't scale well across multiple devices running different resolutions etc. due to the fixed margin dp values.
How can I achieve this in the best possible way which would work across multiple devices and now that the TextView would be on the same position everytime? I know that I can make use of dimens.xml and use different margin values for different device dimensions there, but I don't believe this would be the best approach.