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
You are applying to your TextView a compound Drawable on the right.. to make the three dots appear in this scenario, you have to apply a android:drawablePadding="{something}dp" attribute to the TextView as well. Hope it helps!
Line height usually means text size + "padding" top/bottom. So, if your designer write line height 19sp and text size 15sp, it means you need to have extra padding 4sp. 19sp - 15sp = 4sp. To implement it in your layout, use lineSpacingExtra attribute.
To use preset sizes to set up the autosizing of TextView in XML, use the android namespace and set the following attributes: Set the autoSizeText attribute to either none or uniform. none is a default value and uniform lets TextView scale uniformly on horizontal and vertical axes.
EditText is used for user input. TextView is used to display text and is not editable by the user. TextView can be updated programatically at any time.
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:
str.size()>numCol
vs is too large? You will need to implement your animation (hint #1: insert a newline character)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).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With