I have an app in which i have lots of edit text but in some edit text i only want to implement only one method of TextWatcher. How do i do that kindly guide.
code:-
private TextWatcher m_textWatcher = 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) {
isFieldsEmpty();
}
#Override
public void afterTextChanged(Editable s) {
m_mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = m_mainLayout.getRootView().getHeight() - m_mainLayout.getHeight();
if (!(heightDiff > RewardUtil.dpToPx(mContext, 200))) { // if more than 200 dp, it's probably a keyboard...
m_otpEditText.clearFocus();
}
}
});
}
};
m_otpEditText.addTextChangedListener(m_textWatcher);
Create metod lke this.
public static class MyTextWatcher implements TextWatcher {
private EditText mEditText;
public MyTextWatcher(EditText editText) {
mEditText = editText;
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
oldText = mEditText.toString();
}
....
}
And use like this...
m_otpEditText.addTextChangedListener(new MyTextWatcher(mFirstEditText));
It will help you.
Thanks.
First of all, you can create a class as follows :
public class CustomTextWatcher implements 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) {
}
}
Thereafter, just override the method/s you need. Suppose you need only onTextChanged method, then you can do as follows :
m_otpEditText.addTextChangedListener(new CustomTextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
TextWatcher class gives you flexibility to override methods whichever you need. If you really need only one method then override only one, but you have to always implement all methods .
Related
when I tested adding two TextWatchers to one EditText, it seems like first TextWatcher that is registered gets ignored and only TextWatcher that is registered last works fine. for example,
myEditText.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(...) {}
override fun onTextChanged(...) {}
override fun afterTextChanged(s: Editable) { // first logic here}
})
then I add one more TextWatcher to myEditText
myEditText.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(...) {}
override fun onTextChanged(...) {}
override fun afterTextChanged(s: Editable) { // second logic here}
})
now first logic gets ignored and only second logic remains and works.
I wonder if there is a way to make both of them work together.
(if someone who has experience of hbb20 ccp library, could you also take a look at this thread and get me some help as well?)
I appreciate your help in advanced.
Edit:
my real problem was at logic1 and logic2 not on two TextWatchers. logic1 and logic2 were trying to update one same variable with two different conditions. my code was something like this(which was the problem).
button1.isEnable = s.toString().length == 2 // in first TextWatcher
button1.isEnable = s.toString().length == 3 // in second TextWatcher
so actually second TextWatcher's condition was overriding first TextWatcher's condition.
Conclusion: multiple TextWatchers on single EditText will work just fine
my questions was wrong since my logic was wrong. but thanks to all who tried to help me!
public interface MyTextWatcher {
public void beforeTextChanged(CharSequence s, int start,
int count, int after);
public void onTextChanged(CharSequence s, int start, int before, int count);
public void afterTextChanged(Editable s);
}
Now you can use multiple TextWatcher such as below :
public class TextWatcherActivity {
EditText editText;
List<MyTextWatcher> myTextWatcher = new ArrayList<>();
public void init() {
myTextWatcher.add(new MyTextWatcher() {
#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) {
}
});
myTextWatcher.add(new MyTextWatcher() {
#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) {
}
});
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
for (MyTextWatcher textWatcher : myTextWatcher) {
textWatcher.beforeTextChanged(s, start, count, after);
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
for (MyTextWatcher textWatcher : myTextWatcher) {
textWatcher.onTextChanged(s, start, before, count);
}
}
#Override
public void afterTextChanged(Editable s) {
for (MyTextWatcher textWatcher : myTextWatcher) {
textWatcher.afterTextChanged(s);
}
}
});
}
}
I am wondering how to implement Interface Segregation Principle from SOLID for the TextWatcher.
More specific: How to remove the not needed functions:
beforeTextChanged(),
afterTextChanged()
as I only need:
onTextChanged()
passwordinput.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) {
presenter.validateCredentials(emailinput.getText().toString(), passwordinput.getText().toString());
}
#Override
public void afterTextChanged(Editable s) {
}
});
Create a class NewClass that inherits from MyTextWatcher
and
passwordinput.addTextChangedListener(new New Class() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
presenter.validateCredentials(emailinput.getText().toString(), passwordinput.getText().toString());
}
});
You may create your own TextWatcher class by implementing TextWatcher and leave the default implementations empty. Then in your anonymous implementation only override onTextChanged().
class MyTextWatcher implements 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) {
}
}
Usage:
passwordinput.addTextChangedListener(new MyTextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
presenter.validateCredentials(emailinput.getText().toString(), passwordinput.getText().toString());
}
});
Currently I am learning to code and came across one problem. I need my upload button to be disabled untill Edit Text field is somekind of symbol than default hint
Full code
private TextWatcher EmptyEdit = 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) {
if (s.toString().trim().length()==0)
{
mUpload.setEnabled(false);
} else {
mUpload.setEnabled(true);
}
}
#Override
public void afterTextChanged(Editable s) {
}
};
Add your code in afterTextChanged() instead of onTextChanged().
private TextWatcher EmptyEdit = 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) {
if (s.toString().trim().length()==0)
{
mUpload.setEnabled(false);
} else {
mUpload.setEnabled(true);
}
}
};
Do your code inside afterTextChanged().
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
mUpload.setEnabled(!TextUtils.isEmpty(s.toString().trim()))
}
// don't forget to set listner
editText.addTextChangedListener(EmptyEdit);
yourEditText.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) {
//Disable your button in here
}
});
I set TextWatcher to EditText like below. But when I try to clear text, cursor is coming to start after clearing every letter.
class MyInputWatcher implements 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) {
et.removeTextChangedListener(watcher2);
et.setText(s.toString().replaceAll("[^[:alpha:]]", ""));
et.addTextChangedListener(watcher2);
}
#Override
public void afterTextChanged(Editable s) {
}
}
Do it like this (UPDATED):
class MyInputWatcher implements 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) {
String temp = s.toString().replaceAll("[^a-zA-Z]", "");
if (s.toString().length() != temp.length()) {
et.setText(temp);
et.setSelection(temp.length());
}
}
#Override
public void afterTextChanged(Editable s) {
}
}
Please try like this
editText.setSelection(editText.getText().toString().length());
Subhash Kumar, you can use method:
et.setSelection(position)
for displaying cursor in need position
Set position to your cursor on afterTextChanged() method like this.
class MyInputWatcher implements 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) {
et.removeTextChangedListener(watcher2);
et.setText(s.toString().replaceAll("[^[:alpha:]]", ""));
et.addTextChangedListener(watcher2);
}
#Override
public void afterTextChanged(Editable s) {
et.setSelection(et.getText().toString().length())
}
}
Every time you clear a character it calls onTextChanged() method, as your implementation it get the edittext text and back set to it, so the cursor comes to the starting of the text. Clear et.setText(s.toString().replaceAll("[^[:alpha:]]", "")); and it will be fixed. Or use this et.setSelection(et.getText().toString().length+1);
I've an adapter and and I use a ad.getFilter().filter(s); for filter a listView.
It works well if I write a single word in EditText.
It doesn't work (result no row) if a write a space Character after a word.
I'd like to do a filter with name and surmane for example.
I want to insert "Mario Rossi" in edittext and have a result..
I hope that I explained.
Thanks a lot.
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
ad.getFilter().filter(s);
}
};
Your Code is perfect but some small mistake there
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
ad.getFilter().filter(s);
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
};