Android on Text Change Listener

android on Text Change Listener

You can add a check to only clear when the text in the field is not empty (i.e when the length is different than 0).

field1.addTextChangedListener(new TextWatcher() {

@Override
public void afterTextChanged(Editable s) {}

@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(s.length() != 0)
field2.setText("");
}
});

field2.addTextChangedListener(new TextWatcher() {

@Override
public void afterTextChanged(Editable s) {}

@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(s.length() != 0)
field1.setText("");
}
});

Documentation for TextWatcher here.

Also please respect naming conventions.

How to properly use edit text change listener in android?

It stays visible because searchusersedittext is always non-null in that call (if it were null, the earlier call to searchusersedittext.addTextChangedListener would fail with a NullPointerException), so once you type anything at all it only goes into the first case every time (keeping it visible).

Also searchusersedittext.equals("") isn't checking for an empty entry in the EditText, since searchusersedittext is an EditText object, not the entered string. The EditText object continues to exist, even if there is no text entered in it.

Try something like this:

@Override
public void afterTextChanged(Editable s) {
String txt = searchusersedittext.getText().toString();
// or String txt = s.toString();
if( !txt.isEmpty() ) {
recyclerViews.setVisibility(View.VISIBLE);
filter(txt);
}
else {
recyclerViews.setVisibility(View.INVISIBLE);
}
}

Text Change Listener for EditText in custom adapter

If you would use setTextChangedListener if it exists, or remove all TextChangedListeners from a view before you add your new one, it will probably work.

These views are recycled, but you're adding new listeners to them all the time. I think that might be the problem.

How can I add TextChange Listener to all EditText?

Add a CustomEditText class

class CustomEditText : EditText {

private var mContext: Context? = null
private var attributeSet: AttributeSet? = null

constructor(context: Context) : super(context) {
this.mContext = context
initCustomEditText()
}

constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
this.mContext = context
this.attributeSet = attrs
initCustomEditText()
}

constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
this.mContext = context
this.attributeSet = attrs
initCustomEditText()
}

private fun initCustomEditText() {

this.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
}
override fun afterTextChanged(arg0: Editable) {
if(context is MainActivity){
(context as MainActivity).tI_description_auxiliary_charges_info.isErrorEnabled=false
}else if(context is MainActivity2){
(context as MainActivity2).tI_description_auxiliary_charges_info.isErrorEnabled=false
}
// ... other Activity
}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
})

}

}

Make sure -

  1. Your MainActivity (don't know yours since you haven't mentioned) and tI_description_auxiliary_charges_info both are public

  2. All your EditText in XML change to like com.a4plus1.com.dhq_app.Components.CustomEditText (package name.CustomEditText)

Why is my Text Change Listener not working?

You can set in tow way

First Way:

 EditText et_auto_complete_edit_text;
et_auto_complete_edit_text = findViewById(R.id.et_auto_complete_edit_text);
et_auto_complete_edit_text.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {
s.toString()
}
});

XML:

 <EditText
android:id="@+id/et_auto_complete_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_weight="8"
android:background="@color/gray_light_EA"
android:drawableRight="@mipmap/ic_search_new"
android:drawablePadding="@dimen/margin5dp"
android:hint="Search here"
android:inputType="text"
android:maxLines="1"
android:padding="10dp"
android:textColor="@color/black"
android:textColorHint="@color/grey"
android:visibility="visible" />

Second Way :

      SearchView search;
search = findViewById(R.id.search);
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
fetchData(query);
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
//collectionProductsListAdapter.getFilter().filter(newText);
return false;
}
});
search.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
search.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
return false;
}
});

XML:

<androidx.appcompat.widget.SearchView
xmlns:n2="http://schemas.android.com/tools"
android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
n2:searchIcon="@mipmap/ic_et_search" />

Hope this may help you first way is more easy.

How to fix text change listener error when I implement it?

I think the error is that your XML code has the edit text labeled as fiftythres. In your code, you have it as:

fiftythinput = rootView.findViewById(R.id.fiftyinput);

What you need to do is this:

fiftythinput = (EditText)rootView.findViewById(R.id.fiftythinput);

Hope it helps! If it doesn't, inform me.

Changing the text in addTextChangedListener listener of edit text in android gives error

Changing EditText in TextWatcher is not good idea at all.

When you setting text to editText your TextWatcher will call again .

Note:- And it will goes in infinite loop.

Solution for this:-

Use string value as a result in TextWacher and change your ExitText outside text-watcher.

Using TextView you can do like this .....

Create TextView in you layout like below.

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">


<EditText
android:id="@+id/edit_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/border_edittext"
android:gravity="center"
android:inputType="number"
android:padding="10dp"
android:singleLine="true" />



<TextView
android:id="@+id/text_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/border_edittext"
android:gravity="center"
android:inputType="number"
android:padding="10dp"
android:singleLine="true" />



</FrameLayout>

and use textWatcher and Update your TextView text inside onTextChange() method

Another Solution :- Create XMl like this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">

<EditText
android:id="@+id/code"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:background="@drawable/border_edittext"
android:gravity="center"
android:inputType="number"
android:padding="10dp"
android:text=""
android:singleLine="true" />

<EditText
android:id="@+id/mobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/border_edittext"
android:gravity="center"
android:inputType="number"
android:padding="10dp"
android:singleLine="true" />


</LinearLayout>

and update your editText with id code in your textWatcher of your editText with id mobile.

android edittext onchange listener

First, you can see if the user finished editing the text if the EditText loses focus or if the user presses the done button (this depends on your implementation and on what fits the best for you).
Second, you can't get an EditText instance within the TextWatcher only if you have declared the EditText as an instance object. Even though you shouldn't edit the EditText within the TextWatcher because it is not safe.

EDIT:

To be able to get the EditText instance into your TextWatcher implementation, you should try something like this:

public class YourClass extends Activity {

private EditText yourEditText;

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

setContentView(R.layout.main);
yourEditText = (EditText) findViewById(R.id.yourEditTextId);

yourEditText.addTextChangedListener(new TextWatcher() {

public void afterTextChanged(Editable s) {

// you can call or do what you want with your EditText here

// yourEditText...
}

public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
}
}

Note that the above sample might have some errors but I just wanted to show you an example.

On setting Edit Text box text with respect to change in another Edit text and Vice versa in real time changing

Adding text watcher at the same time may cause a problem (Recurring effect). But you can achieve your requirement by activating text watcher one at a time. While edittext1 text changes we will remove text watcher for edittext2 and we will set edittext2 in onTextChanged of text watcher of edittext1. then we will reactivate text watcher of edittext2 in afterTextChanged text watcher of edittext1. the same logic applies to edittext1.

 public class MainActivity extends AppCompatActivity {
EditText editText1,editText2,editText3,editText4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1=findViewById(R.id.editText1);
editText2=findViewById(R.id.editText2);
editText3=findViewById(R.id.editText3);
editText4=findViewById(R.id.editText4);
editText1.addTextChangedListener(textWatcher1);
editText2.addTextChangedListener(textWatcher2);
editText3.addTextChangedListener(textWatcher3);
editText4.addTextChangedListener(textWatcher4);
}

TextWatcher textWatcher1=new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
editText2.removeTextChangedListener(textWatcher2);
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
editText2.setText(editText1.getText());
}

@Override
public void afterTextChanged(Editable s) {
editText2.addTextChangedListener(textWatcher2);
}
};

TextWatcher textWatcher2=new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
editText1.removeTextChangedListener(textWatcher1);
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
editText1.setText(editText2.getText());
}

@Override
public void afterTextChanged(Editable s) {
editText1.addTextChangedListener(textWatcher1);
}
};
}

TextWatcher textWatcher3=new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
editText4.removeTextChangedListener(textWatcher4);
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
editText4.setText(editText3.getText());
}

@Override
public void afterTextChanged(Editable s) {
editText4.addTextChangedListener(textWatcher4);
}
};

TextWatcher textWatcher4=new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
editText3.removeTextChangedListener(textWatcher3);
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
editText3.setText(editText4.getText());
}

@Override
public void afterTextChanged(Editable s) {
editText3.addTextChangedListener(textWatcher3);
}
};
}


Related Topics



Leave a reply



Submit