user3235688
user3235688

Reputation: 45

android: limit of 10 characters per line TextView

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

Upvotes: 4

Views: 7094

Answers (3)

Lokanath
Lokanath

Reputation: 682

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;
}

Upvotes: 2

ramaral
ramaral

Reputation: 6179

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;
}

Upvotes: 6

Or Bar
Or Bar

Reputation: 1566

Add the following 2 attributes to the TextView definition:

android:singleLine="false"  
android:maxems="10"

Upvotes: 0

Related Questions