Рет қаралды 13,188
Hi and welcome to another tutorial from CodingDemos :)
In this tutorial, you will learn how to add contacts programmatically in Android.
Here are the steps:
1- Open up Android Studio.
2- Add some checking Inside the addContact button's onClickListener to ensure that the name, email, and phone edittexts are not empty. Otherwise, show an error Toast message.
if (!name.getText().toString().isEmpty() && !email.getText().toString().isEmpty() && !phone
.getText().toString().isEmpty()) {
}else{
Toast.makeText(MainActivity.this, "Please fill all the fields",
Toast.LENGTH_SHORT).show();
}
3- Initialize the Android contact Intent.
Intent intent = new Intent(Intent.ACTION_INSERT);
4- Set the type of intent so that only certain apps that support this action can be used.
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
5- Pass the name, email, and phone values inside the intent using PutExtra.
intent.putExtra(ContactsContract.Intents.Insert.NAME, name.getText().toString());
intent.putExtra(ContactsContract.Intents.Insert.EMAIL, email.getText().toString());
intent.putExtra(ContactsContract.Intents.Insert.PHONE, phone.getText().toString());
6- Before you can call startActivity(), you need to do some checking to ensure that there is an application that can handle this type of action. Otherwise, show an error Toast Message.
if(intent.resolveActivity(getPackageManager()) != null){
startActivity(intent);
}else{
Toast.makeText(MainActivity.this, "There is no app that support this action", Toast.LENGTH_SHORT).show();
}
7- Now build and run the app to see the result :)
Links:
Modify contacts using intents: developer.android.com/trainin...
Website: www.codingdemos.com
FaceBook: / codingdemos
Introduction 00:00
Final output 00:15
Android Studio project introduction 03:54
Working with Android Contact Intent 05:10
Conclusion 17:50