Clear text in EditText when entered [duplicate]
Solution 1:
Also you can use code below
editText.getText().clear();
Solution 2:
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("");
}
}
Solution 3:
just use the android:hint
attribute in your EditText. This text shows up when the box is empty and not focused, but disappears upon selecting the EditText box.
Solution 4:
We can clear EditText data in two ways
First One setting EditText is empty like below line
editext.setText("");
Second one clearing EditText data like this
editText.getText().clear();
I suggest second way
Solution 5:
Your code should be:
public class Project extends Activity implements OnClickListener {
/** Called when the activity is first created. */
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) {
// TODO Auto-generated method stub
if(v == editText) {
editText.setText("");
}
}
}