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 [email protected] then it should be auto completed as i start typing "s". Any Ideas??? Thanks...
Users can enable or disable autofill as well as change the autofill service by navigating to Settings > System > Languages & input > Advanced > Input assistance > Autofill service.
Autofill is a dedicated framework introduced by Google that manages communication between the autofill service and apps on your Android device. The service works much like password managers, which take the stress out of forgetting passwords and fills out information in other apps using your data.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With