Skip to content

Instantly share code, notes, and snippets.

@Keshava11
Created February 6, 2018 11:02
Show Gist options
  • Select an option

  • Save Keshava11/ecdee57d773d10bd4dab2fe8eb856fec to your computer and use it in GitHub Desktop.

Select an option

Save Keshava11/ecdee57d773d10bd4dab2fe8eb856fec to your computer and use it in GitHub Desktop.
Simplest input filter (non-editable country code prefix) for a Phone Number EditText.
/**
* InputFilter for Myanmar Phone Numbers
* TODO Update it for all country codes
*/
class CountryCodeInputFilter implements InputFilter {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
//Back Key Press conditions
boolean deleteCond0 = dstart == 0 && dend == 1; // +
boolean deleteCond1 = dstart == 1 && dend == 2; // 9
boolean deleteCond2 = dstart == 2 && dend == 3; // 5
boolean insertCond0 = dstart == 0 && dend == 0; // ahead of +
boolean insertCond1 = dstart == 1 && dend == 1; // ahead of 9
boolean insertCond2 = dstart == 2 && dend == 2; // ahead of 5
// Handling Back Key Presses
if ((deleteCond0 || deleteCond1 || deleteCond2) && (source.equals(""))) {
// BackPress
if (deleteCond0) {
return "+";
} else if (deleteCond1) {
return "9";
} else {
return "5";
}
}
// Handling insertions at invalid positions
if ((insertCond0 || insertCond1 || insertCond2) && (!source.equals(""))) {
return "";
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment