Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic mask using TextWatcher?

I'm trying create mask using EditText with TextWatcher. This mask need format a phone number but the problem is I have two situations to this mask. Situation 1 the mask need has 13 digit (99)9999-9999 and situation 2 need has 14 digit (99)99999-9999. I want when I type numbers are formatted.

How could I do it ?

I'm trying this.

//Activity
private EditText etPhone;
etPhone = (EditText)findViewById(R.id.etPhone());
etPhone.addTextChangedListener(new TelefoneMask(etPhone));

//TextWatcher
public class TelefoneMask implements TextWatcher {
    private String current;
    private EditText phone;
    private StringBuilder mask = new StringBuilder();

    public TelefoneMask(EditText phone) {
        this.phone = phone;
    }

    @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 synchronized void afterTextChanged(Editable s) {
        // mask1 -> (99)9999-9999   13 digits
        // mask2 -> (99)99999-9999  14 digits

        Integer size = s.length();
        if(size == 10){
            mask.append(s.toString());
            mask.insert(0, "(");
            mask.insert(3, ")");
            mask.insert(8, "-");
        }else if(size > 10){
            mask.setLength(0);
            mask.append(s.toString());
            mask.insert(0, "(");
            mask.insert(3, ")");
            mask.insert(9, "-");
        }
        current = mask.toString();
        phone.setText(current);
        phone.setSelection(mask.length());
    }
}
like image 985
FernandoPaiva Avatar asked Apr 15 '15 20:04

FernandoPaiva


1 Answers

Solved the problem

I did

public abstract class EditTextTelefoneMask {
    private static final String mask8 = "####-####";
    private static final String mask9 = "#####-####";
    private static final String mask10 = "(##) ####-####";
    private static final String mask11 = "(##) #####-####";

    public static String unmask(String s) {
        return s.replaceAll("[^0-9]*", "");
    }

    public static TextWatcher insert(final EditText editText) {
        return new TextWatcher() {
            boolean isUpdating;
            String old = "";

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str = EditTextTelefoneMask.unmask(s.toString());
                String mask;
                String defaultMask = getDefaultMask(str);
                switch (str.length()) {
                    case 11:
                        mask = mask11;
                        break;
                    case 10:
                        mask = mask10;
                        break;
                    case 9:
                        mask = mask9;
                        break;
                    default:
                        mask = defaultMask;
                        break;
                }

                String mascara = "";
                if (isUpdating) {
                    old = str;
                    isUpdating = false;
                    return;
                }
                int i = 0;
                for (char m : mask.toCharArray()) {
                    if ((m != '#' && str.length() > old.length()) || (m != '#' && str.length() < old.length() && str.length() != i)) {
                        mascara += m;
                        continue;
                    }

                    try {
                        mascara += str.charAt(i);
                    } catch (Exception e) {
                        break;
                    }
                    i++;
                }
                isUpdating = true;
                editText.setText(mascara);
                editText.setSelection(mascara.length());
            }

            public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
            public void afterTextChanged(Editable s) {}
        };
    }

    private static String getDefaultMask(String str) {
        String defaultMask = mask8;
        if (str.length() > 11){
            defaultMask = mask11;
        }
        return defaultMask;
    }

}

Activity

etPhone.addTextChangedListener(EditTextTelefoneMask.insert(etPhone));
like image 98
FernandoPaiva Avatar answered Nov 12 '22 04:11

FernandoPaiva