Created
April 15, 2022 08:46
-
-
Save massimilianochiodi/2344c3c74a51c99eb297da0af142f238 to your computer and use it in GitHub Desktop.
Format mac address on EditText ( java )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
txtmacmacaddress.addTextChangedListener(new TextWatcher() { | |
String mPreviousMac = null; | |
@Override | |
public void afterTextChanged(Editable arg0) { | |
} | |
@Override | |
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { | |
} | |
@SuppressLint("DefaultLocale") | |
@Override | |
public void onTextChanged(CharSequence s, int start, int before, int count) { | |
String enteredMac = txtmacmacaddress.getText().toString().toUpperCase(); | |
String cleanMac = clearNonMacCharacters(enteredMac); | |
String formattedMac = formatMacAddress(cleanMac); | |
int selectionStart = txtmacmacaddress.getSelectionStart(); | |
formattedMac = handleColonDeletion(enteredMac, formattedMac, selectionStart); | |
int lengthDiff = formattedMac.length() - enteredMac.length(); | |
setMacEdit(cleanMac, formattedMac, selectionStart, lengthDiff); | |
} | |
/** | |
* Strips all characters from a string except A-F and 0-9. | |
* @param mac User input string. | |
* @return String containing MAC-allowed characters. | |
*/ | |
private String clearNonMacCharacters(String mac) { | |
return mac.replaceAll("[^A-Fa-f0-9]", ""); | |
} | |
/** | |
* Adds a colon character to an unformatted MAC address after | |
* every second character (strips full MAC trailing colon) | |
* @param cleanMac Unformatted MAC address. | |
* @return Properly formatted MAC address. | |
*/ | |
private String formatMacAddress(String cleanMac) { | |
int grouppedCharacters = 0; | |
StringBuilder formattedMac = new StringBuilder(); | |
for (int i = 0; i < cleanMac.length(); ++i) { | |
formattedMac.append(cleanMac.charAt(i)); | |
++grouppedCharacters; | |
if (grouppedCharacters == 2) { | |
formattedMac.append(":"); | |
grouppedCharacters = 0; | |
} | |
} | |
// Removes trailing colon for complete MAC address | |
if (cleanMac.length() == 12) | |
formattedMac = new StringBuilder(formattedMac.substring(0, formattedMac.length() - 1)); | |
return formattedMac.toString(); | |
} | |
/** | |
* Upon users colon deletion, deletes MAC character preceding deleted colon as well. | |
* @param enteredMac User input MAC. | |
* @param formattedMac Formatted MAC address. | |
* @param selectionStart MAC EditText field cursor position. | |
* @return Formatted MAC address. | |
*/ | |
private String handleColonDeletion(String enteredMac, String formattedMac, int selectionStart) { | |
if (mPreviousMac != null && mPreviousMac.length() > 1) { | |
int previousColonCount = colonCount(mPreviousMac); | |
int currentColonCount = colonCount(enteredMac); | |
if (currentColonCount < previousColonCount) { | |
formattedMac = formattedMac.substring(0, selectionStart - 1) + formattedMac.substring(selectionStart); | |
String cleanMac = clearNonMacCharacters(formattedMac); | |
formattedMac = formatMacAddress(cleanMac); | |
} | |
} | |
return formattedMac; | |
} | |
/** | |
* Gets MAC address current colon count. | |
* @param formattedMac Formatted MAC address. | |
* @return Current number of colons in MAC address. | |
*/ | |
private int colonCount(String formattedMac) { | |
return formattedMac.replaceAll("[^:]", "").length(); | |
} | |
/** | |
* Removes TextChange listener, sets MAC EditText field value, | |
* sets new cursor position and re-initiates the listener. | |
* @param cleanMac Clean MAC address. | |
* @param formattedMac Formatted MAC address. | |
* @param selectionStart MAC EditText field cursor position. | |
* @param lengthDiff Formatted/Entered MAC number of characters difference. | |
*/ | |
private void setMacEdit(String cleanMac, String formattedMac, int selectionStart, int lengthDiff) { | |
txtmacmacaddress.removeTextChangedListener(this); | |
if (cleanMac.length() <= 12) { | |
txtmacmacaddress.setText(formattedMac); | |
txtmacmacaddress.setSelection(selectionStart + lengthDiff); | |
mPreviousMac = formattedMac; | |
macmask = formattedMac; | |
} else { | |
txtmacmacaddress.setText(mPreviousMac); | |
txtmacmacaddress.setSelection(mPreviousMac.length()); | |
macmask = mPreviousMac; | |
} | |
txtmacmacaddress.addTextChangedListener(this); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment