Vivek Mishra
Vivek Mishra

Reputation: 5705

TextView Center Gravity not working

I have tried all the possible combinations that I can think of but I am not able to position textview correctly. Here is the screenshot of the problem

Screenshot

From the first row of the image of the A Symbol it is clearly visible that margin from top is more than that of bottom.

This is the xml layout of the row

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#191e1e">

    <RelativeLayout
        android:id="@+id/client_initial_layout"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_centerVertical="true"

        android:layout_marginLeft="10dp"
        android:background="#1f97e5">
        <!--  android:layout_marginLeft="10dp"
          android:layout_marginRight="10dp"
          android:layout_marginTop="20dp"-->
        <TextView
            android:id="@+id/client_initial"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"


            android:layout_centerHorizontal="true"

            android:text="X"
            android:textColor="#ffffff"
            android:textSize="60sp" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/lin_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_toRightOf="@+id/client_initial_layout"
        android:orientation="vertical">

        <TextView
            android:id="@+id/Client_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Guest"
            android:textColor="#bcc3c4"
            android:textSize="16sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp">

            <TextView
                android:id="@+id/text_contacted"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Contacted:-"
                android:textColor="#797878"
                android:textSize="10sp" />

            <TextView
                android:id="@+id/contacted"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=" Username"
                android:textColor="#797878"
                android:textSize="10sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp">

            <TextView
                android:id="@+id/text_product"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Product:-"
                android:textColor="#797878"
                android:textSize="10sp" />

            <TextView
                android:id="@+id/product"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=" Product Name"
                android:textColor="#797878"
                android:textSize="10sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp">

            <TextView
                android:id="@+id/text_contact_type"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Contact Type:-"
                android:textColor="#797878"
                android:textSize="10sp" />

            <TextView
                android:id="@+id/contact_type"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=" Dispute"
                android:textColor="#797878"
                android:textSize="10sp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/lin_created"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginTop="10dp">

            <TextView
                android:id="@+id/text_Created_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Created Date:-"
                android:textColor="#bcc3c4"
                android:textSize="8sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/request_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" 2015-09-11-19:22:08"
                android:textColor="#bcc3c4"
                android:textSize="8sp"
                android:textStyle="bold" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="3dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="Request Date:-"
                    android:textColor="#bcc3c4"
                    android:textSize="8sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/create_date"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=" 2015-09-11"
                    android:textColor="#bcc3c4"
                    android:textSize="8sp"
                    android:textStyle="bold" />
            </LinearLayout>
        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

I have tried all alignment related to center but it has no effect. But the preview shown in xml editor is perfect and is in center but not when I run the app and I am also not setting any kind of margin from java code

Upvotes: 0

Views: 2734

Answers (5)

Shishram
Shishram

Reputation: 1516

Try using below code

Explanation:- as you have defined width & height so your layout wont show text in center of your layout so You have to make layout and TextView as wrap_content and try giving padding to your TextView and it should work!

<RelativeLayout
            android:id="@+id/client_initial_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"


            android:layout_marginLeft="10dp"
            android:background="#1f97e5">

            <TextView
                android:id="@+id/client_initial"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:paddingLeft="20dp"
                android:paddingRight="20dp"

                android:text="A"
                android:textColor="#ffffff"
                android:textSize="60sp" />
        </RelativeLayout>

Upvotes: 0

Haniyeh Khaksar
Haniyeh Khaksar

Reputation: 804

use it:

 <RelativeLayout
    android:id="@+id/client_initial_layout"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_centerVertical="true"

    android:layout_marginLeft="10dp"
    android:background="#1f97e5">
    <!--  android:layout_marginLeft="10dp"
      android:layout_marginRight="10dp"
      android:layout_marginTop="20dp"-->
    <TextView
        android:id="@+id/client_initial"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="X"
        android:textColor="#ffffff"
        android:textSize="60sp" />
</RelativeLayout>

Upvotes: 0

Rathna Kumaran
Rathna Kumaran

Reputation: 113

try centerVertical

<RelativeLayout
        android:id="@+id/client_initial_layout"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_centerVertical="true"

        android:layout_marginLeft="10dp"
        android:background="#1f97e5">
        <!--  android:layout_marginLeft="10dp"
          android:layout_marginRight="10dp"
          android:layout_marginTop="20dp"-->
        <TextView
            android:id="@+id/client_initial"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"


            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="X"
            android:textColor="#ffffff"
            android:textSize="60sp" />
    </RelativeLayout>

Upvotes: 0

Amit Vaghela
Amit Vaghela

Reputation: 22945

changed to Linearlayout and with minor changes got this result,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#191e1e">

    <LinearLayout
        android:id="@+id/client_initial_layout"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_gravity="center"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:background="#1f97e5"
        android:gravity="center">

        <TextView
            android:id="@+id/client_initial"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="X"
            android:textColor="#ffffff"
            android:textSize="60sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lin_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/Client_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Guest"
            android:textColor="#bcc3c4"
            android:textSize="16sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp">

            <TextView
                android:id="@+id/text_contacted"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Contacted:-"
                android:textColor="#797878"
                android:textSize="10sp" />

            <TextView
                android:id="@+id/contacted"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=" Username"
                android:textColor="#797878"
                android:textSize="10sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp">

            <TextView
                android:id="@+id/text_product"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Product:-"
                android:textColor="#797878"
                android:textSize="10sp" />

            <TextView
                android:id="@+id/product"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text=" Product Name"
                android:textColor="#797878"
                android:textSize="10sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp">

            <TextView
                android:id="@+id/text_contact_type"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Contact Type:-"
                android:textColor="#797878"
                android:textSize="10sp" />

            <TextView
                android:id="@+id/contact_type"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=" Dispute"
                android:textColor="#797878"
                android:textSize="10sp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/lin_created"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginTop="10dp">

            <TextView
                android:id="@+id/text_Created_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Created Date:-"
                android:textColor="#bcc3c4"
                android:textSize="8sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/request_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" 2015-09-11-19:22:08"
                android:textColor="#bcc3c4"
                android:textSize="8sp"
                android:textStyle="bold" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="3dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="Request Date:-"
                    android:textColor="#bcc3c4"
                    android:textSize="8sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/create_date"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=" 2015-09-11"
                    android:textColor="#bcc3c4"
                    android:textSize="8sp"
                    android:textStyle="bold" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

Upvotes: 1

AmeeJoshi
AmeeJoshi

Reputation: 201

change your relative layout like below, it will work

 <RelativeLayout
        android:id="@+id/client_initial_layout"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_centerVertical="true"

        android:layout_marginLeft="10dp"
        android:background="#1f97e5">
        <!--  android:layout_marginLeft="10dp"
          android:layout_marginRight="10dp"
          android:layout_marginTop="20dp"-->
        <TextView
            android:id="@+id/client_initial"
            android:layout_width="70dp"
            android:layout_height="70dp"


       android:gravity="center"

            android:text="X"
            android:textColor="#ffffff"
            android:textSize="60sp" />
    </RelativeLayout>

Hope this answer help full to you

Upvotes: 0

Related Questions