adam steinberg
adam steinberg

Reputation: 31

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

I have a TextView with a very large string that uses multiple lines when compiled. I am trying to figure out how I can find the number of string elements or characters there are in each line.

The XML for the TextView

        <TextView
        android:id="@+id/textViewSentenceP"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.00"
        android:gravity="center"
        android:singleLine="false"
        android:padding="25dip" 
        android:scrollHorizontally="false"
        android:textColor="#FFFFFFFF"
        android:maxLines = "5"
        android:scrollbars = "vertical"
        android:textSize="12sp" />

My code works perfectly i just need to know how I can find the number of elements in each line. Also, if this can not be found then if I can determine which element is at the end the line of text or which element starts the next line of text will also be a fine answer. Thank you!!

Upvotes: 1

Views: 1243

Answers (1)

adam steinberg
adam steinberg

Reputation: 31

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

Upvotes: 2

Related Questions