user2374527 user2374527 - 1 year ago 108
Java Question

How to clear the string in the editText when clicking on it

I really wish that you can help me. I my application in Android to clear or erase the the default string assigned to the editText object on the main Activity when clicking on it, only if the value is equal to this text "Enter a message here", which I assigned it in the file as "R.string.editTextOnStart". Here's the code:

public class MainActivity extends AppCompatActivity {

private String text;
protected void onCreate(Bundle savedInstanceState) {

final TextView textView = (TextView) findViewById(;
final EditText editText = (EditText) findViewById(;



//if(editText.getText().toString().equals(String.valueOf(R.string.editTextOnStart))) {

editText.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
text = editText.getText().toString();

if(text.equals(R.string.editTextOnStart)) {
Button button = (Button) findViewById(;
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {

Answer Source

Instead of doing that, you can specify the following using hint

In your layout xml file , under your EditText, add this

android:hint="Enter your text here"

This will appear on your edit text when you start the activity and disappears when user have clicked on it

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download