Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align the text to top of TextView?

Tags:

android

layout

People also ask

How do I center align text 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.

How do I align text in text view?

To center align text in TextView widget, set the textAlignment attribute of the widget to center value. The default text alignment of text in TextView is left. Note: When you specify text alignment, it takes effect only when the width of TextView widget is more than the width of text.

How do you align text on android?

To right align text in TextView in Kotlin Android, set android:textAlignment attribute with the value “viewEnd” in layout file, or programmatically set the textAlignment property of the TextView object with View.


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:includeFontPadding attribute to false in your XML or you can do it programmatically with the function setIncludeFontPadding(false).

Look at the SDK documentation for TextView if this is still unclear.

EDITED ANSWER
If setting the android:includeFontPadding attribute does not accomplish what you're trying to do, the other solution is to override the onDraw(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.

public class TopAlignedTextView extends TextView {

    // Default constructor when inflating from XML file
    public TopAlignedTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    // Default constructor override
    public TopAlignedTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs);
        setIncludeFontPadding(false); //remove the font padding
        setGravity(getGravity() | Gravity.TOP); //make sure that the gravity is set to the top
    }

    /*This is where the magic happens*/
    @Override
    protected void onDraw(Canvas canvas){
        TextPaint textPaint = getPaint(); 
        textPaint.setColor(getCurrentTextColor());
        textPaint.drawableState = getDrawableState();
        canvas.save();

        //converts 5dip into pixels            
        int additionalPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getContext().getResources().getDisplayMetrics());

        //subtracts the additional padding from the top of the canvas that textview draws to in order to align it with the top.            
        canvas.translate(0, -additionalPadding);
        if(getLayout() != null)
            getLayout().draw(canvas);
        canvas.restore();
    }
} 

In your .xml, write that:

android:gravity="center|top"

The first "center" align your line horizontally in the center and the next "top" align vertically up.


The actual font itself has extra whitespace on the top and bottom I believe.

Usually what I do is give the view negative margins. The issue is that this looks bad when devices come with different fonts.

android:layout_marginTop="-5dp"
android:layout_marginBottom="-5dp"

You will have to adjust based on the text size, bigger text sizes require more negative padding.


Possible, you do not need make your view based on TextView. Try to extend View and write your text in onDraw().

Look at here: https://stackoverflow.com/a/32081250/1263771


I think your snippet is right. I guess you are bothered by some pixels between top of the letter "T" and top edge of the TextView? This space is required to render some letters that are not present in English alphabet.

Check that links to get idea:

  • http://unicode.org/charts/PDF/U0400.pdf
  • http://en.wikipedia.org/wiki/Typeface