2

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 R.java file as "R.string.editTextOnStart". Here's the code:

    public class MainActivity extends AppCompatActivity {

    private String text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        editText.setText(R.string.editTextOnStart);

        textView.setText(R.string.textViewString);

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

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

                    if(text.equals(R.string.editTextOnStart)) {
                        editText.getText().clear();
                    }
                }
            });
        //}
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                textView.setText(editText.getText().toString());
            }
        });
    }
}

5 Answers5

3

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

Md. Nasir Uddin Bhuiyan
  • 1,598
  • 1
  • 14
  • 24
Vidya Sagar
  • 170
  • 1
  • 16
2

Better make "Enter a message here" as hint in layout

android:hint="Enter a message here"
Shiv Buyya
  • 3,770
  • 2
  • 30
  • 25
0

It looks like your code is almost perfect, however the issue is that you are comparing the variable text which is a String directly to R.string.editTextOnStart which is an int. To get the actual value you assigned to R.string.editTextOnStart you need to use the method [Context.getString(R.string.editTextOnStart)][1]. So your code should look like:

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

   if(text.equals(getApplicationContext().getString(R.string.editTextOnStart))) {
     editText.setText("");
   }
 }
});
...
Dr. Nitpick
  • 1,662
  • 1
  • 12
  • 16
0

Java

setHint()

-or-

XML

android:hint=""

See here for more info

Community
  • 1
  • 1
Pat Myron
  • 4,437
  • 2
  • 20
  • 39
0

Write this in your onclick listener

((EditText) findViewById(R.id.yoursXmlId)).setText("");

Or set the text hint in your XML layout

android:hint="Enter Name"
Thecarisma
  • 1,424
  • 18
  • 19