Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6106323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:04:02+00:00 2026-05-23T14:04:02+00:00

How to align the text to top of a TextView? Equivalent Android API for

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:04:02+00:00Added an answer on May 23, 2026 at 2:04 pm

    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();
        }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

<?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:paddingTop=4dip android:paddingBottom=6dip android:layout_width=fill_parent android:layout_height=wrap_content android:orientation=horizontal> <TableLayout android:layout_width=fill_parent android:layout_height=wrap_content> <TableRow
how can i Reduce Tab height and align text on top? import android.app.TabActivity; import
I want to align some text to the top of a div. It seems
I need to align text using li in a centered table so that the
I'm trying to align text on top of an image in such a way
How can I make the multiline form field Description word-wrap and top-align its text?
How to select with a specific style attribute? <div style=width: 420px; text-align: left; margin-top:
The sun is red should align to the top of the image, and 2pm
I am trying to align a text Email and textbox for it on top
I have an HTML string that has the following form: <tr valign=top><td colspan=2 style=padding-bottom:5px;text-align:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.