How to find android TextView number of characters per line?

28,350

Solution 1

Try this:

private boolean isTooLarge (TextView text, String newText) {
    float textWidth = text.getPaint().measureText(newText);
    return (textWidth >= text.getMeasuredWidth ());
}

Detecting how many characters fit will be impossible due to the variable width of the characters. The above function will test if a particular string will fit or not in the TextView. The content of newText should be all the characters in a particular line. If true, then start a new line (and using a new string to pass as parameter).


Answer to the comment:

  1. because the app can be run in many systems is exactly why you need to measure it.
  2. This is a way to solve your "overall question". What is the difference between using str.size()>numCol vs is too large? You will need to implement your animation (hint #1: insert a newline character)
  3. as I said before when you start a new line, you start a new string (hint #2: if you extend TextView, you can implement all this in overriding setText). (hint #3: Keep track of the lines created with a static int lines; and use newString.split("\\r?\\n")[lines-1] to check for length).

Solution 2

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.

Share:
28,350

Related videos on Youtube

Derek
Author by

Derek

Updated on July 31, 2020

Comments

  • Derek
    Derek almost 4 years

    So I have a TextView in android that has the width of the whole length of the screen and a padding of dip 5. How can I calculate the number of characters that will fit a single line on the screen? I guess in other words, I'm trying to get the number of columns of a textview?

    I considered manual calculation depending on textsize and width, but 1) don't know the correlation and 2) due to the padding in the units of dip, different screens will use different number of actual pixels to pad.

    Overall Question: I am trying to use this to solve: if given a string how can I manually edit to string such that when the textview prints the string character by character, I will know when to start a word that won't fit on one line on the next. Note: I know that textview automatically puts words that won't fit onto the next line, however, since I'm printing character by character, like typing animation, textview doesn't know the word won't fit until it prints out the overflowing characters of that word.

    Been searching everywhere for this...

    Thanks!

    Added solutions:

    one possible solution:

    public String measure2 (TextView t, String s) {
        String u = "";
        int start = 0;
        int end = 1;
        int space = 0;
        boolean ellipsized = false;
        float fwidth = t.getMeasuredWidth();
        for(;;) {
            //t.setText(s.substring(start, end));
            float twidth = t.getPaint().measureText(s.substring(start, end));
            if (twidth < fwidth){
                if (end < s.length())
                    end++;
                else {
                    if (!ellipsized)
                        return s;
                    return u + s.subSequence(start, end);
                }
            }
            else {
                ellipsized = true;
                space = (u + s.substring(start, end)).lastIndexOf(" ");
                if (space == -1)
                    space = end - 1;
                u += s.subSequence(start, space) + "\n";
                start = space + 1;
                end = start + 1;
            }
        }
    }
    

    solution 2, but still uses solution1 sometimes:

    public String measure3 (TextView t, String s) {
        List<String> wlist = Arrays.asList(s.split(" "));
        if (wlist.size() == 1)
            return measure2(t, s);
        String u = "";
        int end = 1;
        float fwidth = t.getMeasuredWidth();
        for(;;) {
            //t.setText(s.substring(start, end));
            if (wlist.isEmpty())
                return u;
            String temp = listStr(wlist, end);
            float twidth = t.getPaint().measureText(temp);
            if (twidth < fwidth){
                if (end < wlist.size())
                    end++;
                else {
                    return u + temp;
                }
            }
            else {
                temp = listStr(wlist, end-1);
                if (end == 1)
                    temp = measure2(t, temp);
                if (wlist.isEmpty())
                    return u + temp;
                else
                    u = u + temp + "\n";
                wlist = wlist.subList(end - 1, wlist.size());
                end = 1;
            }
        }
    }
    
    public String listStr (List<String> arr, int end) {
        String s = "";
        for (String e : arr.subList(0, end) ){
            s = s + e + " ";
        }
        return s.trim();
    }
    

    I used the above code to generate off a original string s, a string u that would be printed. However, I think this approach is very inefficient. Is there another approach or a better algorithm? Note: there are some errors in measure3 that I fixed, but was too lazy to edit

    • FoamyGuy
      FoamyGuy about 13 years
      I don't think the default font is fixed width. So it would fit less Ms than Is for instance. If you are including a fixed width font then one way to find out is keep adding on to a string and set it to the view then count them by hand on the screen. Its also going to end up different for different screens though which could cause problems for your use.
    • codeskraps
      codeskraps about 10 years
      I would have a look at this question stackoverflow.com/questions/6272614/…
    • VIISHRUT MAVANII
      VIISHRUT MAVANII almost 5 years
      @Derek Can you update measure3 function. I want to use this solution. Thanks
    • VIISHRUT MAVANII
      VIISHRUT MAVANII almost 5 years
      @Derek Finally I got the solution you should also try this stackoverflow.com/a/56753731/6676310
  • Derek
    Derek about 13 years
    I am setting the font size, but since I prefer to use the app on multiple systems, I'm not going to manually count the length. Also, your isTooLarge could be used to get length of string when the first occurrence of overflow occurs, however, it would still leave the first line subject to the ellipsize animation of textview. Thanks for the help, but I need something that allows me to know when textview will ellipsize for every line.
  • Aleadam
    Aleadam about 13 years
    @Derek I The reply was too long so I added to the answer
  • Derek
    Derek about 13 years
    Okay so I posted my solution in my edited question, is there a more efficient way of doing this?
  • Aleadam
    Aleadam about 13 years
    @Derek - firstly, I see is that you're measuring one character at a time. You're interested in knowing if the whole word will fit or not, so divide the string in words (str.split(" ")) and loop through the words instead. And secondly, test the function first. Is it really that inefficient? Do you notice any lag?
  • Derek
    Derek about 13 years
    I see your point, so I made measure3, however, I don't know why but textview ellipsizes even if you have one giant string with no spaces, so measure2 had to be used on cases when a single word exceeded the width, but measure3 does use str.split(" ") which does improve the efficiency. I realize that what I'm doing on this app does not lag since it's only reading short to medium strings, but say I use this function in another app for whatever reason that takes in a chapter of a book or something huge, is there a more efficient code for that or is this kind of the only way?
  • Aleadam
    Aleadam about 13 years
    @Derek two last points and let's call it a day: (1) If you're animating one character at a time, the calculations will be done much faster than the animation, so the length of the string doesn't matter. Furthermore, you can stop calculating when you reached the bottom of the screen. Why bother calculating more? (2) If you keep track of the number of word that started each line, then you can use that number to start the next iteration, instead of starting from scratch (you may be doing that already, I did not look at the whole code again, I'm just based on your reply). Good luck!
  • VIISHRUT MAVANII
    VIISHRUT MAVANII almost 5 years
    @Aleadam Thanks for your answer. I have made a solution using your answer. stackoverflow.com/a/56753731/6676310
  • Xenolion
    Xenolion almost 4 years
    I believe this gives an approximation!