Skip to content
Advertisement

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(R.id.editTextID);
Button btn = findViewById(R.id.btnID);

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);
      txt.setText(removeFirstLine);
   }
});

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

Advertisement

Answer

I believe that this will do what you want:-

    btn.setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View view) {
            txt.setText(txt.getText().toString().substring(txt.getText().toString().indexOf("n")+1));
        }
    });
3 People found this is helpful
Advertisement