Jaimin Modi
Jaimin Modi

Reputation: 1677

Erasing Character from EditText on Button click

I created an Application to dial particular contact number. It has one EditText and ten buttons for the digits from 0 to 9 and a BACK button. I want to erase single digit from EditText on each click event of BACK button. Is there any way to do so ?

Upvotes: 0

Views: 972

Answers (1)

subhash
subhash

Reputation: 2707

Namaskar modiji, try

myButton.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View view){
     String text = et.getText().toString();
     if(!TextUtils.isEmpty(text)){
                String newText = text.substring(1, text.length()); //delete from left
                //or
                String newText1 = text.substring(0, text.length() - 1); //delete from right
                et.setText(newText);
                et.setSelection(newText.length());
                //or
                et.setText(newText1);
                et.setSelection(newText1.length());

     } 
   }
}

Upvotes: 2

Related Questions