text - How to find android TextView number of characters per line? -


so have textview in android has width of whole length of screen , padding of dip 5. how can calculate number of characters fit single line on screen? guess in other words, i'm trying number of columns of textview?

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

overall question: trying use solve: if given string how can manually edit string such when textview prints string character character, know when start word won't fit on 1 line on next. note: know textview automatically puts words won't fit onto next line, however, since i'm printing character character, typing animation, textview doesn't know word won't fit until prints out overflowing characters of word.

been searching everywhere 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, 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 = "";     (string e : arr.sublist(0, end) ){         s = s + e + " ";     }     return s.trim(); } 

i used above code generate off original string s, string u printed. however, think approach inefficient. there approach or better algorithm? note: there errors in measure3 fixed, lazy edit

try this:

private boolean istoolarge (textview text, string newtext) {     float textwidth = text.getpaint().measuretext(newtext);     return (textwidth >= text.getmeasuredwidth ()); } 

detecting how many characters fit impossible due variable width of characters. above function test if particular string fit or not in textview. content of newtext should characters in particular line. if true, start new line (and using new string pass parameter).


answer comment:

  1. because app can run in many systems exactly why need measure it.
  2. this way solve "overall question". difference between using str.size()>numcol vs istoolarge? need implement animation (hint #1: insert newline character)
  3. as said before, when start new line, start new string (hint #2: if extend textview, can implement in overriding settext). (hint #3: keep track of lines created static int lines; , use newstring.split("\\r?\\n")[lines-1] check length).

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -