Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can put my title at top of the ImageView – Android Studio

I want to put my Title at top of the ImageView. However, I can not use layout_above to put my title at the top. When I trying this my text is invisible. The title in the last image is exactly the alignment I want. How can I do that ?

Code:
enter image description here

Actual:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

Expected:

enter image description here

>Solution :

You don’t need to wrap ImageView and TextView in another RelativeLayout.
It’s actually easier and more performant to put them both directly in ConstraintLayout. Then it’s only a question of setting the constraints correctly. Demo 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/above"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title above"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="@id/image"
        app:layout_constraintEnd_toEndOf="@id/image"/>

    <ImageView
        android:id="@+id/image"
        android:layout_width="400px"
        android:layout_height="400px"
        android:src="@color/purple_200"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/above"/>

    <TextView
        android:id="@+id/overlay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text overlay"
        app:layout_constraintStart_toStartOf="@id/image"
        app:layout_constraintTop_toTopOf="@id/image"/>

</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading