Search code examples
androidandroid-edittext

Print EditText output in \n format - android


I enter edittext text and I enter three lines of text. The output of these lines is as follows "Line1 line2 Line3 ". But I want it to be printed as" Line1 \ nLine \ 2Line \ 3 ".

 final EditText edittext = new EditText(getContext());

Solution

  • If the output you want to achieve is this

    Line 1
    Line 2
    Line 3
    

    Use this code

    String text = edittext.getText().toString();
    List<String> splittedText = text.split(" ");
    String formattedText = "";
    for (int i = 0; i < splittedText.size(); i++) {
        formattedText += splittedText[i] + "\n";
    }
    Toast.makeText(this, formattedText, Toast.LENGTH_SHORT).show();