Android - TextView text is centered from its beginning letter

118 Views Asked by At

I have this problem, using TextView : (below is the screenshot link)

http://nicolas-barroso.legtux.org/textview.png

I would like the text to be centered in TextView, and after some research, its appears to me that I seem to use the right code, but it doesn't work...

This is the xml code I use :

<LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageButton
                    android:id="@+id/sad_button"
                    android:layout_width="60dp"
                    android:layout_height="60dp"
                    android:background="@drawable/round_button"
                    android:src="@drawable/icon_menu"/>

                <TextView
                    android:id="@+id/textView6"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="Sad"
                    android:textColor="@color/white"
                    android:textSize="15sp" />
            </LinearLayout>

Help would be welcome. Thanks

2

There are 2 best solutions below

0
On BEST ANSWER

Since your gravity on LinearLayout centered all childs. i remove gravity on textview and width and height wrap_content

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:gravity="center"
    android:orientation="vertical" >

<ImageButton
    android:id="@+id/sad_button"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:background="@drawable/round_button"
    android:src="@drawable/icon_menu"/>

<TextView
    android:id="@+id/textView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Sad"
    android:textColor="@color/white"
    android:textSize="15sp" />

</LinearLayout>

hope this can help

0
On

Use The Following line in TextView:

android:gravity="center"

& also use sp instead of dp