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 922831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:05:44+00:00 2026-05-15T19:05:44+00:00

I want a TextView which adjusts the font size so that the text in

  • 0

I want a TextView which adjusts the font size so that the text in the view will automatically expand (or shrink) to fill the full width of the view. I thought I might be able to do this by creating a customised TextView which overrides onDraw() as follows:

public class MaximisedTextView extends TextView {
    // (calls to super constructors here...)

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        TextPaint textPaint = this.getPaint();
        Rect bounds = new Rect();

        String text = (String)this.getText(); // current text in view
        float textSize = this.getTextSize();  // current font size
        int viewWidth = this.getWidth() - this.getPaddingLeft() - this.getPaddingRight();
        textPaint.getTextBounds(text, 0, text.length(), bounds);
        int textWidth = bounds.width();

        // reset font size to make text fill full width of this view
        this.setTextSize(textSize * viewWidth/textWidth);
    }
}

However, this sends the app into an endless loop (with the text size growing and shrinking slightly each time!), so I’m clearly going about it the wrong way. Does the call to setTextSize() trigger an invalidate so that onDraw is called again, endlessly?

Is there a way I can prevent the recursive call (if that’s what is happening)? Or should I be going about it a completely different way?

  • 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-15T19:05:44+00:00Added an answer on May 15, 2026 at 7:05 pm

    Does the call to setTextSize() trigger
    an invalidate so that onDraw is called
    again, endlessly?

    Yes, that’s probably what is hapening. If you take a look of the source code of setTextSize you will see that it will call this method:

    private void setRawTextSize(float size) {
        if (size != mTextPaint.getTextSize()) {
            mTextPaint.setTextSize(size);
    
            if (mLayout != null) {
                nullLayouts();
                requestLayout();
                invalidate(); // yeahh... invalidate XD
            }
        }
    }
    

    So, if you are doing the hard work of overriding the onDraw method, why don’t you use directly some of the drawText methods of the Canvas class?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my application, I want to set a style to the TextView which will
I have a textview which displays a lot of text. My users want to
I have a TextView which displays a long text. I want to give some
I have a Textview in which i am showing text 12345 now i want
I have a TextView which height is dynamic. I want to set that the
I have a TextView which I fill with text from a string resources in
I want to design simple app widget which has two textview and two button
In my code,I want to display TextView text name as Toast message. I do
I want to use the marquee effect on a TextView, but the text is
I want to be able to do this: the TextView to change its size

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.