How to center a textview inside a linearlayout
Asked Answered
P

14

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>
Perse answered 20/11, 2012 at 14:8 Comment(0)
N
97

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>
Nickelodeon answered 20/11, 2012 at 14:13 Comment(4)
The RelativeLayout solution worked for me, but in the TextView I had to use wrap_content for both width and heightNappe
@Nappe : 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 ;)Nickelodeon
Doesn't work for me if I set the width to match_parent even after adding layout_gravity="center"Mousetail
it's ‘android:gravity‘ not layout_gravity . the height should be match_parent too .Nickelodeon
G
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>
Glabrescent answered 15/3, 2017 at 7:49 Comment(1)
This should be the answerAllegorical
E
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.

Emend answered 9/6, 2016 at 6:47 Comment(0)
C
5
    <TextView
...
    android:layout_gravity="center"
....
/>
Crocket answered 20/11, 2012 at 14:15 Comment(0)
K
3

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

android:gravity="center_horizontal"
Kameko answered 20/11, 2012 at 14:13 Comment(0)
H
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>
Huck answered 20/11, 2012 at 14:10 Comment(0)
W
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"
Waist answered 19/6, 2019 at 22:31 Comment(0)
E
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>
Emery answered 15/2, 2023 at 10:50 Comment(0)
T
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".

Tannic answered 20/11, 2012 at 14:17 Comment(1)
Thanks, here worked by setting both height and weight to centerTiv
K
0

Sounds like you want android:layout_gravity for the TextView

see docs: developer.android.com

Kelso answered 20/11, 2012 at 14:14 Comment(0)
D
0

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

Delegacy answered 17/7, 2018 at 9:48 Comment(0)
N
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>
Nd answered 17/11, 2021 at 12:25 Comment(1)
Can you edit your answer to precise wy this should fix the issue ?Immature
H
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>
Houstonhoustonia answered 2/1, 2022 at 7:9 Comment(0)
I
-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>
Inge answered 20/11, 2012 at 14:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.