Configure android EditText to allow decimals,Birthdates, Other Formates

October 02, 2017 , 0 Comments

Hello friends,

Today we are helping to make masking in edittext.Let's Start..


import android.text.Editable;
import android.text.TextWatcher;

public class MaskWatcher implements TextWatcher {
private boolean isRunning = false;
private boolean isDeleting = false;
private final String mask;

public MaskWatcher(String mask) {
this.mask = mask;
}

public static MaskWatcher buildCpf() {
return new MaskWatcher("###.###.###-##");
}

@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
isDeleting = count > after;
}

@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
}

@Override
public void afterTextChanged(Editable editable) {
if (isRunning || isDeleting) {
return;
}
isRunning = true;

int editableLength = editable.length();
if (editableLength < mask.length()) {
if (mask.charAt(editableLength) != '#') {
editable.append(mask.charAt(editableLength));
} else if (mask.charAt(editableLength-1) != '#') {
editable.insert(editableLength-1, mask, editableLength-1, editableLength);
}
}

isRunning = false;
}
}



How to Used :

YourEdittext.addTextChangedListener(new MaskWatcher("##.##")); // decimal
YourEdittext.addTextChangedListener(new MaskWatcher("##.##.####")); //birthday

Contributer

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google

0 comments:

Hot-product

Best Watch Store

Popular Posts

Contact Form

Name

Email *

Message *