Monday 4 February 2013

Contact Picker in android

Contact Picker one of the impotent utility which we are using many of app. with the help of this we can pick contact from our phone contact list and show/use them into our app.

1) xml layout for Contact Picker

                      <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" >

                            <EditText
                                android:id="@+id/editText1"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:ems="10"
                                android:hint="Select Contact" >

                                <requestFocus />
                            </EditText>

                            <Button
                                android:id="@+id/button1"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:text="Pick " />
                        </LinearLayout>

2) Activity java Code:-

public class Scheduler extends Activity implements OnClickListener {
private Button b1;
private EditText contact;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule);
b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(this);
contact=(EditText)findViewById(R.id.editText1);

}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0==b1)
{
Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);
}

}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri ur=data.getData();
Cursor c=managedQuery(ur, null, null, null, null);
if(c.moveToFirst())
{
String s=c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contact.setText(s);
}
}
}

}


1 comment:

  1. Layout is wrong. Should be
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    method managedQuery is deprecated.

    You seriously need to update your SDK.

    ReplyDelete