Android editText remove first line break after onclick button Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Android editText remove first line break after onclick button without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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.

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));
        }
    });
We are here to answer your question about Android editText remove first line break after onclick button - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji