How do I automatically clear text in 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 text in EditText so users don’t have to do it themselves. You can see in all “big” websites when you enter text in a text box (for example: “What’s your name?”) ), you will see “Name” in the text box, and when you click on it, it disappears. How do I make text disappear automatically without users manually deleting it?
Solution
You’re not actually referring to the text you’re setting — it’s a hint (that’s why it doesn’t show up after EditText gets focus or text has already been entered). You can set these placeholder hints in the EditText XML, for example:
android:hint="First Name".
If you’re really adventurous, < a href="https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html" rel="noreferrer noopener nofollow" > you can also wrap your EditText in a TextInputLayout for a Material Design style floating hint.
Be sure to accept this answer if it helps.