4

I read value from EditText and write it to TextView

editTitle1.addTextChangedListener(new TextWatcher() {
              public void afterTextChanged(Editable s) {
              }

              public void beforeTextChanged(CharSequence s, int start, int count, int after) {
              }

              public void onTextChanged(CharSequence s, int start, int before, int count) {
                  s = editTitle1.getText().toString();
                  textTitle1.setText(s);

              }
           });

And I want that in one line - maximum 10 characters, so if all 20 characters - it's two line in TextView with 10 chars per line. How I can do this? I try android:maxLength="10" but it does not help

ramaral
  • 6,149
  • 4
  • 34
  • 57
user3235688
  • 45
  • 1
  • 3

3 Answers3

6

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;
}
ramaral
  • 6,149
  • 4
  • 34
  • 57
2

I edited the answer given by ramaral he used wrong escape sequence

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;
}
Lokanath
  • 662
  • 5
  • 18
0

Add the following 2 attributes to the TextView definition:

android:singleLine="false"  
android:maxems="10"
Or Bar
  • 1,566
  • 11
  • 12
  • The result is the same:( – user3235688 Jan 26 '14 at 15:19
  • Try setting the `android:maxWidth="size"` instead of max ems, and replace the "size" with the size you prefer. For example, 100dip. – Or Bar Jan 26 '14 at 15:42
  • 1
    you can also just write a simple function to add a newline character after every 10 characters [like here](http://stackoverflow.com/a/537190/2333395) – Or Bar Jan 26 '14 at 15:46