How to Find Android Textview Number of Characters Per Line

How to find android TextView number of characters per line?

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

Get how many characters a line in Textview can have

TextPaint paint = textView.getPaint();
int wordwidth=(int)paint.measureText("a",0,1);
int screenwidth = getResources().getDisplayMetrics().widthPixels;
int num = screenwidth/wordwidth

Specify number of characters in each line in TextView?

yes , sure android:maxLength="5" works for TextView to set the maximum number of characters for TextView. You can set android:layout_width="130dip" for the TextView too. That will show the character in the format abc... if the text exceed the layout width.

How can I find the number of characters OR string elements per line line in Android

Never mind I got it!!

Using the getLineStart method fixed the problem! For anyone who was having a similar problem to me,try this code, maybe it will help you! Note: the getLineStart() will read all the characters from every line including spaces in order till it gets to that specific line.

     int curLine = tv.getLayout().getLineStart(0);
int nextLine = tv.getLayout().getLineStart(1);
int difference = nextLine-curLine;

The value of difference will be how many characters (including spaces) are on the curLine

android: limit of 10 characters per line TextView

Define a method that returns a string formatted to have 10 characters per line:

public String getTenCharPerLineString(String text){

String tenCharPerLineString = "";
while (text.length() > 10) {

String buffer = text.substring(0, 10);
tenCharPerLineString = tenCharPerLineString + buffer + "/n";
text = text.substring(10);
}

tenCharPerLineString = tenCharPerLineString + text.substring(0);
return tenCharPerLineString;
}

How to set maximum characters per line for text view in android

Using

android:maxWidth="size"

Replace the "size" with the size you prefer. For example, 100dip.

how to know that how many characters could be placed in one line?

This should do the job for you (give or take a few mistakes from coding without an ide :-/ )

int countLineBreaks(final TextView view, final String toMeasure) {

final Paint paint = textView.getPaint(); // Get the paint used by the TextView
int startPos = 0;
int breakCount = 0;
final int endPos = toMeasure.length();

// Loop through the string, moving along the number of characters that will
// fit on a line in the TextView. The number of iterations = the number of line breaks

while (startPos < endPos) {
startPos += paint.breakText(toMeasure.substring(startPos, endPos),
true, tv.getWidth(),(float[]) null);
lineCount++;
}
// Line count will now equal the number of line-breaks the string will require
return lineCount;
}

..you get the idea ;-)

Can I limit TextView's number of characters?

Here is an example. I limit the sizewith the maxLength attribute, limit it to a single line with maxLines attribute, then use the ellipsize=end to add a "..." automatically to the end of any line that has been cut-off.

<TextView 
android:id="@+id/secondLineTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:maxLength="10"
android:ellipsize="end"/>


Related Topics



Leave a reply



Submit