Search code examples
androidandroid-studiotextviewimageviewandroid-constraintlayout

Problem placing TextView on ImageView using ConstraintLayout


I am using Constraintlayout and I want to place my Textview on my Imageview, it is supposed to look like this after I'm done: https://i.sstatic.net/mYAws.png but each time I add constraints to the texts(Vertically and horizontally), the texts disorganizes and goes totally from how I want it to be, Sometimes even shifting my images. I have tried severally to adjust it well to no avail

My layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 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:fillViewport="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".FirstFragment">

        <!-- TODO: Update blank fragment layout -->


        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="Current"
            android:textColor="#FF3D19"
            android:textSize="24sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="358dp"
            android:layout_height="337dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView3"
            app:srcCompat="@drawable/orange_panel" />

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="146dp"
            android:layout_height="151dp"
            android:layout_marginStart="45dp"
            android:layout_marginTop="7dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView3"
            app:srcCompat="@drawable/green_panel" />

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="146dp"
            android:layout_height="151dp"
            android:layout_marginTop="6dp"
            android:layout_marginEnd="44dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView3"
            app:srcCompat="@drawable/pink_panel" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tonight"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            tools:layout_editor_absoluteX="89dp"
            tools:layout_editor_absoluteY="415dp" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tomorrow"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            tools:layout_editor_absoluteX="254dp"
            tools:layout_editor_absoluteY="415dp" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.core.widget.NestedScrollView>

Solution

  • The answer is pretty straightforward. First you should delete this lines from both text views.

    tools:layout_editor_absoluteX="254dp"
    tools:layout_editor_absoluteY="415dp"
    

    Then all you need to do is add constraints like this. Below are constraints for your image

    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    

    And the constraints for your text view would be like this.

    app:layout_constraintEnd_toEndOf="@+id/image_view"
    app:layout_constraintStart_toStartOf="@+id/image_view"
    app:layout_constraintTop_toTopOf="@+id/image_view"
    

    Of course you can add different constraints for the image because i do not know how do you want to position the image view in the app. But if you want a text view to be inside the image like in the link you sent then the constraints for the text view will be always like this.