Clear Text in Edittext When Entered

Clear text in EditText when entered

First you need to call setContentView(R.layout.main) then all other initialization.

Please try below Code.

public class Trackfolio extends Activity implements OnClickListener {
/** Called when the activity is first created. */
public EditText editText;

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

setContentView(R.layout.main);

editText = (EditText) findViewById(R.id.editText1);
editText.setOnClickListener(this);
}

@Override
public void onClick(View v) {
editText.getText().clear(); //or you can use editText.setText("");
}
}

how to clear EditText in Android?

Add this code to clear all your editText :

    morning.setText("");

noon.setText("");

afternoon.setText("");

night.setText("");

before_meal.setText("");

after_meal.setText("");

hourly.setText("");

days.setText("");

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

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

How can I automatically clear the text in EditText?

What you're referring to isn't actually text that's being set -- it's a hint (which is why it doesn't show after the EditText is focused or text has already been entered). You can set these placeholder hints in your EditText XML, for example:

android:hint="First Name". 

If you're feeling really adventerous, you can also wrap your EditText in a TextInputLayout for a Material Design style floating hint.

If that helps, be sure to accept this answer.

Clearing previously entered text in EditText field

Errr...

The only code I use to achieve 1) is:

    // Clear text when clicked
host.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
host.setText("");
}
});

host just being an EditText. Works just fine? I think you are making this more complicated than it is.

EditText clear text on first focus - Android

Matan, I am not sure if this is what you are looking at, but I think you want to display a 'hint' for your Edit Text

Example

<EditText
.
.
android:hint="Please enter your name here">

For an example check this http://www.giantflyingsaucer.com/blog/wp-content/uploads/2010/08/android-edittext-example-3a.jpg



Related Topics



Leave a reply



Submit