androidandroid-layouttextviewimageviewdrawable

How can I have an ImageView beside a multiline TextView and don't exceed the limit of parent?


I am trying to align an ImageView besides to a TextView, but I am having troubles getting the expected result.

The TextView can have more than one line, therefore I am using android:layout_width="0dp". However, because of this, ImageView is getting placed too far from TextView:

Result with layout_width="0dp"

If instead of using android:layout_width="0dp", I set layout_width="wrap_content", I get the expected result if there is enough screen space:

Result with layout_width="wrap_content"

In smaller screens the TextView is not split in multiple lines and hence exceeds the limits of the parent view:

Result with layout_width="wrap_content" in small screen

I also tried to use android:drawableLeft="@drawable/vc_estadio" attribute of TextView, but then the drawable is too big:

Result with android:drawableLeft="@drawable/vc_estadio"

Finally, I also tried resizing the drawable, but then I have the same problem than with android:layout_width="0dp". The image is too far from the text:

Result with android:drawableLeft="@drawable/vc_estadio" and resizing the drawable

What I expect is something like this in small screens:

Expected result in small screens

And this in bigger ones:

Expected result in big screens

The code I am using and trying to get working is this:

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

    <ImageView
        android:id="@+id/estadioImageView"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginEnd="8dp"
        android:src="@drawable/vc_estadio"
        app:layout_constraintBottom_toBottomOf="@+id/estadioTextView"
        app:layout_constraintEnd_toStartOf="@+id/estadioTextView"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/estadioTextView" />

    <TextView
        android:id="@+id/estadioTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:autoSizeTextType="uniform"
        android:gravity="center"
        android:maxLines="2"
        android:text="Estadio Pedro Garrido (Jerez de la Frontera)"
        android:textColor="@color/gray_dark"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/estadioImageView"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Thank you in advance for you help!


Solution

  • use android:layout_width="0dp" , as you are using wrap_content the constraint for width is not working so it's going outside when text is long instead of going to next line.

    Use ImageView to show image as in drawableLeft you don't have control over the size.

    Final code :

    <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="@color/white">
    
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/start_guideline"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.08" />
    
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/end_guideline"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.92" />
    
    
    <ImageView
        android:id="@+id/estadioImageView"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginEnd="8dp"
        android:src="@drawable/female_icon"
        app:layout_constraintBottom_toBottomOf="@+id/estadioTextView"
        app:layout_constraintEnd_toStartOf="@+id/estadioTextView"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toEndOf="@id/start_guideline"
        app:layout_constraintTop_toTopOf="@+id/estadioTextView" />
    
    <TextView
        android:id="@+id/estadioTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:autoSizeTextType="uniform"
        android:maxLines="2"
        android:text="Estadio Pedro Garrido (Jerez de la Frontera) "
        android:textAlignment="textStart"
        android:textColor="@color/gray"
        android:textSize="20sp"
        app:layout_constrainedWidth="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/end_guideline"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/estadioImageView"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    NOTE : UPDATE THE DRAWABLES USED AS I HAVE USED SAMPLE DRAWABLES

    Final Output : enter image description here