How to delete the last letter from EditText with button?

34,449

Solution 1

You can retrieve the text of EditText and then get the sub-string of that text and set again that text to EditText as below...

String text = editText.getText().toString();
editText.setText(text.substring(0, text.length() - 1));

You can also use following procedure....it will be more efficient.

int length = editText.getText().length();
if (length > 0) {
    editText.getText().delete(length - 1, length);
}

You should use switch-case as below...and handle your nuttondel onclick() as follows...

public class MainActivity extends Activity implements OnClickListener {

    Button buttona, buttonb;
    Button buttonDel;
    EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText) findViewById(R.id.editText);

        addListenerOnButton();

    }


   public void addListenerOnButton() {

    buttona = (Button) findViewById(R.id.buttona);
    buttona.setOnClickListener(this);

    buttonb = (Button) findViewById(R.id.buttonb);
    buttonb.setOnClickListener(this);

    buttonDel = (Button) findViewById(R.id.buttondel);
    buttonDel.setOnClickListener(this);


   }

    public void onClick(View v) {

       switch(v.getId()) {

          case R.id.buttona:
                      editText.setText(editText.getText().toString()+buttona.getText().toString());
              break;

          case R.id.buttonb:
                      editText.setText(editText.getText().toString()+buttonb.getText().toString());
              break;

          case R.id.buttondel:

              int length = editText.getText().length();
              if (length > 0) {
                  editText.getText().delete(length - 1, length);
              }
              break;
       }

    }

}

Solution 2

Using substring() method , you can do it,

String str = myEditText.getText().toString();
str = str.substring ( 0, str.length() - 1 );
// Now set this Text to your edit text
myEditText.setText ( str );

you need to write above lines in onClick() method.

Share:
34,449
Tamas Koos
Author by

Tamas Koos

Love simplicity.

Updated on September 07, 2020

Comments

  • Tamas Koos
    Tamas Koos almost 4 years

    I have to buttons that writes A and B to an edittext. If there is something in the edittext how can I delete the last letters with the "Del" button ?

    My layout:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    
    <Button
    android:id="@+id/buttonb"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/buttona"
    android:text="@string/buttonb"
    android:textSize="50sp"
    android:textStyle="bold" />
    
    <Button
    android:id="@+id/buttona"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:text="@string/buttona"
    android:textSize="50sp"
    android:textStyle="bold" />
    
    <Button
    android:id="@+id/buttondel"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:text="@string/buttondel"
    android:textSize="50sp"
    android:textStyle="bold" />
    
    <EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:ems="58"
    android:textSize="20sp"
    android:textStyle="bold"
    android:inputType="text" >
    
    <requestFocus />
    </EditText>
    
    </RelativeLayout>
    

    And my java:

    package com.koostamas.keyboard;
    
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.app.Activity;
    
    public class MainActivity extends Activity implements OnClickListener {
    
    Button buttona, buttondel;
    EditText editText;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText = (EditText) findViewById(R.id.editText);
    
    addListenerOnButton();
    }
    
    
       public void addListenerOnButton() {
    
    buttona = (Button) findViewById(R.id.buttona);
    buttona.setOnClickListener(this);
    
    buttondel = (Button) findViewById(R.id.buttonb);
    buttondel.setOnClickListener(this);
    
    
       }
    
    public void onClick(View v) {
    switch(v.getId()) {
        case R.id.buttona : 
            Button buttona = (Button)v;
        editText.setText(editText.getText().toString()+buttona.getText().toString());
            break;
        case R.id.buttondel :
            String text = editText.getText().toString();
            editText.setText(text.substring(0, text.length() - 1));
            break;
    
    
    
    }
    
    }
    
    
    }
    

    How can I do it? Thanks inn advance.

  • Tamas Koos
    Tamas Koos over 10 years
    But where should I put it? Could you write it into the code? Thanks.
  • Tamas Koos
    Tamas Koos over 10 years
    But where should I put it? Could you write it into the code? Thanks.
  • Android
    Android over 10 years
    on button click u want na? then put it on button click event... this will remove last character
  • Tamas Koos
    Tamas Koos over 10 years
    And what is the str variable in the 3rd line?
  • user2060383
    user2060383 over 10 years
    Please tell me, which is your del button ?
  • Tamas Koos
    Tamas Koos over 10 years
    I can't do it. It does nothing.
  • Tamas Koos
    Tamas Koos over 10 years
    Please see my update.
  • Hamid Shatu
    Hamid Shatu over 10 years
    @user2923589...see my updated answer...I think this answer will help you lot.
  • user2060383
    user2060383 over 10 years
    Good, you have done right thing.
  • Android
    Android over 10 years
    then again settext(str1)
  • Tamas Koos
    Tamas Koos over 10 years
    Thanks all. It works.
  • Tamas Koos
    Tamas Koos over 10 years
    Thanks all. It works.
  • Tamas Koos
    Tamas Koos over 10 years
    Thanks all. It works.
  • Gopal Gopi
    Gopal Gopi over 10 years
    An efficient way would be int length = editText.getText().length();if (length > 0) {editText.getText().delete(length - 1, length);}
  • Hamid Shatu
    Hamid Shatu over 10 years
    @GopalRao...thanks for your concern. In have updated my answer. I wouldn't know that.
  • Erich
    Erich about 9 years
    That would cause an error when the EditText is empty. Add if(str.length() > 0) { ... } to catch this case.
  • Rajesh Nasit
    Rajesh Nasit over 6 years
    what if my cursor is at center and want to remove center charactor
  • ingyesid
    ingyesid over 5 years
    i use the efficient way just because the cursor is placed in the correct position