Get registered email accounts from the your device or android phone.
This features is widely use in any mobile devices when we write email or login any application.
In this blog we implement the this features very easy way and make your application one step to a head to others
Advantage Of Get Registered Email Accounts featured,
- User Friendly : Whenever user needs to write email or fill any form that time not required to write email, it’s automatically fetch and display on the screen.
- Time saving : User not required to write any kind of the email, that while speedly peform any kind of form filling
- Less spelling mistake : Using get registered email account features, we not need to write email account, if email is registered in your account, that’s while less spelling mistake generate.
Download full source code,
Basically needs four step to performed get registered email accounts,
1) Add Account permission in AndroidManifest.xml file
add android.permission.GET_ACCOUNTS permission in our AndroidManifest.xml file, after adding the permission our file look like this,
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="in.co.lifs.accountlist"> <uses-permission android:name="android.permission.GET_ACCOUNTS"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
2) import Account and Account Manager library
Now, add Account and AccountManager import in your activity.
import android.accounts.Account; import android.accounts.AccountManager;
3) Write Account fetching code
this is main section of the get existing account list from the mobile, given code get the email accounts and it’s type.
AccountManager manager = (AccountManager)mContext.getSystemService(ACCOUNT_SERVICE); Account[] accountdatalist = manager.getAccounts();
4) Full code
Here, after the implementation of the all things, how to look like our code, see here,
activity_main.xml
In this file contain, one edittext.
When we click on edittext dialog open with email account list. We select any email and dialog box will close.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <EditText android:id="@+id/etEmail" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/hint_email" android:inputType="textWebEmailAddress" android:padding="18dp" android:textColor="@android:color/black" android:textSize="18sp" /> </LinearLayout>
dialog_account_list.xml
this, layout file is used to display the email and it’s type data.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorAccent" android:padding="18dp"> <TextView android:id="@+id/tvLoginDialogTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Login with " android:textColor="#FFFFFF" android:textSize="20sp" /> </RelativeLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rvAccountRecycleCiew" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/tvNoneOftheAbove" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="4dp" android:gravity="center_vertical" android:padding="8dp" android:text="None of the above" android:textColor="#000000" android:textSize="18sp" /> </LinearLayout>
Create, row_account.xml
this layout used to display each email row vice in recyclerview.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/lnrowAccount" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="8dp"> <TextView android:id="@+id/tvAccountEmail" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="abcdefg@xyz.com" android:textColor="#000000" android:textSize="18sp" /> <TextView android:id="@+id/tvAccountType" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="com.google" android:textColor="@color/text_colorpost" android:textSize="14sp" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginTop="2dp" android:background="@color/text_colorpost" /> </LinearLayout>
Create one model class and give the name is AccountSimpleModel.java
AccountSimpleModel.java contains setter and getter method of the email and it’s type.
package in.co.lifs.accountlist; public class AccountSimpleModel { private String accountEmail = "", accountType = ""; public String getAccountEmail() { return accountEmail; } public void setAccountEmail(String accountEmail) { this.accountEmail = accountEmail; } public String getAccountType() { return accountType; } public void setAccountType(String accountType) { this.accountType = accountType; } }
Next, need to one adapter class that set the data of the each row of the recyclerview.
so create one more java file name is AccountListAdapter.java
package in.co.lifs.accountlist; import android.content.Context; import android.graphics.Typeface; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; /** * Created by Tarbundiya Hitesh on 16-10-2016. */ public class AccountListAdapter extends RecyclerView.Adapter<AccountListAdapter.ViewHolder> { private String TAG = "AccountListAdapter"; private Context mContext; private LayoutInflater infalter; private Typeface mTypefaceBold, mTypefaceSemiBold, mTypefaceRegular, mTypefaceLight; private ArrayList<AccountSimpleModel> mAccountSimpleModelArrayList = new ArrayList<AccountSimpleModel>(); private View.OnClickListener mAccountListClickListener; public AccountListAdapter(Context context, View.OnClickListener accountlistClickListener, ArrayList<AccountSimpleModel> groupListCustomModelArrayList) { this.infalter = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.mContext = context; this.mAccountSimpleModelArrayList = groupListCustomModelArrayList; this.mAccountListClickListener = accountlistClickListener; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(mContext).inflate(R.layout.row_account, parent, false); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(AccountListAdapter.ViewHolder holder, int position) { holder.tvAccountEmail.setText(mAccountSimpleModelArrayList.get(position).getAccountEmail()); holder.tvAccountType.setText(mAccountSimpleModelArrayList.get(position).getAccountType()); holder.lnrowAccount.setTag(position); holder.lnrowAccount.setOnClickListener(mAccountListClickListener); } @Override public int getItemCount() { return mAccountSimpleModelArrayList.size(); } public class ViewHolder extends RecyclerView.ViewHolder { private TextView tvAccountEmail, tvAccountType; private LinearLayout lnrowAccount; public ViewHolder(View itemView) { super(itemView); tvAccountEmail = (TextView) itemView.findViewById(R.id.tvAccountEmail); tvAccountType = (TextView) itemView.findViewById(R.id.tvAccountType); lnrowAccount = (LinearLayout) itemView.findViewById(R.id.lnrowAccount); tvAccountEmail.setTypeface(mTypefaceRegular); tvAccountType.setTypeface(mTypefaceRegular); } } }
at the end need to inmplement our main source code in activity or fragment class.
like click of the our view and what happen when we click on that, all of the main source code available here,
create MainActivity.java
package in.co.lifs.accountlist; import android.accounts.Account; import android.accounts.AccountManager; import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.Window; import android.widget.EditText; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.DefaultItemAnimator; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private EditText etEmail; /*Dialog Accounts initialization*/ private Dialog mAccountListDialog; private RecyclerView rvAccountRecycleCiew; private TextView tvLoginDialogTitle, tvNoneOftheAbove; private AccountListAdapter mAccountListAdapter; private ArrayList<AccountSimpleModel> tempAccountSimpleModelArrayList = new ArrayList<AccountSimpleModel>(); /*End Dialog Accounts initialization*/ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etEmail = (EditText)findViewById(R.id.etEmail); dialog_AccountList_Initialization(); etEmail.setOnClickListener(this); } private void dialog_AccountList_Initialization() { mAccountListDialog = new Dialog(MainActivity.this); mAccountListDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); mAccountListDialog.setCancelable(true); mAccountListDialog.setContentView(R.layout.dialog_account_list); tvLoginDialogTitle = (TextView) mAccountListDialog.findViewById(R.id.tvLoginDialogTitle); tvNoneOftheAbove = (TextView) mAccountListDialog.findViewById(R.id.tvNoneOftheAbove); rvAccountRecycleCiew = (RecyclerView)mAccountListDialog.findViewById(R.id.rvAccountRecycleCiew); LinearLayoutManager mLinearLayout = new LinearLayoutManager(MainActivity.this, RecyclerView.VERTICAL, false); rvAccountRecycleCiew.setLayoutManager(mLinearLayout); rvAccountRecycleCiew.setItemAnimator(new DefaultItemAnimator()); tvNoneOftheAbove.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.etEmail: tempAccountSimpleModelArrayList.clear(); tempAccountSimpleModelArrayList = getMobileAccounts(MainActivity.this); if(tempAccountSimpleModelArrayList.size()>0){ /*for (int i = 0; i < tempAccountSimpleModelArrayList.size(); i++) { Logger.e(TAG,"Account Type : "+tempAccountSimpleModelArrayList.get(i).getAccountType()); Logger.e(TAG,"Account email : "+tempAccountSimpleModelArrayList.get(i).getAccountEmail()); }*/ mAccountListAdapter = new AccountListAdapter(MainActivity.this, mAccountClickListener, tempAccountSimpleModelArrayList); rvAccountRecycleCiew.setAdapter(mAccountListAdapter); mAccountListAdapter.notifyDataSetChanged(); mAccountListDialog.show(); } break; case R.id.tvNoneOftheAbove: mAccountListDialog.dismiss(); break; } } private View.OnClickListener mAccountClickListener = new View.OnClickListener() { @Override public void onClick(View v) { int position = (int) v.getTag(); etEmail.setText(tempAccountSimpleModelArrayList.get(position).getAccountEmail()); etEmail.setSelection(etEmail.getText().toString().trim().length()); mAccountListDialog.dismiss(); } }; /*Get All accounts available in application*/ private ArrayList<AccountSimpleModel> getMobileAccounts(Context mContext){ AccountManager manager = (AccountManager)mContext.getSystemService(ACCOUNT_SERVICE); Account[] accountdatalist = manager.getAccounts(); ArrayList<AccountSimpleModel> tempAccountSimpleModelArrayList = new ArrayList<AccountSimpleModel>(); for (int i = 0; i <accountdatalist.length ; i++) { Account tempAccount = accountdatalist[i]; if(isEmailValid(tempAccount.name)){ AccountSimpleModel tempAccountSimpleModel = new AccountSimpleModel(); tempAccountSimpleModel.setAccountEmail(tempAccount.name); tempAccountSimpleModel.setAccountType(tempAccount.type); tempAccountSimpleModelArrayList.add(tempAccountSimpleModel); } } return tempAccountSimpleModelArrayList; } /*End Get All accounts available in application*/ private boolean isEmailValid(String email) { String expression = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; CharSequence inputStr = email; boolean flag = false; Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(inputStr); if (matcher.matches()) { flag = true; } return flag; } }
Above code getMobileAccounts() function is used to get all accounts that are available in mobile with full fill the email rules.
and fetch that all email list in and assign in to particular arraylist.
while isEmailValid() function is used to verified the given email is valid email or not.