Skip to content

Android editText remove first line break after onclick button

I’ve been trying to search for solution for this but ain’t lucky enough to find a correct answer.

So my problem is.. I want to remove the first Line break in my EditText after clicking a certain button.

First Line <- This should be removed after I click the button and the rest will remain.
Second Line
Third Line
Fourth Line
EditText txt = findViewById(;
Button btn = findViewById(;

btn.setOnClickListener(new Button.OnClickListener(){
   public void onClick(View v) {
      String firstLine = txt.getText().toString().substring(0, txt.getText().toString().indexOf("n"));
      String removeFirstLine = firstLine.substring(0, firstLine.length()-1);

But this leaves only 1 line each time I click the button.



I believe that this will do what you want:-

    btn.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View view) {
User contributions licensed under: CC BY-SA
3 People found this is helpful