get width of textview in characters

I think this is the opposite of Set width of TextView in terms of characters

I have a TextView where I'm showing some report data. I use a monospace TypefaceSpan for a portion of it because I want columns to line up.

I used my test Android device to figure out how many columns I could fit, but the Android emulator seems to have exactly one less column, which makes things wrap in an ugly way in portrait mode.

Is there a way to find out how many characters should fit on 1 line?


The answer would be to use the breakText() of your textView's Paint Object. Here is a Sample ,

int totalCharstoFit= textView.getPaint().breakText(fullString,  0, fullString.length(), 
 true, textView.getWidth(), null);

Now totalCharstoFit contains the exact characters that can be fit into one line. And now you can make a sub string of your full string and append it to the TextView like this,

String subString=fullString.substring(0,totalCharstoFit);

And to calculate the remaining string, you can do this,


Now the full code,

Do this in a while loop,

int totalCharstoFit= textView.getPaint().breakText(fullString,  0, fullString.length(), 
     true, textView.getWidth(), null);
 String subString=fullString.substring(0,totalCharstoFit);


Well you could do math to find this out, find the width of the character, divide the width of the screen by this, and you'd have what you're looking for.

But is it not possible to design it better? Are there any columns you can group together? Display as a graphic, or even exclude completely?

Another possible solution is to use something like a viewpager. (Find out how many columns' width fit on the first page, and then split the remaining tables onto the second page).

You can get total line of Textview and get string for each characters by below code.Then you can set style to each line whichever you want.

I set first line bold.

private void setLayoutListner( final TextView textView ) {
    textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        public void onGlobalLayout() {

            final Layout layout = textView.getLayout();

            // Loop over all the lines and do whatever you need with
            // the width of the line
            for (int i = 0; i < layout.getLineCount(); i++) {
                int end = layout.getLineEnd(0);
                SpannableString content = new SpannableString( textView.getText().toString() );
                content.setSpan(new StyleSpan(, 0, end, 0);
                content.setSpan(new StyleSpan(, end, content.length(), 0);
                textView.setText( content );

Try this way.You can apply multiple style this way.

You can also get width of textview by:

for (int i = 0; i < layout.getLineCount(); i++) {
        maxLineWidth = Math.max(maxLineWidth, layout.getLineWidth(i));

Need Your Help

How to place images in a single column in appcelerator

ios ios-simulator appcelerator appcelerator-titanium

I have the following simple code in appcelerator. I am trying to place 2 image one below the other. Actually I have 10 - and I am trying to do

RestKit - Appending a parameter to all requests

iphone objective-c restkit

I have an iOS app using RestKit in order to communicate with a RESTful Rails server. The server uses a basic session token for authentication of users. I want to append this token to every request ...