Wednesday, April 3, 2013

How do I center text horizontally and vertical in a TextView in Android?

How do I center text horizontally and vertical in a TextView in Android? - Stack Overflow



















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





























I'm assuming you're using XML layout.



android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="@string/**yourtextstring**"
/>


























android:gravity="center"


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.

























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:



























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.

























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);
























If you are trying to center text on a TableRow in a TableLayout, here is how I achieved this:



android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
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" />






















You cab also use combination:



android:gravity="left|center"


Then, if textview width is more than "fill_parent" the text will be still aligned to left (not centered as with gravity set only to "center").






















I used the android:gravity="center" and only in the view with :inputType="textPersonName" the text is center, the view with :inputType="number", the numbers are still flying to the top... :"(


Even iOS 3.0 was very good at things like this :"|

























In Relative layout, it will be nice with it. and another button and anything else you can add.



android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff314859"
android:paddingLeft="16dp"
android:paddingRight="16dp">
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...



works nicely for me.






















I'm setting the height of the textview and can't get the text to center vertically. It stays at the bottom of the textview. Code is below but is not formatting correctly in this post (even though each line preceeded by 4 spaces).


Problem seems to be that I set textview layout_height to 20dp. If I change layout_height to wrap_content: the text does center vertically - but it's too high for my needs.


The textview is in a relative layout and has layout_centerVertical="true" and gravity="center_horizontal|center_vertical". I've also tried android:gravity="center" and tried omitting the centerVertical + many other options.

























This worked better for me: android:layout_gravity="center". The other way mentioned works but in a few test apps was being overriden. This worked better for me.






















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






















just use android:gravity="center"

























You can set textview in horizontaly and vartically using below method. add one layout set the layout gravity horizontal and vertical and add the textview in that layout so you have textview in center horizontal and vartical




















default






.

forum.xda-developers.com

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...