How to add new contacts in Android App?

AndroidApps/ApplicationsMobile Development

This example demonstrate about How to add new contacts in Android App.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml

<? xml version= "1.0" encoding= "utf-8" ?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
   xmlns: tools = "http://schemas.android.com/tools"
   android :layout_width= "match_parent"
   android :layout_height= "match_parent"
   android :layout_margin= "16dp"
   android :orientation= "vertical"
   tools :context= ".MainActivity" >
   <EditText
      android :id= "@+id/etContactName"
      android :layout_width= "match_parent"
      android :layout_height= "wrap_content"
      android :hint= "Contact name"
      android :inputType= "text" />
   <EditText
      android :id= "@+id/etContactNumber"
      android :layout_width= "match_parent"
      android :layout_height= "wrap_content"
      android :hint= "Contact number"
      android :inputType= "number" />
   <Button
      android :layout_width= "match_parent"
      android :layout_height= "wrap_content"
      android :onClick= "addContact"
      android :text= "ADD" />
</LinearLayout>

Step 3 − Add the following code to src/MainActivity

package app.tutorialspoint.com.sample ;
import android.app.Activity ;
import android.content.Intent ;
import android.os.Bundle ;
import android.provider.ContactsContract ;
import android.support.v7.app.AppCompatActivity ;
import android.view.View ;
import android.widget.EditText ;
import android.widget.Toast ;
public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate (Bundle savedInstanceState) {
      super .onCreate(savedInstanceState) ;
      setContentView(R.layout. activity_main ) ;
   }
   public void addContact (View view) {
      EditText etContactName = findViewById(R.id. etContactName ) ;
      EditText etContactNumber = findViewById(R.id. etContactNumber ) ;
      String name = etContactName.getText().toString() ;
      String phone = etContactNumber.getText().toString() ;
      Intent contactIntent = new Intent(ContactsContract.Intents.Insert. ACTION ) ;
      contactIntent.setType(ContactsContract.RawContacts. CONTENT_TYPE ) ;
      contactIntent
      .putExtra(ContactsContract.Intents.Insert. NAME , name)
      .putExtra(ContactsContract.Intents.Insert. PHONE , phone) ;
      startActivityForResult(contactIntent , 1 ) ;
   }
   @Override
   protected void onActivityResult ( int requestCode , int resultCode , Intent intent) {
      super .onActivityResult(requestCode , resultCode , intent) ;
      if (requestCode == 1 ) {
         if (resultCode == Activity. RESULT_OK ) {
            Toast. makeText ( this, "Added Contact" , Toast. LENGTH_SHORT ).show() ;
         }
         if (resultCode == Activity. RESULT_CANCELED ) {
            Toast. makeText ( this, "Cancelled Added Contact" ,
            Toast. LENGTH_SHORT ).show() ;
         }
      }
   }
}

Step 6 − Add the following code to androidManifest.xml

<? xml version= "1.0" encoding= "utf-8" ?>
<manifest xmlns: android = "http://schemas.android.com/apk/res/android"
   package= "app.tutorialspoint.com.sample" >
   <uses-permission android :name= "android.permission.CALL_PHONE" />
   <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>
      <receiver
         android :name= ".DeviceAdmin"
         android :description= "@string/app_description"
         android :label= "@string/app_name"
         android :permission= "android.permission.BIND_DEVICE_ADMIN" >
         <meta-data
            android :name= "android.app.device_admin"
            android :resource= "@xml/policies" />
         <intent-filter>
            <action android :name= "android.app.action.DEVICE_ADMIN_ENABLED" />
         </intent-filter>
      </receiver>
   </application>
</manifest>

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen –

raja
Published on 07-Jun-2019 08:04:19
Advertisements