How to clear the edittext when onclick on Button

83,776

Solution 1

Try

 public void clear(View v) {
     edittext.setText("");
 } 

Where clear is registered as onclick handler for the button in the layout file like this

<ImageButton android:id="@+id/ClearButton"
        android:text="@string/ClearButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="clear"           <<<--------here
        android:src="@drawable/clear"
    />

Solution 2

Button btn=(Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v){
        EditText et=(EditText) findViewById(R.id.et);
        et.setText("");
    }
});

Solution 3

You can use function

myEditText.setText("");

Solution 4

Its simple, I'm just considering that u know how to create the XML file... I'm putting across the java part of it....

public class StackOverFlow extends Activity{

    Button buttonToClick;
    EditText editTextToclear;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    buttonToClick.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            editTextToclear.setText("");


        }
    });
    }

}

Solution 5

if your button name is : rstBtn

& edittext field name is :name

then insert an Onclick Event handler :

rstBtn.setOnClickListener( new OnClickListener() {          
public void onClick(View v){
            name.setText("");
        }
    });
Share:
83,776
Ratna Yellapragadaa
Author by

Ratna Yellapragadaa

Updated on July 05, 2022

Comments

  • Ratna Yellapragadaa
    Ratna Yellapragadaa almost 2 years

    How do I clear the data which is given to an EditText dynamically when clicking the button? How do I write the code? What function or method do I use?