2

I'm having trouble adding a new line after every 20 characters. I've seen many posts and am getting confused about the android: commands and which commands should be used for TextView/EditText.

This is the XML code I have written for my textview:

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/descriptionResult"
            android:text="abcdefghijklmnopqrstuvwz abcdefghijklmnopqrstuvwz"
            android:textColor="#FFFFFF"
            android:maxEms="20"
            android:singleLine="false"/>

I've also tried a variation of commands such as:

android:maxLength(20) - Which cuts off my text but does not display the rest on a new line

android:maxWidth(100p) - Error

The Error I keep Getting is:

Exception raised during rendering: java.util.Locale.toLanguageTag()Ljava/lang/String;

Does anyone have a solution to my problem does this have to be done in java? If so how?, thank you in advance

Phillip
  • 59
  • 3
  • 9
  • 1
    `android:maxLength(20)` limits your text to 20 characters. If you want to split your line every 20 characters, you have to insert a **newline character** (which is **\n**, for your information) at every 20th character position. This is done in Java code, not in the xml layout. – Phantômaxx Nov 26 '14 at 22:37
  • I'm really stuck do you have any idea about how to go about this? – Phillip Nov 26 '14 at 22:50
  • Have a look at this post: http://stackoverflow.com/questions/9276639/java-how-to-split-a-string-by-a-number-of-characters – Phantômaxx Nov 26 '14 at 22:58

2 Answers2

7

With the help of @Der Golem provided link, I try to make one solution. Idea is that you need to set text from class file. And append '\n' character after every 20 character.

Variables required,

TextView textView;
String tmpString = "1234567890123456789012345678901234567890";
StringBuffer finalString;

Block of code that you need to write in onCreate method.

    textView = (TextView) findViewById(R.id.txt);
    int index = 0;
    finalString = new StringBuffer(); 
    while (index < tmpString.length()) {
        Log.i(TAG, "test = " + tmpString.substring(index, Math.min(index + 20,tmpString.length())));
        finalString.append(tmpString.substring(index, Math.min(index + 20,tmpString.length()))+"\n");
        index += 20;
    }
    textView.setText(finalString);

Update : Reference,

    String mainString = "Hi Android, How are you? I hope you are doing great.";
    String[] stringArray = mainString.split("\\s+");
    String tmpString = "";
    for (String singleWord : stringArray) {
        Log.d(TAG, "singleWord = " + singleWord);
        if ((tmpString + singleWord + " ").length() > 20) {
            finalString.append(tmpString + "\n");
            Log.e(TAG, "finalString = " + finalString);
            tmpString = singleWord + " ";
        } else {
            tmpString = tmpString + singleWord + " ";
        }
        Log.d(TAG, "tmpString = " + tmpString);
    }

    if (tmpString.length() > 0) {
        finalString.append(tmpString);
        Log.e(TAG, "last finalString = " + finalString);
    }
    textView.setText(finalString);

Not a great solution but may be meets your requirement. Let me know if you need further assistance.

Community
  • 1
  • 1
Chitrang
  • 5,097
  • 1
  • 35
  • 58
  • Thank you so much, I just have a few questions. What does the TAG mean? And is the tempString variable included just as an example? Would I replace that with my textView variable? Sorry for my inexperience – Phillip Nov 27 '14 at 15:02
  • I only ask because the TAG is showing as an error on my java file – Phillip Nov 27 '14 at 15:03
  • In your tmpString just add your string you want to display. And you can comment Log line, that is only for debugging purpose. – Chitrang Nov 27 '14 at 16:54
  • 2
    The code works perfectly thank you, it does however cut words off half way through and continues them below. Would you have any idea how to add maybe elipses or only enter a new line if there is a space? – Phillip Nov 27 '14 at 17:34
  • Glad that worked, please accept it as an answer. This is based on number of character, so it will only consider characters not words. – Chitrang Nov 27 '14 at 17:42
  • @Chitrang This helped me too.But AS Phillip said it is breaking the words.Your logic much simpler.Could you help in dividing the line based on words.like 2nd white space occurence or provide any link if possible – Prabs Aug 01 '15 at 06:28
  • **Not a great solution** How could you conclude that @Chitrang.It's the perfect solution.For this trick 10 upvotes :) A small edit `finalString.append(tmpString + "\n");` instead of `finalString.append(tmpString);` – Prabs Aug 03 '15 at 11:35
  • String[] : if your string is lengthy than this solutions creates so many String objects, if you understand String class very well. – Chitrang Aug 03 '15 at 11:51
0

Every TextView has a rendering engine in the back called Layout. The Layout class determines when to split into a newline factoring in variables such as text size, view dimensions, padding, right-to-left, string content, etc.

You can start off by checking out StaticLayout and overriding the getLineCount() and getLineStart(int) methods. getLineCount() returns the number of lines of text in this layout, while getLineStart(int) returns the string content's character offset of the beginning of the specified line.

Some Noob Student
  • 14,186
  • 13
  • 65
  • 103
  • I'm sorry i really don't understand what you are saying i'm new to android and thought this simple task would be a lot easier to implement – Phillip Nov 27 '14 at 00:02