How to align the text to top of a TextView?
Equivalent Android API for Swings setInsets()?
that is top of text should start be in (0,0) of TextView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="42sp"
android:paddingTop="0px"
android:gravity="top">
</TextView>
</LinearLayout>
I have used above snippet, however still output is not as expected
Any ideas?
So the space at the top of the TextView is padding used for characters outside the English language such as accents. To remove this space you can either set the
android:includeFontPaddingattribute tofalsein your XML or you can do it programmatically with the functionsetIncludeFontPadding(false).Look at the SDK documentation for TextView if this is still unclear.
EDITED ANSWER
If setting the
android:includeFontPaddingattribute does not accomplish what you’re trying to do, the other solution is to override theonDraw(Canvas canvas)method of the TextView that you’re using so that it eliminates the additional top padding that Android adds to every TextView. After writing my original answer, I noticed that for some reason TextView includes extra padding in addition to the font padding. Removing the font padding as well as this additional padding perfectly aligns the text to the top of the TextView. Look at the code snippet below.