-1

The following code snippet is used for a text Watcher...it doesnt work...everything works fine,untill the last line of code...

private TextWatcher mobileTextWatcher = 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) {
        String number = s.toString();
        number = AppConstants.convertToOnlyDigits(number);
        Editable temp = new SpannableStringBuilder(
                AppConstants.formatPhoneNumber(number));
        s = temp;
    }
};

After that the new doesn't get assigned to s.. Or in otherwords,I cannot see a change in the text in the Edit Text.

Sahil Mahajan Mj
  • 11,033
  • 8
  • 53
  • 100
Sreekanth Karumanaghat
  • 3,383
  • 6
  • 44
  • 72

4 Answers4

0

As s is function argument you cannot reassign it and save new value beyond the scope of function.

If I understood your goal right - you should assign temp value to EditText by setText() method

Viacheslav
  • 5,443
  • 1
  • 29
  • 36
0

The parameter are not passed as reference, so s = temp; will not change the text in edittext.

Try like this

private TextWatcher mobileTextWatcher       =   new TextWatcher() {
        boolean inWatcher = false;
        @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) {
        if(inWatcher) { inWatcher = false; return; }
        String number   =   s.toString();
        number          =   AppConstants.convertToOnlyDigits(number);
        Editable temp   =   new SpannableStringBuilder(AppConstants.formatPhoneNumber(number));
        inWatcher = true;
        your_edit_text.setText(temp);
    }
};
Vivek Khandelwal
  • 7,829
  • 3
  • 25
  • 40
0

You gotta actually set your EditText's text again with the setText() method.

Before you do this, you might want to add a simple boolean check as 'mFormatting', to make sure you do not get into an infinite loop of the textWatcher calling itself, after making changes.

Stefan de Bruijn
  • 6,289
  • 2
  • 23
  • 31
0

You are not changing the object in s, you are instead replacing the variable s with another object.

Try something like this:

boolean recurseProtection = false;
@Override
public void afterTextChanged(Editable s) { 
    if(!recurseProtection) {
        String number = s.toString();
        number = AppConstants.convertToOnlyDigits(number);
        recurseProtection = true;
        s.clear();
        s.append(AppConstants.formatPhoneNumber(number));
        recurseProtection = false;
    }
}

Note I've added some protection to stop afterTextChanged being called when you change s object (which I assume has a DataObserver placed on it). Recursion protection is off the top of my head so might need tweaking.

Hope this helps.

Graeme
  • 25,714
  • 24
  • 124
  • 186