Search code examples
androidandroid-layoutandroid-imageviewtextview

Textview in center below of imageview Android


how are you going today.

I want to know something where i stuck today.I am getting json of image and text in a list and my image is circular.When i get image with text it looks so bad because text is not in center below imageview.I searched a lot on this but i didn't find.I just found gravity "center" and center in parent "true",i tried both of these but i did not got solution.My code is:-

<RelativeLayout 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"
tools:context=".MainActivity>


<de.hdodenhof.circleimageview.CircleImageView
    android:layout_width="110dp"
    android:layout_height="110dp"
    android:layout_marginTop="70dp"
    android:layout_marginStart="15dp"
    android:id="@+id/img_design" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/img_design"
    android:id="@+id/design_color"
    android:text=" "
    android:layout_centerInParent="true"
    android:textStyle="bold"
    android:textSize="15sp" />
</RelativeLayout>

I want like this automatically text set in middle of image because length is not fixed of text:

         IMAGEVIEW
           text

Please guide me,suggest me and give solution of this problem and if you have any query in my question then please ask me.Thank you.


Solution

  • You can replace your layout with below XML code.

    Here no need to take extra layout.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true">
    
    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/img_design"
        android:layout_width="110dp"
        android:layout_height="110dp"
        android:layout_marginStart="15dp"
        android:layout_marginEnd="15dp"
        android:layout_marginTop="10dp"
        tools:src="@drawable/profile_sidemenu"/>
    
    <TextView
        android:id="@+id/design_color"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_below="@+id/img_design"
        android:layout_centerInParent="true"
        tools:text="This is sample text"
        android:textSize="15sp"
        android:textStyle="bold" />
    </RelativeLayout>
    

    Output:

    Layout of above code