I have an issue, which seems quite trivial, but I have not been able to solve it this far.
I have a full screen landscape activity in which I have a single LinearLayout containing two TextViews which occupy half of the screen each. One of the TextViews shall have a centered character, regardless of font size.
The problem is that when I increase font size of this character it does not center, but instead has its center below the center line of the screen.
Here is the activity with 200sp size character on a 800*480 screen, looks ok - character is centered:
http://i122.photobucket.com/albums/o251/px_seven/char_200_sp.jpg
Here is the activity with 300sp size character, now the character has moved down:
http://i122.photobucket.com/albums/o251/px_seven/char_300_sp.jpg
This is the layout (I change from the default font size 250sp in the code):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:background="#000000" />
<TextView
android:id="@+id/tv2"
android:lines="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center"
android:background="#000000"
android:text="N"
android:textSize="250sp" />
</LinearLayout>
Does anybody see what the problem is?
Thanks, Fredric
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.
Try this: Using LinearLayout if you are using textview height and width match_parent You can set the gravity text to center and text alignment to center text horizontal if you are using textview height and width wrap_content then add gravity center attribute in your LinearLayout. Save this answer.
Select the text that you want to center. in the Page Setup group, and then click the Layout tab. In the Vertical alignment box, click Center. In the Apply to box, click Selected text, and then click OK.
How can I center text programmatically in android? To center align text in TextView in Kotlin Android, set android:textAlignment attribute with the value “center” in layout file, or programmatically set the textAlignment property of the TextView object with View. TEXT_ALIGNMENT_CENTER in activity file.
Try adding this attribute to your text view android:includeFontPadding="false"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With