Last active
December 12, 2015 10:49
-
-
Save jdamcd/4761834 to your computer and use it in GitHub Desktop.
Email adapter for AutoCompleteTextView that looks for emails from all account types, not just Google.
This file contains hidden or 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
private static final String EMAIL_REGEX = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; | |
private static final Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REGEX); | |
private ArrayAdapter<String> getEmailAddressAdapter(Context context) { | |
Account[] accounts = AccountManager.get(context).getAccounts(); | |
HashSet<String> emailSet = new HashSet<String>(); | |
for (int i = 0; i < accounts.length; i++) { | |
if (isEmailAddress(accounts[i].name)) { | |
emailSet.add(accounts[i].name); | |
} | |
} | |
String[] emailArray = emailSet.toArray(new String[emailSet.size()]); | |
return new ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line, emailArray); | |
} | |
private boolean isEmailAddress(String possibleEmail) { | |
return EMAIL_PATTERN.matcher(possibleEmail).matches(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@orrc Neat. Had no idea that class existed.