Here's another solution for checking the IP address, but which can be used as a link also for checking the web url.
EditText ipText = (EditText) findViewById(R.id.ip_address); ipText.setKeyListener(IPAddressKeyListener.getInstance());
.........
public class IPAddressKeyListener extends NumberKeyListener { private char[] mAccepted; private static IPAddressKeyListener sInstance; @Override protected char[] getAcceptedChars() { return mAccepted; } private static final char[] CHARACTERS = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.' }; private IPAddressKeyListener() { mAccepted = CHARACTERS; } public static IPAddressKeyListener getInstance() { if (sInstance != null) return sInstance; sInstance = new IPAddressKeyListener(); return sInstance; } public int getInputType() { return InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL; } @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { if (end > start) { String destTxt = dest.toString(); String resultingTxt = destTxt.substring(0, dstart) + source.subSequence(start, end) + destTxt.substring(dend); if (!resultingTxt.matches("^\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3})?)?)?)?)?)?")) { return ""; } else { String[] splits = resultingTxt.split("\\."); for (int i = 0; i < splits.length; i++) { if (Integer.valueOf(splits[i]) > 255) { return ""; } } } } return null; }
}
Mario source share