How can I automatically clear the text in an EditText?

final TextView textView = (TextView) findViewById(R.id.textView);

final EditText editText = (EditText) findViewById(R.id.editText);

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        String etString = editText.getText().toString();
        textView.setText(etString);
    }
});

      

I'm new to coding and I don't know how to automatically clear the text in the EditText so that the user doesn't do it himself. You can see on all "big" websites that when you enter text in a text box (ex: "What's your name?") You see "name" in the text box, and when you click on it, it disappears. How can I automatically delete text if the user doesn't have to manually delete it?

+3


source to share


2 answers


What you mean is not the text that is being set - it is the tooltip (which is why it does not appear after the EditText is focused or the text has already been entered). You can set these links in the EditText XML editor, for example:

android:hint="First Name". 

      



If you feel really adventurous, you can also wrap your EditText in a TextInputLayout for a Material Design style floating tooltip .

If that helps, be sure to accept this answer.

+4


source


You can do this grammatically by changing your code to use the hint property:

editText.setText("");
editText.setHint(<string>);

      



also you can change the color by adding

editText.setHintTextColor(<some color>);

      

+2


source







All Articles