I want to auto-complete an emailId EditText with the gmail-id registered with the device. e.g if my phone is registered with the gmail id sd@gmail.com then it should be auto completed as i start typing "s". Any Ideas??? Thanks...
Asked
Active
Viewed 7,215 times
7
-
http://stackoverflow.com/questions/2112965/how-to-get-the-android-devices-primary-e-mail-address – ingsaurabh Mar 16 '12 at 07:20
-
in your app it has to auto complete your registered email ?? – Yugandhar Babu Mar 16 '12 at 07:20
-
Just an idea. once you get the id, instead of auto-fill why don't you set it as a hint? U can use setHint() – Shubhayu Mar 16 '12 at 07:34
-
yes @shubhayu thats good one. but you cannot select hint. so when you click on hint it disappears. So i made an ArrayAdapter out of these results and took AutoCompleteTextView instead of EditText. Now works as expected. – Shashank Degloorkar Mar 17 '12 at 05:54
-
See this: http://stackoverflow.com/a/39998008/1307387 – Milad Ghiravani Oct 12 '16 at 11:57
2 Answers
5
You have to go through Android AccountManager class:
AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
Account[] list = manager.getAccounts();
and also need to add required permissions to AndroidManifest file:
<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>
From there you can autofill info.
Vadim Kotov
- 8,084
- 8
- 48
- 62
Mayank
- 8,777
- 4
- 35
- 60
-
1@Dallas Substantially editing the content of someone else's answer is not appropriate. You may post your content as a new separate answer. Rolling this answer back, do not repeat it. – nobody May 27 '14 at 15:12
-
@AndrewMedico you should probably roll back to the version prior to the major edit, then, as that one had spelling/grammar/syntax fixes. – Dallas May 27 '14 at 19:30
4
Firstly set this permission in your AndroidManifest.xml file
<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>
The Java code:
//declaration
String possibleEmail="";
//onCreate
EditText emailEdt=new EditText(this);
Account[] accounts = AccountManager.get(this).getAccounts();
for (Account account : accounts)
{
// TODO: Check possibleEmail against an email regex or treat
// account.name as an email address only for certain account.type values.
possibleEmail = account.name;
}
emailEdt.setText(possibleEmail);
Ant4res
- 1,217
- 1
- 18
- 36
Pradeep Sodhi
- 2,135
- 1
- 19
- 19