Clear text in EditText when entered
AndroidAndroid EdittextAndroid Problem Overview
I'm trying to set and onclicklistener so that when I click within the edittext element it will clear its current contents. Is there something wrong here? When I compile this code I get a force quit and ActivityManager: Can't dispatch DDM chunk 4d505251: no handler defined error.
public class Project extends Activity implements OnClickListener {
/** Called when the activity is first created. */
EditText editText = (EditText)findViewById(R.id.editText1);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
editText.setOnClickListener(this);
setContentView(R.layout.main);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
editText.setText("");
}
}
Android Solutions
Solution 1 - Android
Also you can use code below
editText.getText().clear();
Solution 2 - Android
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 - Android
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 - Android
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 - Android
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("");
}
}
}
Solution 6 - Android
public EditText editField;
public Button clear = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text_layout);
this. editField = (EditText)findViewById(R.id.userName);
this.clear = (Button) findViewById(R.id.clear_button);
this.editField.setOnClickListener(this);
this.clear.setOnClickListener(this);
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.clear_button){
//setText will remove all text that is written by someone
editField.setText("");
}
}
Solution 7 - Android
Very Simple to clear editText values.when u click button then only follow 1 line code.
> Inside button or anywhere u want.Only use this
editText.setText("");
Solution 8 - Android
For Kotlin:
Create two extensions, one for EditText and one for TextView
EditText:
fun EditText.clear() { text.clear() }
TextView:
fun TextView.clear() { text = "" }
and use it like
myEditText.clear()
myTextView.clear()
Solution 9 - Android
i don't know what mistakes i did while implementing the above solutions, bt they were unsuccessful for me
txtDeck.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
txtDeck.setText("");
}
});
This works for me,
Solution 10 - Android
package com.example.sampleproject;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class SampleProject extends Activity {
EditText mSearchpeople;
Button mCancel , msearchclose;
ImageView mprofile, mContact, mcalender, mConnection, mGroup , mFollowup , msetting , mAddacard;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
mSearchpeople = (EditText)findViewById(R.id.editText1);
mCancel = (Button)findViewById(R.id.button2);
msearchclose = (Button)findViewById(R.id.button1);
mprofile = (ImageView)findViewById(R.id.imageView1);
mContact = (ImageView)findViewById(R.id.imageView2);
mcalender = (ImageView)findViewById(R.id.imageView3);
mConnection = (ImageView)findViewById(R.id.imageView4);
mGroup = (ImageView)findViewById(R.id.imageView5);
mFollowup = (ImageView)findViewById(R.id.imageView6);
msetting = (ImageView)findViewById(R.id.imageView7);
mAddacard = (ImageView)findViewById(R.id.imageView8);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mSearchpeople.clearFocus();
}
});
}
}
Solution 11 - Android
//To clear When Clear Button is Clicked
>firstName = (EditText) findViewById(R.id.firstName);
clear = (Button) findViewById(R.id.clearsearchSubmit);
clear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.clearsearchSubmit);
firstName.setText("");
}
});
This will help to clear the wrong keywords that you have typed in so instead of pressing backspace again and again you can simply click the button to clear everything.It Worked For me. Hope It Helps
Solution 12 - Android
final EditText childItem = (EditText) convertView.findViewById(R.id.child_item);
childItem.setHint(cellData);
childItem.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
//Log.d("NNN", "Has focus " + hasFocus);
if (hasFocus) {
Toast.makeText(ctx.getApplicationContext(), "got the focus", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(ctx.getApplicationContext(),
"loss the focus", Toast.LENGTH_SHORT).show();
}
return;
});
Solution 13 - Android
by setting Empty string you can clear your edittext
editext.setText("");
Solution 14 - Android
In XML you can write like:
<EditText
android:id="@+id/txtsearch"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_weight="1"
android:background="@drawable/roundlayoutbutton1"
android:ems="10"
android:gravity="center"
android:inputType="text"
android:textAllCaps="true"
android:text="search_xxxx"
android:textColor="@color/colorPrimaryDark"
android:visibility="visible" />
and in java class you may have below one :
EditText searchHost;
OnCreate() you write:
searchHost=findViewById(R.id.txtsearch);
searchHost.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(searchHost.getText().toString().equalsIgnoreCase("search_xxxx")){
searchHost.setText("");
Toast.makeText(getApplicationContext(),"Enter you text xxx...", Toast.LENGTH_LONG).show();
}
}
});
It works fine for me.
Solution 15 - Android
You can use the 'android:hint' attribute in your EditText also from code:
editText.setHint(CharSequence hint / int resid);
Then you don't need any onClickListener or similar. But consider that the hint value won't be passed. The editText will be stayed empty. In this case you can set your editText with your deflault value:
if(editText.getText().toString().equals("")) {
...use your default value instead of the editText... }
Solution 16 - Android
If the use of EditText is not mandatory, you can implement this behavior easily with the new material components:
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_field"
app:endIconDrawable="@drawable/ic_close_black_24dp"
app:endIconMode="clear_text"
app:endIconTint="@color/black">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_value"
android:maxLines="1"
android:text="@{itemModel.value}" />
</com.google.android.material.textfield.TextInputLayout>
You only have to specify the drawable you want for the button that will clear the text and the action that it will execute. To clear the text, you can use iconMode="clear_text", but also "password_toggle" is available.
Solution 17 - Android
It's simple: declare the widget variables (editText
, textView
, button
etc.) in class but initialize it in onCreate
after setContentView
.
The problem is when you try to access a widget of a layout first you have to declare the layout. Declaring the layout is setContentView
.
And when you initialize the widget variable via findViewById
you are accessing the id
of the widget in the main layout in the setContentView
.
I hope you get it!
Solution 18 - Android
I am not sure if your searching for this one
{
<EditText
.
.
android:hint="Please enter your name here">
}