How to define Textchanged event for edit text? - android

how to define textchanged event for EditText??
Thanks,
Balu.

You need this:
inputView.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});

Related

How to limit specific character in edit text?

My question is that How to limit specific character in edit text?
for example I want to limit # character using max. 3 times in edit text.
How can I do this? Any advice or code sample please.
thanks.
use this
yourEditText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(count>3){
yourEditText.setEnabled(false);
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
Try this
int currHash=0;
edtText.addTextChangedListener(new TextWatcher() {
CharSequence previous;
public void afterTextChanged(Editable source) {
if(source.toString().contains("#") && currHash=2){
source.clear();
source.append(previous.toString());
}
else
if(source.toString().contains("#"))
currHash++;
}
public void beforeTextChanged(CharSequence source, int start, int count, int after) {
previous = source;
}
public void onTextChanged(CharSequence source, int start, int before, int count) {}
});
I found a solution like this.
etDesc.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
hastagCount = s.toString().length() - s.toString().replace("#", "").length();
if(hastagCount<=3){
previousDesc = s.toString();
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
hastagCount = s.toString().length() - s.toString().replace("#", "").length();
if(hastagCount>3){
s.clear();
s.append(previousDesc);
int pos = etDesc.getText().length();
etDesc.setSelection(pos);
etDesc.setFocusable(true);
}
}
});

SOLID - The Interface Segregation Principle

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());
}
});

Disable button if edit text field is empty. Can't get it to working

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
}
});

EditText - Cursor coming to start for every letter when clear text

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);

Monitor changes in user entry

I need a method that can monitor each change in the user in an EditText. To be more specific the EditText in the SearchDialog. Is there any such method?
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
//do something
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
//do something
}
#Override
public void afterTextChanged(Editable s) {
//do something
}
});

Categories

Resources