How do I center text horizontally and vertically in a TextView? - android

How do I center the text horizontally and vertically in a TextView, so that it appears exactly in the middle of the TextView in Android?

I'm assuming you're using XML layout.
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/**yourtextstring**"
/>
You can also use gravity center_vertical or center_horizontal according to your need.
As #stealthcopter commented, in java: .setGravity(Gravity.CENTER);.
And for Kotlin users, .gravity = Gravity.CENTER

android:gravity="center"
This will do the trick

You can also set it up dynamically using:
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

android:layout_centerInParent="true"
This works when used with a RelativeLayout where the layout's height & width are set to wrap_content.

You can also use the combination:
android:gravity="left|center"
Then, if textview width is more than "fill_parent" the text will still be aligned to left (not centered as with gravity set only to "center").

Apply gravity:
TextView txtView = (TextView) findViewById(R.id.txtView);
txtView.setGravity(Gravity.CENTER_HORIZONTAL);
For vertical:
txtView.setGravity(Gravity.CENTER_VERTICAL);
In XML:
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="#string/Hello_World"
/>

There are two ways of doing this.
The first in the XML code. You need to pay attention at the Gravity Attribute. You also can find this attribute in the Graphic Editor; it may be easier than the XML EDITOR.
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:text="Your Text"
/>
For your specific scenario, the values of gravity will be:
center_vertical|center_horizontal
In the graphical editor you will find all the possible values, even see their results.

If you are using TableLayout make sure to set the gravity of the TableRows to center, too.
Otherwise it will not work. At least it didn't work with me until I set the gravity of the TableRow to center.
For example, like this:
<TableRow android:id="#+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center">
<TextView android:text="#string/chf" android:id="#+id/tv_chf" android:layout_weight="2" android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="center"></TextView>
</TableRow>

You need to set TextView Gravity (Center Horizontal & Center Vertical) like this:
android:layout_centerHorizontal="true"
and
android:layout_centerVertical="true"
And dynamically using:
textview.setGravity(Gravity.CENTER);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

In my opinion,
android:gravity="center"
is better than,
android:layout_centerInParent="true"
which is better than,
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
at least for formatting text.

For Linear Layout:
In XML use something like this
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:text="Your Text goes here"
/>
To do this at run time use something like this in your activity
TextView textView1 =(TextView)findViewById(R.id.texView1);
textView1.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
For Relative Layout: in XML use some thing like this
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:text="Your Text goes here"
/>
To do this at run time use something like this in your activity
TextView textView1 =(TextView)findViewById(R.id.texView1);
RelativeLayout.LayoutParams layoutParams = RelativeLayout.LayoutParams)textView1.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
textView1.setLayoutParams(layoutParams);

Use in the XML file.
Layout file
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="#string/stringtext"/>
or:
Use this inside the Java class
TextView textView =(TextView)findViewById(R.id.texviewid);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

Use this for relative layout
android:layout_centerInParent="true"
and for other layout
android:gravity="center"

If the TextView's height and width are wrap content then the text within the TextView always be centered. But if the TextView's width is match_parent and height is match_parent or wrap_content then you have to write the below code:
For RelativeLayout:
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hello World" />
</RelativeLayout>
For LinearLayout:
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hello World" />
</LinearLayout>

While using gravity works for TextView, there's an alternate method implemented in API level 17 -
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
Don't know the difference, but it works too. However only for API level 17 or higher.

In RelativeLayout, it will be nice with it.
And another Button and anything else you can add.
The following works nicely for me.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff314859"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<TextView
android:id="#+id/txt_logo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="your text here"
android:textSize="30dp"
android:gravity="center"/>
...other button or anything else...
</RelativeLayout>

Use android:textAlignment="center"
<TextView
android:text="HOW WAS\nYOUR\nDAY?"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:id="#+id/textView5"
/>

Easiest way (which is surprisingly only mentioned in comments, hence why I am posting as an answer) is:
textview.setGravity(Gravity.CENTER)

You can just set the gravity of your textview into CENTER.

TextView gravity works as per your parent layout.
LinearLayout:
If you use LinearLayout then you will find two gravity attribute
android:gravity & android:layout_gravity
android:gravity : represent layout potion of internal text of TextView while
android:layout_gravity : represent TextView position in parent view.
If you want to set text horizontally & vertically center then use below code this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="300dp"
android:background="#android:color/background_light"
android:layout_height="300dp">
<TextView
android:layout_width="match_parent"
android:text="Hello World!"
android:gravity="center_horizontal"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content"
/>
</LinearLayout>
RelativeLayout:
Using RelativeLayout you can use below property in TextView
android:gravity="center" for text center in TextView.
android:gravity="center_horizontal" inner text if you want horizontally centered.
android:gravity="center_vertical" inner text if you want vertically centered.
android:layout_centerInParent="true" if you want TextView in center position of parent view.
android:layout_centerHorizontal="true" if you want TextView in horizontally center of parent view.
android:layout_centerVertical="true" if you want TextView in vertically center of parent view.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="300dp"
android:background="#android:color/background_light"
android:layout_height="300dp">
<TextView
android:layout_width="match_parent"
android:text="Hello World!"
android:gravity="center"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
/>
</RelativeLayout>

If you are trying to center text on a TableRow in a TableLayout, here is how I achieved this:
<TableRow android:id="#+id/rowName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView android:id="#+id/lblSomeLabel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="100"
android:text="Your Text Here" />
</TableRow>

If you are using Relative Layout:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/stringname"
android:layout_centerInParent="true"/>
If you are using LinearLayout
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/stringname"
android:layout_gravity="center"/>

Try this way,it will work
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"/>
</LinearLayout>

Here is my answer that I had used in my app. It shows text in center of the screen.
<TextView
android:id="#+id/txtSubject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/subject"
android:layout_margin="10dp"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />

The TextView's height and width are wrap content then the text within the textview always be centered, then make center in its parent layout by using:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hello.."/>
</RelativeLayout>
For LinearLayout also the code is same :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello.."/>
</LinearLayout>
and pro-grammatically parent is RelativeLayout java code this at run time use something like this in your activity
TextView textView1 =(TextView)findViewById(R.id.texView1);
RelativeLayout.LayoutParams layoutParams = RelativeLayout.LayoutParams)textView1.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
textView1.setLayoutParams(layoutParams);

Actually, we can do better by excluding fontPadding.
<TextView
android layout_height="wrap_content"
android layout_height="wrap_content"
android:includeFontPadding="false"
android:textAlignment="center"
/>

As many answers suggest above works fine.
android:gravity="center"
If you want to center it just vertically:
android:gravity="center_vertical"
or just horizontally:
android:gravity="center_horizontal"

Simply, in your XML file, set the textview gravity to center:
<TextView
android:gravity="center" />

android:gravity="center_horizontal" for align text Center horizontally.
android:gravity="center_vertical" for align text Center vertically.
android:gravity="center" for align text Center both vertically and horizontally.
<TextView
android:layout_width="match_parent"
android:gravity="center_horizontal|center_vertical"
android:layout_height="match_parent" />

You can do like this to get text centered
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />

Related

TextView Alignment Issue on Android

I am having two TextView in LinearLayout.
<LinearLayout
android:layout_marginTop="-10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/product_discounted_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/activity_vertical_margin"
android:layout_marginEnd="#dimen/margin_between_prices"
android:drawablePadding="5dp"
android:gravity="center_vertical"
android:text="AED200000000.00"
android:textColor="#color/black"
android:textSize="#dimen/font_size_sub_normal"
android:visibility="visible" />
<TextView
android:id="#+id/product_unit_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/activity_vertical_margin"
android:drawablePadding="5dp"
android:gravity="center_vertical"
android:text="AED200000000000.00"
android:textSize="#dimen/font_size_emphasized"
android:visibility="visible" />
</LinearLayout>
I want something like if 2nd TextView doesnt come in single line than it should shift to second line.
Issue is :
With SingeLine=true
With maxLine=1
What I want :
change
android:orientation="horizontal"
to
android:orientation="vertical"
and also add maxline and singleline to textView
It is easy put your text views in a RelativeLayout and all you need to set layout_above or layout_bottom for your TextViews
or you can change orientation:vertical and then the gravity works.
You can also set layout_gravity

TextSwitcher not centered vertically with layout_gravity: center_vertical

I have a vertical LinearLayout with 2 TextSwitcher inside. Sometimes only the first one(#+id/ts1) will show, sometimes both of them will show on screen. The font size for ts1 is 20, for ts2 is 16.
<LinearLayout
android:id="#+id/linearLayout1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="center_vertical"
android:focusable="false"
android:layout_marginLeft="#dimen/dimen_left1"
android:visibility="gone">
<TextSwitcher
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:id="#+id/ts1"/>
<TextSwitcher
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:id="#+id/ts2"/>
</LinearLayout>
When I tested it, when both of them showed on screen, it worked fine, but when there's only ts1 shown, the text is not centered vertically, it's more like on the top vertically instead of centered. I programmatically set the visibility of these 2 TextSwitchers.
Does anyone know why this happens?
Thanks!!!
Setting the layout_gravity on TextSwitchers does not change the text android:gravity of the underlying TextViews. You can override the default TextViews that TextSwitchers use by putting them in your XML. From there, you can set the android:layout_gravity or android:gravity on them to center the text vertically. This should work:
<TextSwitcher
android:id="#+id/ts1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
</TextSwitcher>
<TextSwitcher
android:id="#+id/ts2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
</TextSwitcher>
You need to add gravity option to TextView, not the TextSwitcher.
For example, you can add layout_width, layout_height, and gravity option dynamically.
mSwitcher1 = (TextSwitcher) findViewById(R.id.textSwitcher1);
mSwitcher1.setFactory(new ViewSwitcher.ViewFactory() {
public View makeView() {
TextView myText = new TextView(MainActivity.this);
myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
myText.setTextSize(36);
myText.setTextColor(Color.BLUE);
// Add this
myText.setGravity(Gravity.CENTER);
// Add this
myText.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
return myText;
}
});
Have you tried using the ConstraintLayout? Using it may help you out. If you give more information on what the behavior of everything needs to be, I can give you a code example.

How to align two TextView in a LinearLayout in the same line

I have two TextView in a LinearLayout, I want to align them one to the left (or center) and one to right in the same line. How to do this? I try to use gravity but they ignore it.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="TextView" />
</LinearLayout>
The easiest way is to change your LinearLayout to a RelativeLayout.
You can use android:layout_alignParentRight="true" and android:layout_alignParentLeft="true". Or to center it use android:layout_centerInParent="true"
See here why gravity won't work
You are using gravity instead of layout_gravity which is what you would want. This post should help clarify the difference
The docs show you available properties.
android:gravity is used to set the gravity of content inside the view. However, in your case the width is wrap_content, hence the content has nowhere to go in the text views.
Use a RelativeLayout with layout_width as match_parent. Then use the android:layout_alignParentLeft="true" and android:layout_alignParentRight="true"with the textViews.
Use it with or without the android:gravity in the second textview and try .
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:layout_weight="1" />
</LinearLayout>
If LinearLayout is Vertical, you can put only an object per line.
You can use RelativeLayout, or else put in a line a LinearLayout Horizontal, that contains textviews
ex.
<LinearLayout vertical>
<LinearLayout horizontal>
<textview 1></>
<textview 2></>
</LinearLayout>
</LinearLayout>
I fixed all my issues with GridLayout...is the best thing bcos u don't need to align anithing to nothing...just put what u want into the matrix (row,column)...and this will allow you to visualize all the field in exactly wrap content of your datas also in landscape is perfect!

TextView not properly in the center

i want to place a textview in the center of a linearlayout but i am having problems with it. my linearlayout is match_parent in layout_width. textview is centered by assigning center to layout_gravity. textview is wrap_content in layout_width. here is my xml file
<LinearLayout 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"
android:background="#drawable/background"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text=" Text"
android:textSize="40sp"
android:textStyle="bold"
android:typeface="monospace" />
</LinearLayout>
</LinearLayout>
Two things:
1) set
android:gravity="center"
android:layout_gravity="center"
in your textview;
2) The second linearlayout (inside) is useless. Just remove it. What's more, if you only have one view, consider using FrameLayout instead of LinearLayout, then use <merge> tag to optimize the performance (see details here)
if you want to center the text inside your TextView you have to use android:gravity="center"
Change
android:layout_gravity="center" to
android:gravity="center"
Set these properties in the second Linearlayout.
android:gravity="center"
android:layout_gravity="center"
to make your textview center set
android:gravity="center"
android:layout_gravity="center"
of your textview.
see the difference here:-
android:gravity :-- sets the gravity of the content of the View its used on.
android:layout_gravity:-- sets the gravity of the View or Layout in its parent.

TextView height does not match_parent / fill_parent

I have a simple TextView with a singleLine.
I would like to set the height to match_parent/fill_parent, but the behaviour of Android is to only to wrap_content.
Is there a way to force that the TextView takes all height ?
Tkx
Edit :
This is what I get :
and my layout :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:paddingLeft="13dp"
android:paddingRight="3dp"
android:textColor="#android:color/white"
android:textStyle="bold" />
[...]
</RelativeLayout>
You can set the TextView height to match_parent/fill_parent. And change the gravity and all other property of that TextView according to your requirement.
If this is not suffiecient for you then let me know what exactly you want to do.
EDIT:
As per your new edited code i worked on your code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:paddingLeft="13dp"
android:paddingRight="3dp"
android:textStyle="bold"
android:textColor="#ffffff"
android:text="dateText"/>
</RelativeLayout>
Now its showing the TextView at the central_vertical position of the whole screen.
Try this code. Its working. Change your RelativeLayout and TextView as i did with these.
For that TextView parent width, you may use wrap_content.

Categories

Resources