Skip to main content

How to save contacts to the phone contacts in android


We can easily save the contacts to the phone contacts without having any database of own . The code is very simple.



import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.provider.Contacts.People;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
 protected static final int PICK_CONTACT = 0;
 EditText edt1,edt2;
 Button btn;
 String s1,s2;

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

  edt1=(EditText)findViewById(R.id.editText1);
  edt2=(EditText)findViewById(R.id.editText2);
  btn=(Button)findViewById(R.id.button1);
  
  btn.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    s1=edt1.getText().toString();
    s2=edt2.getText().toString();
     ContentValues values = new ContentValues();
           values.put(People.NUMBER, s2);
           values.put(People.TYPE, Phone.TYPE_CUSTOM);
           values.put(People.LABEL, s1);
           values.put(People.NAME, s1);
           Uri dataUri = getContentResolver().insert(People.CONTENT_URI, values);
           Uri updateUri = Uri.withAppendedPath(dataUri, People.Phones.CONTENT_DIRECTORY);
           
           values.clear();
           values.put(People.Phones.TYPE, People.TYPE_MOBILE);
           values.put(People.NUMBER, s2);
           updateUri = getContentResolver().insert(updateUri, values);
           
           
           Intent i = new Intent(Intent.ACTION_INSERT_OR_EDIT);
                 i.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);   

                 startActivity(i);
        
   }
  });
 }
 
 
}

activity_main.xml


<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.example.phonecontacts.MainActivity$PlaceholderFragment" >



    <TextView

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_alignParentTop="true"

        android:layout_marginLeft="33dp"

        android:layout_marginTop="46dp"

        android:text="Name" />



    <EditText

        android:id="@+id/editText1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignBaseline="@+id/textView1"

        android:layout_alignBottom="@+id/textView1"

        android:layout_marginLeft="39dp"

        android:layout_toRightOf="@+id/textView1"

        android:ems="10" >



        <requestFocus />

    </EditText>



    <TextView

        android:id="@+id/textView2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@+id/editText1"

        android:layout_marginTop="48dp"

        android:layout_toLeftOf="@+id/editText1"

        android:text="Phone No" />



    <EditText

        android:id="@+id/editText2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/editText1"

        android:layout_alignTop="@+id/textView2"

        android:inputType="number"

        android:ems="10" />



    <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@+id/editText2"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="82dp"

        android:text="Submit" />



</RelativeLayout>
On clicking the submit button the contacts will be saved to the phone contacts.

Comments

Popular posts from this blog

Server communication in Android

In Android we can communicate with server using HTTP request methods for getting some details from the server.We use RESTful service.Mainly we use two types of HTTP request HTTP GET(),HTTP POST() methods. In some case we have to give some values to the server for fetching some informations, then we use commonly HTTP POST() and if we simply want to request for some datas we use HTTP GET(). Lets see HTTP POST() method. public void getResponse(String a, String b) { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("URL"); //here write the url List < NameValuePair > nameValueList = new ArrayList < NameValuePair > (2); nameValueList.add(new BasicNameValuePair("keyword", a)); nameValueList.add(new BasicNameValuePair("keyword", b)); try { httppost.setEntity(new UrlEncodedFormEntity(nameValueList)); HttpResponse response = httpclient.execute(httppost); Log.i(...

How to create a flash light on Android

I always wonder the use of a flash light on a phone. This simple app is more useful than anything. So here is the code to build a flashlight app, MainActivity.java import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.pm.PackageManager; import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ImageButton; public class MainActivity extends Activity { ImageButton btnSwitch; private Camera camera; private boolean isFlashOn; private boolean hasFlash; Parameters params; MediaPlayer mp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // flash switch button ...

How to record and save a video in Android

Let's record a video :). Check the code snippet. First need to give necessary permissions. So add these permissions to AndroidManifest.xml   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />   <uses-permission android:name="android.permission.RECORD_AUDIO" /> MainActivity.java import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { Button btn_record; pu...