Add contact suggesion in compose mail in andriod studio

To add a contact suggestion feature in the compose mail functionality in Android Studio, you'll need to implement the AutocompleteTextView widget and use the ContactsContract API to retrieve contact suggestions. Here's a step-by-step guide to help you achieve this:

Step 1: Add the AutocompleteTextView widget

In your activity_main.xml file, add the AutocompleteTextView widget:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/recipient_edittext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:hint="Recipient">

    <com.google.android.material.textfield.AutocompleteTextView
        android:id="@+id/recipient_autocomplete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:completionThreshold="1" />

</com.google.android.material.textfield.TextInputLayout>

Step 2: Create a custom adapter for contact suggestions

Create a new Java class, e.g., ContactSuggestionAdapter.java, that extends ArrayAdapter<Contact>. This adapter will be responsible for populating the AutocompleteTextView with contact suggestions:

public class ContactSuggestionAdapter extends ArrayAdapter<Contact> {
    private Context context;
    private List<Contact> contacts;

    public ContactSuggestionAdapter(Context context, int resource, List<Contact> contacts) {
        super(context, resource, contacts);
        this.context = context;
        this.contacts = contacts;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.contact_suggestion_item, parent, false);
        }

        Contact contact = getItem(position);
        TextView nameTextView = view.findViewById(R.id.name_textview);
        nameTextView.setText(contact.getName());

        return view;
    }
}

Step 3: Retrieve contact suggestions using the ContactsContract API

Create a new Java class, e.g., ContactSuggestionProvider.java, that will retrieve contact suggestions using the ContactsContract API:

public class ContactSuggestionProvider {
    private Context context;

    public ContactSuggestionProvider(Context context) {
        this.context = context;
    }

    public List<Contact> getContactSuggestions(String query) {
        List<Contact> contacts = new ArrayList<>();

        // Retrieve contacts from the ContactsContract
        Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        if (cursor!= null && cursor.moveToFirst()) {
            do {
                String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                // Filter contacts based on the query
                if (name.toLowerCase().contains(query.toLowerCase())) {
                    Contact contact = new Contact(contactId, name);
                    contacts.add(contact);
                }
            } while (cursor.moveToNext());
        }

        cursor.close();

        return contacts;
    }
}

Step 4: Set up the AutocompleteTextView with the custom adapter and contact suggestion provider

In your MainActivity.java file, set up the AutocompleteTextView with the custom adapter and contact suggestion provider:

public class MainActivity extends AppCompatActivity {
    private AutocompleteTextView recipientAutocomplete;
    private ContactSuggestionAdapter contactSuggestionAdapter;
    private ContactSuggestionProvider contactSuggestionProvider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recipientAutocomplete = findViewById(R.id.recipient_autocomplete);
        contactSuggestionAdapter = new ContactSuggestionAdapter(this, R.layout.contact_suggestion_item, new ArrayList<Contact>());
        recipientAutocomplete.setAdapter(contactSuggestionAdapter);

        contactSuggestionProvider = new ContactSuggestionProvider(this);

        recipientAutocomplete.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}

            @Override
            public void afterTextChanged(Editable s) {
                String query = s.toString();
                List<Contact> contacts = contactSuggestionProvider.getContactSuggestions(query);
                contactSuggestionAdapter.clear();
                contactSuggestionAdapter.addAll(contacts);
                contactSuggestionAdapter.notifyDataSetChanged();
            }
        });
    }
}

Step 5: Define the Contact class

Create a new Java class, e.g., Contact.java, that represents a contact:

public class Contact {
    private String id;
    private String name;

    public Contact(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}

That's it! With these steps, you should now have a AutocompleteTextView that suggests contacts as you type in the recipient's email address.