TextView in the center of the screen
Solution 1
If your font size is big enough, it might look like it's not centered, because of the font padding.
Try using the already mentioned properties combined with android:includeFontPadding
, something like this:
android:gravity="center"
android:includeFontPadding="false"
Solution 2
If you want to align contents of RelativeLayout in the center then you need to put android:gravity="center" in the RelativeLayout's properties. Below is a sample XML code for it along with its visual representation.
XML Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:background="@color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Text View"
android:textColor="@color/black"
android:textSize="25dp"
android:textStyle="bold" />
</RelativeLayout>
Graphical Layout:
Solution 3
Try android:layout_centerInParent="true"
or android:layout_centerHorizontal="true"
these apply to RelativeLayout
Solution 4
You can also set the gravity="center"
of its parent.
Solution 5
android:gravity="center"
works on RelativeLayouts only. So try
<TextView
android:id="@+id/title"
android:layout_width="fill_parent" <!--relative Layout-->
android:layout_height="wrap_content"
android:gravity="center"
android:text="Mint Payments"/>
Mun0n
Android Developer and Drunkcode Co-founder StackOverflow is the best site to do Copy Paste and share some kwnoledge Drunkcode o_O
Updated on June 20, 2021Comments
-
Mun0n almost 3 years
Possible Duplicate:
How do I center text horizontally and vertical in a TextView in Android?I have a
RelativeLayout
(before it was aLinerLayout
), that occupies all the screen and I want to put in the center of thisLayout
, aTextView
. I try to do it withgravity = "center"
layout_gravity = "center"
and a few more, but it doesn't work.Anybody knows how to center the
TextView
in the middle of the screen?EDIT
Ok, I think I explained badly. I think the
TextView
is in the center, but what I want to center is the text in theTextView
. Can I do this? -
Aldryd over 12 yearsThe answer here is what you're looking for and will get the TextView object centered within the layout, but you may also still need to add android:gravity="center" so that the text will be centered within the TextView object.
-
Mun0n over 12 yearsI tried this, and doesn't work, the text don't center.
-
Orkun Ozen over 12 yearsWhat layout do you have? Cos it did the trick with linear layout for me.
-
Mun0n over 12 yearsI have
RelativeLayout
, I will try withLinearLayout
and I will tell you. -
Orkun Ozen over 12 yearsi wonder if the other approach works for every other layout.
-
Mun0n over 12 yearsIt doesn't work. My textSize is too large and I don't know how to center it.
-
Rotemmiz over 12 yearsTry posting the xml layout, I will be able to help you further more...
-
Mun0n over 12 yearsthanks @mdelolmo, is just what I need!
-
Mun0n over 12 yearsit is wrong because you don't understand the question. I test it and doesn't work. @mdelolm knows how to do it.
-
Jin35 over 12 yearsGive bounty please, your question are still in featured :)
-
Ruchir Baronia over 8 yearsWhat does
android:includeFontPadding="false"
do? -
mdelolmo over 8 years@RuchirBaronia, it means Android will take into account the font "white space" that it has above and below, to calculate its position in the layout.
-
Michael Fulton almost 8 yearsThis is what I needed +1
-
Admin about 7 yearsthis command is false
-
Lavakush about 7 yearsandroid:gravity will work here.
-
MeqDotNet almost 5 yearsworks like a charm thank you @mdelolmo