I am creating a quizz game and struggling with constraining the image of my Imageview in the guidelines I have declared.
In Android Studio preview everything seems fine and as expected. Below is a printscreen of this expected result:
But when I run the app in the emulator (Pixel 3 XL API 29 on AVD Manager), I get the result below:
The image is not constrained by the vertical guidelines or somehow the vertical guidelines are never applied while the preview shows otherwise. I tried running it on different devices and the result is always the same.
Here is my XML code:
<?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:background="#427e83"
tools:context=".level1">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/level1completion"
android:layout_width="342dp"
android:layout_height="32dp"
android:layout_marginTop="32dp"
android:autofillHints=""
android:fontFamily="@font/sunrise_international"
android:textAlignment="center"
android:textSize="36sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/level1answer" />
<EditText
android:id="@+id/level1tail1answer"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="32dp"
android:background="@drawable/rounded_corners_edittext"
android:ems="10"
android:fontFamily="@font/sunrise_international"
android:hint="@string/type_your_answer"
android:inputType="textPersonName"
android:textColor="@android:color/darker_gray"
android:textColorHint="#45484b"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toBottomOf="@+id/level1imagetoguess" />
<ImageView
android:id="@+id/level1tail1imagetoguess"
android:contentDescription="@string/correct_answer"
android:src="@drawable/my_picture"
android:scaleType="fitStart"
android:adjustViewBounds="true"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="@+id/guideline3" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.18" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.9155" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.055" />
</androidx.constraintlayout.widget.ConstraintLayout>
I am not able to grasp what is going wrong here, even though I've read the documentation. As a last resort, I've tried to play with combinations of the following parameters:
- layout_constraintDimensionRatio
- scaleType
- adjustViewBounds
- layout_width
- layout_height
With no luck.
Any help would be greatly appreciated!
Thanks!