How to make on Textwatcher for multiple Editext? - android

I have three Edittext named as mobile,password,email for these i was implemented seperate Textwatcher for them, But I want to make single Textwatcher for multiple Edittext.How can I do that to acheive.
code:-
private final TextWatcher m_EmailWatcher = 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) {
checkForEmptyField();
}
#Override
public void afterTextChanged(Editable s) {
/*check whether SoftKeyPad open or hide*/
m_MainLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = m_MainLayout.getRootView().getHeight() - m_MainLayout.getHeight();
//noinspection StatementWithEmptyBody
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
} else {/*if hide then clear focus from Password Edit text*/
m_EmailEditText.clearFocus();
m_Email = m_EmailEditText.getText().toString().trim();
if (isValidEmail(m_Email) && m_Email.split("#")[0].length() <= 64 && (m_Email.split("#")[1].length() >= 2 && m_Email.split("#")[1].length() <= 255)) {
} else {
m_EmailEditText.setError("Please enter valid Email Id");
}
}
}
});
}
};
private final TextWatcher m_PassWordWatcher = 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) {
checkForEmptyField();
}
#Override
public void afterTextChanged(Editable s) {
}
};
private final TextWatcher m_MobileWatcher = 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) {
checkForEmptyField();
}
#Override
public void afterTextChanged(Editable s) {
}
};

Create a separate Class extending TextWatcher.
public class CustomTextWatcher implements TextWatcher {
private EditText editText;
private Context context;
private int value;
public CustomTextWatcher(Context context, EditText et, int val){
this.context = context;
this.editText = et;
this.value = val;
}
#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().length() > 0){
if(value == 0){
//Email
}
else if(value == 1){
//Mobile Number
}
else if(value == 2){
//Password
}
}
}
#Override
public void afterTextChanged(Editable s) {
}
}
Whenever you want to apply a TextWatcher to an EditText do it like this.
CustomTextWatcher cew1 = new CustomTextWatcher(this, emailEditText, 0);
emailEditText.addTextChangedListener(cew1);

Related

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

how to handle with addTextChangedListener and memory

public class MainActivity extends AppCompatActivity {
EditText Percent, mmolGlic, mgGlic;
double mmol = 0, mg = 0, perc = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Percent = (EditText) findViewById(R.id.percent);
mmolGlic = (EditText) findViewById(R.id.mmol_glic);
mgGlic = (EditText) findViewById(R.id.mg_glic);
/*mmolGlic.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) {
frommMol();
}
#Override
public void afterTextChanged(Editable s) {
}
});*/
Percent.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) {
fromPercent();
}
#Override
public void afterTextChanged(Editable s) {
}
});
/*mgGlic.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) {
frommg();
}
});*/
}
public void frommMol() {
mmol = Double.parseDouble(mmolGlic.getText().toString());
perc = (mmol/10.929) + 2.15;
Percent.setText(String.format( "%.2f", perc ));
}
public void fromPercent(){
perc = Double.parseDouble(Percent.getText().toString());
mmol = (perc - 2.15) * 10.929;
mmolGlic.setText(String.format( "%.2f", mmol ));
mg = (perc*28.7) - 46.7;
mgGlic.setText(String.format( "%.2f", mg ));
}
public void frommg(){
mg = Double.parseDouble(mgGlic.getText().toString());
perc = (mg + 46.7) / 28.7;
Percent.setText(String.format( "%.2f", perc ));
}
}
Goodmorning everyone :)
this is a continuous question from this one: Question 1
this an example code of what i'm trying to do. But i have some problems. I think most of them are for the logic of variables and how to handle the input in more EditTexts. for example:
the main problem is that i can't use more than one addTextChangedListener. I try to explain better: if i leave the code as here, the app crashes. I am not sure, maybe it's due to how i handle the three EditTexts.
then i have a problem when i delete the text: if i have "5.99" and i
press del, when it's deleting it deletes until the 5 and then
crashes. Probaly i should set when the text field is empty the
variables = 0.
can you help me? thank you very much
public class MainActivity extends AppCompatActivity {
EditText Percent, mmolGlic, mgGlic;
double mmol = 0, mg = 0, perc = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Percent = (EditText) findViewById(R.id.percent);
mmolGlic = (EditText) findViewById(R.id.mmol_glic);
mgGlic = (EditText) findViewById(R.id.mg_glic);
Percent.addTextChangedListener(percentWatcher);
mmolGlic.addTextChangedListener(mmolGlicTextWatcher);
mgGlic.addTextChangedListener(mgGlicWatcher);
}
public void frommMol() {
if (!mmolGlic.getText().toString().trim().isEmpty()) {
mmol = Double.parseDouble(mmolGlic.getText().toString());
perc = (mmol / 10.929) + 2.15;
Percent.removeTextChangedListener(percentWatcher);
Percent.setText(String.format("%.2f", perc));
Percent.addTextChangedListener(percentWatcher);
}
}
public void fromPercent() {
if (!Percent.getText().toString().trim().isEmpty()) {
perc = Double.parseDouble(Percent.getText().toString().trim());
mmol = (perc - 2.15) * 10.929;
mmolGlic.removeTextChangedListener(mmolGlicTextWatcher);
mgGlic.removeTextChangedListener(mgGlicWatcher);
mmolGlic.setText(String.format("%.2f", mmol));
mg = (perc * 28.7) - 46.7;
mgGlic.setText(String.format("%.2f", mg));
mmolGlic.addTextChangedListener(mmolGlicTextWatcher);
mgGlic.addTextChangedListener(mgGlicWatcher);
}
}
public void frommg() {
if (!mgGlic.getText().toString().trim().isEmpty()) {
mg = Double.parseDouble(mgGlic.getText().toString());
perc = (mg + 46.7) / 28.7;
Percent.removeTextChangedListener(percentWatcher);
Percent.setText(String.format("%.2f", perc));
Percent.addTextChangedListener(percentWatcher);
}
}
private TextWatcher percentWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
fromPercent();
}
#Override
public void afterTextChanged(Editable editable) {
}
};
private TextWatcher mgGlicWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
frommg();
}
#Override
public void afterTextChanged(Editable editable) {
}
};
private TextWatcher mmolGlicTextWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
frommMol();
}
#Override
public void afterTextChanged(Editable editable) {
}
};
}
Hope this will help you.
well what it looks like, that you are doing your operation even if you don't have any text to work with.. i would recommend, do a null/empty check on the editTextView before you do thefromPercent(); operation. Hope that helps.

How to multiply values of two edittext on textchangelistner and show result in anthor edittext and vise versa in android

I multiplied the values in two edittext et_weight and et_rate and got result in the other edittext.Now i want to divide et_freight and et_weight to get result in et_rate when i change value in et_freight.Can anyone suggest a method
et_weight.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 (!et_rate.getText().toString().equals("") && !et_weight.getText().toString().equals("")) {
et_freight.setText(String.valueOf(Integer.valueOf(et_weight.getText().toString()) * Integer.valueOf(et_rate.getText().toString())));
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
et_rate.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 (!et_rate.getText().toString().equals("") && !et_weight.getText().toString().equals("")) {
et_freight.setText(String.valueOf(Integer.valueOf(et_weight.getText().toString()) * Integer.valueOf(et_rate.getText().toString())));
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
et_freight.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 (!et_freight.getText().toString().equals("") && !et_weight.getText().toString().equals("")) {
et_rate.setText(String.valueOf(Integer.valueOf(et_freight.getText().toString()) / Integer.valueOf(et_weight.getText().toString())));
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
You could do this by checking if the EditText is focused before changing it's value. For example the first condition:
if (et_weight.isFocused() && !et_rate.getText().toString().equals("") && !et_weight.getText().toString().equals("")) {
Also, to simplify your code you can create some helper methods like these:
private void updateDivide(){
int weight = Integer.valueOf(et_weight.getText().toString());
int freight = Integer.valueOf(et_freight.getText().toString());
et_rate.setText(String.valueOf(freight / weight));
}
private void updateMultiply(){
int weight = Integer.valueOf(et_weight.getText().toString());
int rate = Integer.valueOf(et_rate.getText().toString());
et_freight.setText(String.valueOf(weight * rate));
}
private boolean isSet(EditText editText){
return !editText.getText().toString().matches("");
}
And then use it in your code like this:
et_weight.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 (et_weight.isFocused() && isSet(et_weight) && isSet(et_rate)) {
updateMultiply();
}
}
#Override
public void afterTextChanged (Editable s){
}
});
et_rate.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 (et_rate.isFocused() && isSet(et_rate) && isSet(et_weight)) {
updateMultiply();
}
}
#Override
public void afterTextChanged (Editable s){
}
});
et_freight.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 (et_freight.isFocused() && isSet(et_weight) && isSet(et_freight)) {
updateDivide();
}
}
#Override
public void afterTextChanged (Editable s){
}
});

How to prevent an infinite loop with two EditTexts?

I have two TextViews EditText and have assigned each on a TextChangedListener, which reference each other, changing the text of the other textview. Specifically:
rateText.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(!rateText.getText().toString().equals("")) {
float price = Float.parseFloat(priceText.getText().toString());
float rate = Float.parseFloat(rateText.getText().toString());
// REFERNCES THE OTHER TEXT
amountText.setText(Float.toString(price / rate));
} else {
amountText.setText("");
}
}
});
amountText.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(!rateText.getText().toString().equals("")) {
float price = Float.parseFloat(priceText.getText().toString());
float amount = Float.parseFloat(amountText.getText().toString());
// REFERNCES THE OTHER TEXT
rateText.setText(Float.toString(price / amount));
} else {
rateText.setText("");
}
}
});
This results in an infinite loop, because each listener causes the other one to trigger. How can i prevent this without needing a button or something?
You could hold the TextWatchers in fields and use a static method to set the text "silently" without notifying the listeners like this:
private TextWatcher rateWatcher; //hold watchers in fields
private TextWatcher amountWatcher;
//...
void yourMethodOrCallback() { //whatever
//...
amountWatcher = 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 (!rateText.getText().toString().equals("")) {
float price = Float.parseFloat(priceText.getText().toString());
float amount = Float.parseFloat(amountText.getText().toString());
// REFERNCES THE OTHER TEXT
setTextSilently(amountText, amountWatcher, Float.toString(price / amount));
} else {
setTextSilently(rateText, rateWatcher, "");
}
}
};
rateWatcher = 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 (!rateText.getText().toString().equals("")) {
float price = Float.parseFloat(priceText.getText().toString());
float rate = Float.parseFloat(rateText.getText().toString());
// REFERNCES THE OTHER TEXT
setTextSilently(amountText, amountWatcher, Float.toString(price / rate));
} else {
setTextSilently(amountText, amountWatcher, "");
}
}
};
rateText.addTextChangedListener(rateWatcher);
amountText.addTextChangedListener(amountWatcher);
}
private static void setTextSilently(EditText editText, TextWatcher textWatcher, CharSequence text) {
editText.removeTextChangedListener(textWatcher); //removing watcher temporarily
editText.setText(text); //setting text
editText.addTextChangedListener(textWatcher); //readding watcher
}
I would agree with #Lotharyx's suggestion to try using a boolean first. The boolean variable should only have to be final if declared in the same scope as the method where you are adding these TextWatchers, otherwise you can use an instance variable (doesn't have to be static).
Another thing you could try is to remove the TextWatcher before the body of code in afterTextChanged(), and add it again after you have call setText() on the other EditText. But this would also require you to make the TextWatcher objects instance variables or declare them as final.
#Override
public void afterTextChanged(Editable s) {
rateText.removeTextChangedListener(rateTextListener);
if(!rateText.getText().toString().equals("")) {
float price = Float.parseFloat(priceText.getText().toString());
float amount = Float.parseFloat(amountText.getText().toString());
// REFERNCES THE OTHER TEXT
rateText.setText(Float.toString(price / amount));
} else {
rateText.setText("");
}
rateText.addTextChangedListener(rateTextListener);
}

How to add Text Watcher on multiple Edit text in android?

I have an activity which contain two edit text and have a TextWatcher in that activity which is implemented separately. I want to make a single class which implements TextWatcher and that that TextWatcher in that edit text. How can I do that
code:-
private TextWatcher getmWatcher = 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) {
checkFieldsForEmpty();
}
};
private TextWatcher mWatcher = 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) {
checkFieldsForEmpty();
}
};
m_InputMobile = (EditText) findViewById(R.id.input_mobile);// finding Id of Mobile Number edit text
m_InputMobile.addTextChangedListener(getmWatcher);
m_InputPassword = (EditText) findViewById(R.id.input_password);// finding Id of assword editText
m_InputPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);// defining password edit Tect Input type
There is two ways you can do this.
First
TextWatcher 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) {
}
#Override
public void afterTextChanged(Editable s) {
if (m_InputMobile.getText().hashCode() == s.hashCode()) {
checkFieldsForEmpty();
}
else if (m_InputPassword.getText().hashCode() == s.hashCode()) {
checkFieldsForEmpty();
}
}
};
m_InputMobile = (EditText) findViewById(R.id.input_mobile);
m_InputMobile.addTextChangedListener(getmWatcher);
m_InputPassword = (EditText) findViewById(R.id.input_password);
m_InputPassword.addTextChangedListener(getmWatcher);
or make a custome TextWatcher class
Second
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_InputMobile = (EditText) findViewById(R.id.input_mobile);
m_InputMobile.addTextChangedListener(new CustomTextWatcher(m_InputMobile));
m_InputPassword = (EditText) findViewById(R.id.input_password);
m_InputPassword.addTextChangedListener(new CustomTextWatcher(m_InputPassword));
}
private class CustomTextWatcher implements TextWatcher {
private EditText mEditText;
public CustomTextWatcher(EditText e) {
mEditText = e;
}
#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) {
checkFieldsForEmpty();
}
}
For more details visit this Question.
Happy Coding.
Multiple edittext Intialization & Setting TextChangeListener
for(int i=0;i<editTexts_ids.length;i++)
{
editTexts[i] = (EditText)view.findViewById(editTexts_ids[i]);
editTexts[i].addTextChangedListener(this);
textInputLayouts[i]= (TextInputLayout)view.findViewById(textInputLayouts_ids[i]);
//Log.e("EditText["+i+"]",""+editTexts[i].getId());
}
In afterTextChanged Method
#Override
public void afterTextChanged(Editable editable) {
for(int i=0;i<editTexts_ids.length;i++) // Disable Error AFter Entered Text
{//not include Mobile Number Name,S/o, Nominee Edittexts
if (editTexts[i].getText().hashCode() == editable.hashCode())// Checking
{
//Mobile Number VAlidation At Runtime
if(i==11)
{
if(Utils.Mobile_NumberValidation(editable.toString()))
{
textInputLayouts[i].setError("");
return;
}
else
{
textInputLayouts[i].setError("Invalid Mobile Number (should start with 7 or 8 or 9) , Length 10 digits");
return;
}
}
if (i == 4) { // for Name
if(!Utils.Name_Validation(editable.toString())) {
textInputLayouts[4].setError("Invalid Person Name (Special Symbols(#,!,$ .etc), DOT (.) are not allowed)");
return;
}else
{
textInputLayouts[4].setError("");
return;
}
}
if (i == 5) { //for S/o,D/o,W/o
if(!Utils.Name_Validation(editable.toString())) {
textInputLayouts[5].setError("Invalid Person Name (Special Symbols(#,!,$ .etc), DOT (.) are not allowed)");
return;
}else
{
textInputLayouts[5].setError("");
return;
}
}
if (i == 9) { //for S/o,D/o,W/o
if(!Utils.Name_Validation(editable.toString())) {
textInputLayouts[9].setError("Invalid Person Name (Special Symbols(#,!,$ .etc), DOT (.) are not allowed)");
return;
}else
{
textInputLayouts[9].setError("");
return;
}
}
if (!editable.toString().trim().isEmpty()) {
textInputLayouts[i].setError("");
return;
}
}
}
}
Simply create a TextWatcher object and pass it into the addTextChangeListener like this :
TextWatcher 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) {
}
#Override
public void afterTextChanged(Editable s) {
checkFieldsForEmpty();
}
};
m_InputMobile.addTextChangedListener(textWatcher);

Categories

Resources