Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 994
user3235688 Avatar asked Feb 13 '23 23:02

user3235688


1 Answers

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;
}
like image 131
ramaral Avatar answered Feb 24 '23 00:02

ramaral