55

I have the following layout i'd like to make the textview appear in the center and middle of the view. How can i acheive this?

I've tried adjusting the various gravity attributes when looking at the layout in graphical view, but nothing seems to change it. I'd like the textview in the center which i've done but halfway down the view.

thanks matt.

<?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="match_parent"
    android:orientation="vertical"
    android:background="@drawable/carefreebgscaledalphajpg" >



    <TextView
        android:id="@+id/textviewdatefornocallspage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="You have no calls for "
        android:textSize="25sp" />

</LinearLayout>
turtleboy
  • 8,210
  • 27
  • 100
  • 199

14 Answers14

98

set the gravity to center in your linearLayout tag :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     android:gravity="center"
    android:background="@drawable/carefreebgscaledalphajpg" >

or use a RelativeLayout like this :

<?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="match_parent"
    android:background="@drawable/carefreebgscaledalphajpg" >



    <TextView
        android:id="@+id/textviewdatefornocallspage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="You have no calls for "
        android:textSize="25sp" />

</RelativeLayout>
Houcine
  • 24,001
  • 13
  • 56
  • 83
  • 1
    The RelativeLayout solution worked for me, but in the TextView I had to use `wrap_content` for both width and height – Jose_GD May 08 '14 at 20:57
  • @Jose_GD : in both of cases it will work for you :). in the case of using the attribute `match_parent` for width, just add the `layout_gravity="center"` . That's all ;) – Houcine May 08 '14 at 21:11
  • Doesn't work for me if I set the width to `match_parent` even after adding `layout_gravity="center"` – gegobyte Sep 13 '18 at 18:58
  • 1
    it's ‘android:gravity‘ not layout_gravity . the height should be match_parent too . – Houcine Sep 14 '18 at 17:00
14

Try this

simple i have tried to many answer but all answers are not working ......finally this methods works for me

                     <LinearLayout
                        android:layout_below="@+id/iv_divider"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:layout_marginTop="@dimen/_10dp">
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="@string/mycredits"
                            android:textColor="@color/colorGray"
                            android:gravity="center"
                            android:textSize="@dimen/_25dp" />
                    </LinearLayout>
Community
  • 1
  • 1
Sunil
  • 3,785
  • 1
  • 32
  • 43
7

Set android:gravity="center" attribute for TextView inside linear layout and keep the text view's height and width as wrap_content.

This Solution worked for me.

Community
  • 1
  • 1
Apoorv Singh
  • 1,295
  • 1
  • 14
  • 26
5
    <TextView
...
    android:layout_gravity="center"
....
/>
prozhyga
  • 439
  • 2
  • 7
3

Set width of your textview to wrap_content, and set your LinearLayout to

android:gravity="center_horizontal"
Mohammad Kholghi
  • 533
  • 2
  • 7
  • 21
Hardik Nadiyapara
  • 2,436
  • 2
  • 17
  • 24
2

Add android:gravity="center_horizontal" to your LinearLayout and alter your TextView's layout_width as android:layout_width="wrap_content"

<?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="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:background="@drawable/carefreebgscaledalphajpg" >



    <TextView
        android:id="@+id/textviewdatefornocallspage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="You have no calls for "
        android:textSize="25sp" />

</LinearLayout>
waqaslam
  • 67,549
  • 16
  • 165
  • 178
2

None of the other answers worked for me. I had to use android:textAlignment="center"

My layouts were as follows

<LinearLayout...
    <RelativeLayout...
        <RelativeLayout....
            <TextView
                android:layout_centerInParent="true"
                android:textAlignment="center"
nunya07
  • 65
  • 1
  • 8
2
 <LinearLayout
        android:id="@+id/service_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/card_view"
        android:layout_marginTop="10dp"
        android:background="@color/app_bar_background"
        android:orientation="vertical">

        <TextView
            android:id="@+id/list_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif"
            android:gravity="center"
            android:singleLine="true"
            android:text="@string/Services"
            android:textColor="@color/app_bar_text"
         />
    </LinearLayout>
1

If in linearlayout your orientation vertical, you can put the textview in the "horizontal center" by android:layout_gravity="center". For centering textview vertically you need to set layout_height of textview to match_parent and set android:gravity to "center". If you want to use more than one view in this layout, you should use RelativeLayout and set android:layout_centerInParent="true".

jumper0k
  • 2,166
  • 2
  • 22
  • 31
0

Sounds like you want android:layout_gravity for the TextView

see docs: developer.android.com

MrChaz
  • 1,065
  • 1
  • 8
  • 17
0

set android:gravity="center" in your Linear Layout

Himanshu itmca
  • 395
  • 5
  • 22
0
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".view.fragments.FragmentAncRecords">

    <TextView
        android:id="@+id/tvNoDataFound"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fontFamily="@font/poppins_regular"
        android:visibility="visible"
        android:gravity="center"
        android:text="@string/no_record_available"
        />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvAncRecords"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />



</LinearLayout>
0

center your texteView in RelativeLayout :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    />

</RelativeLayout>
-1

Use Relative Layout, it always gives more accurate and flexible layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/ic_launcher" />
</RelativeLayout>
Tahreem
  • 205
  • 5
  • 15