How to center text on TextView's background

I have developing an application - listview with many items. In each item I have textview with background. How I can move my text to the center of textview ?

My item code:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="75dp"
    android:layout_marginTop="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/lesson_name"
        android:id="@+id/tv_lesson"
        android:layout_gravity="center_horizontal|top"
        android:textAlignment="center"
        android:textStyle="normal|bold"
        android:textSize="20sp"
        tools:targetApi="jelly_bean_mr1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Время"
        android:id="@+id/tv_time"
        android:layout_gravity="center"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="10dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/teacher_name"
        android:id="@+id/tv_teacher"
        android:layout_gravity="center_horizontal|bottom"
        android:textStyle="italic" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Кабинет"
        android:id="@+id/tv_cab"
        android:layout_gravity="center_horizontal|bottom"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="17dp" />

    <TextView
        android:layout_width="45dp"
        android:layout_height="45dp"
        **android:id="@+id/tv_number"**
        android:layout_gravity="center_vertical"
        android:background="@drawable/oval"
        android:textIsSelectable="false"
        android:textStyle="normal|bold"
        android:textSize="40sp"
        android:layout_marginLeft="10dp"
        android:text="1"
        android:textColor="#ffffff"
        tools:targetApi="jelly_bean_mr1"
        android:textAlignment="inherit" />
</FrameLayout>

My textiew:

my textview

Any suggestions?

P.S I already tried android gravity = center, my textview moves to the center of the list item

Upvotes: 3

Views: 2389

Answers (6)

earthw0rmjim
earthw0rmjim

Reputation: 19417

Add the following attribute to your TextView:

android:gravity="center"

Should look something like this:

<TextView
    android:layout_width="45dp"
    android:layout_height="45dp"
    android:layout_gravity="center_vertical"
    android:gravity="center"
    android:layout_marginStart="10dp"
    android:layout_marginLeft="10dp"
    android:background="@drawable/oval"
    android:textIsSelectable="false"
    android:textStyle="normal|bold"
    android:textSize="40sp"
    android:text="1"
    android:textColor="#000000"/>

Upvotes: 7

Jay Rathod
Jay Rathod

Reputation: 11245

Change your Text View as this.

android:layout_gravity property will move your full Text View. And android:gravity will only move your content which is inside that Text View that is the main difference between them.

<TextView
        android:id="@+id/tv_number"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_gravity="center_vertical"
        android:gravity="center"
        android:layout_marginLeft="10dp"
        android:background="#000000"
        android:text="2222"
        android:textAlignment="inherit"
        android:textColor="#ffffff"
        android:textIsSelectable="false"
        android:textSize="40sp"
        android:textStyle="normal|bold"
        tools:targetApi="jelly_bean_mr1" />

enter image description here

Upvotes: 1

Aditya Vyas-Lakhan
Aditya Vyas-Lakhan

Reputation: 13555

try this way,create one xml for background

test.xml

 <?xml version="1.0" encoding="utf-8"?>
    <selector
        xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape>
                <solid android:color="#f2f" />

                <size
                    android:height="150dp"
                    android:width="150dp" />

            </shape>
        </item>
    </selector>

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3"
        android:gravity="center"
        android:background="@drawable/test"
        android:textSize="15dp"
        android:textColor="#ffffff"
        />

</LinearLayout>

OUTPUT

enter image description here

Upvotes: 0

Rajendra
Rajendra

Reputation: 504

Basically this is because of the default font padding,to remove the default padding you should add the following code in your layout xml inside TextView: android:includeFontPadding="false" e.g.:

 <TextView
            android:id="@+id/tv_number"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:layout_gravity="center_vertical"
            android:gravity="center_vertical"
            android:layout_marginLeft="10dp"
            android:background="#000000"
            android:text="2222"
            android:textAlignment="inherit"
            android:textColor="#ffffff"
            android:textIsSelectable="false"
            android:textSize="40sp"
            android:textStyle="normal|bold"
            android:includeFontPadding="false"
            tools:targetApi="jelly_bean_mr1" />

Upvotes: 1

j2ko
j2ko

Reputation: 2539

Use folowing attribute:

 android:gravity="center"

Upvotes: 0

Arjun Issar
Arjun Issar

Reputation: 654

Change the 'gravity' tag of the TextView to "center"

Upvotes: 0

Related Questions