简体   繁体   中英

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);
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());

}

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() {
        @Override
        public void onGlobalLayout() {
            textView.getViewTreeObserver().removeGlobalOnLayoutListener(this);

            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(android.graphics.Typeface.BOLD), 0, end, 0);
                content.setSpan(new StyleSpan(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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM