Skip to content Skip to sidebar Skip to footer

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 i

Solution 1:

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);
textView.append(substring);

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

fullString=fullString.substring(subString.length(),fullString.length());

Now the full code,

Do this in a while loop,

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

}

Solution 2:

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).

Solution 3:

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.

privatevoidsetLayoutListner( final TextView textView ) {
    textView.getViewTreeObserver().addOnGlobalLayoutListener(newViewTreeObserver.OnGlobalLayoutListener() {
        @OverridepublicvoidonGlobalLayout() {
            textView.getViewTreeObserver().removeGlobalOnLayoutListener(this);

            finalLayoutlayout= textView.getLayout();

            // Loop over all the lines and do whatever you need with// the width of the linefor (inti=0; i < layout.getLineCount(); i++) {
                intend= layout.getLineEnd(0);
                SpannableStringcontent=newSpannableString( textView.getText().toString() );
                content.setSpan(newStyleSpan(android.graphics.Typeface.BOLD), 0, end, 0);
                content.setSpan(newStyleSpan(android.graphics.Typeface.NORMAL), 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));
}

Post a Comment for "Get Width Of Textview In Characters"