Search code examples
javaandroidandroid-studiocalculatorbackspace

Making backspace button in java that deletes the last character in a Textview


I want to make a button that acts like a backspace but I cant find the perfect code for doing it, like I made a button that adds certain characters to a Textview using append() method in a single line of code instead of making a multiple line thingy. So my question is: is there a way to do it in a backspace button code, I know I can make a multiple line/long code, but is there an opposite to append()?


Solution

  • This works for character backspace:

    buttonBackspace.setOnClickListener(new View.OnClickListener() {
    
            public void onClick(View view) {
               String word = editText.getText().toString();               
               int input = word.length();
                  if (input > 0){
                     editText.setText(word.substring(0, input-1));
                  }          
               }
    });