Monitor changes in user entry - android

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

Related

Press zero any number of times in edit text, but it should be visible to user in only 1 digit like Calculator

I have an edit text to fill amount. So, I added text watcher, but I don't want user to "0000000..." if user will continuously press "0000..." instead it should show only one time '0' like Calculator
How can I do that?
private void setAmount() {
etAmount.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) {
if (s.length() == 1) {
if (s.toString().equals("0")) {
etAmount.setText("0");
}
}
//Not working
}
#Override
public void afterTextChanged(Editable s) {}
});
}
I think, my logic is also wrong, because when I enter single input and check s.length, and after et.setText("0"), it will go to infinite loop.
Try this:
etAmount.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) {
String text = s.toString();
if (text.length() == 2 && text.charAt(0) == '0') {
etAmount.setText(text.substring(1));
etAmount.setSelection(1);
}
}
});
You can do it like this:
etAmount.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) {}
#Override
public void afterTextChanged(Editable s) {
if (s.length() == 2) {
if (s.toString().startsWith("00")) {
etAmount.setText("0");
}
}
}
});

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

Automatically passing focus betwen EditText

Since my question asking about separating characters that are typed in an EditText into groups or blocks didn't receive much attention, I've come here with another question on an alternative way to accomplish what I need.
So, my idea is to have four EditText that have 4 char length. Now what I want is to change the focus as the user type, from an EdutText to another when the limit is reached.
Example: Let's say we have EditText A, B, C, D; the first to gain focus is EditText A, then when the chars length at A reach 4, the focus passes to EditText B and so on and stops At D.
I'm trying something like the code bellow, but the focus is not requested when the length reaches 4 chars.
inputFieldA.requestFocus();
inputFieldA.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.toString().trim().length() == 4){
inputFieldB.requestFocus();
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
inputFieldB.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.toString().trim().length() == 4){
inputFieldC.requestFocus();
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
inputFieldC.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.toString().trim().length() == 4){
inputFieldD.requestFocus();
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
inputFieldD.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.toString().trim().length() == 4){
//send all to Main EditText (A + B + C + D)
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
So I'm here asking if there is a way to do that.
Write your conditions in afterTextChanged method and make changes as follows :
inputFieldA.requestFocus();
inputFieldA.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) {
if(inputFieldA.getText().toString().trim().length() == 4){
inputFieldB.requestFocus();
}
}
});
inputFieldB.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) {
if(inputFieldB.getText().toString().trim().length() == 4){
inputFieldC.requestFocus();
}
}
});
inputFieldC.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) {
if(inputFieldC.getText().toString().trim().length() == 4){
inputFieldD.requestFocus();
}
}
});
inputFieldD.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) {
if(inputFieldD.getText().toString().trim().length() == 4){
//send all to Main EditText (A + B + C + D)
}
}
});
if(inputFieldA.toString().trim().length() == 4){
inputFieldB.requestFocus();
}
replace this lines in onTextChange() methode.
instead of adding validation on the basis of CharSeq length add validation on the basis of edittext text like this:
replace
if(s.toString().trim().length() == 4)
with
if (currEdittext.getText().toString().length() >= 4)
Note: update your condition as well.
currEdittext will be either inputFieldA, inputFieldB, inputFieldC depending on your requirement

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

How to define Textchanged event for edit text?

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

Categories

Resources