androidandroid-edittextgoogle-accountaccountpicker

How to get the device primary Gmail id to edit text field


I want to automatically add device primary gmail id in the edit text field whenever user open the login page.

this is my emailview

mEmailView = (EditText)findViewById(R.id.account_email);

I have mentioned the permission in the Main fest

<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>

any suggestion ?


Solution

  • Answer for my question simple and easy way to put device email into edittext

        mEmailView = (EditText)findViewById(R.id.email);
        Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
        android.accounts.Account[] accounts = AccountManager.get(getBaseContext()).getAccounts();
        for (android.accounts.Account account : accounts) {
        if (emailPattern.matcher(account.name).matches()) {
        possibleEmail = account.name;
        }
        }
    
    
        TextView t1 = (TextView)findViewById(R.id.account_email);
        t1.setText(possibleEmail);