I am trying to implement an app that only accepts hexadecimal numbers and every 4 hexadecimal numbers must insert a space, I have managed to do this separately using TextWatcher separately, but I can not do both functions in a single TextWatcher, please someone could tell me how to do it.
Here are the two TextWatchers:
This TextWatcher only accepts hexadecimal numbers as an edittext entry
public void onTextChanged(CharSequence s, int start, int before, int count) {
String str = s.toString();
if (str.isEmpty()) {
mTextoEditor2.append(newStr);
newStr = "";
} else if (!str.equals(newStr)) {
newStr = str.replaceAll("[^A-F0-9a-f]", "");
mTextoEditor2.setText("");
}
}
This other inserts a space every four characters entered by edittext
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count == 4)
{
mTextoEditor2.setText(mTextoEditor2.getText()+" ");
mTextoEditor2.setSelection(mTextoEditor2.getText().length());
}
}
I need to join both in one TextWatcher, could someone tell me how to do?