Hello Guys,
Here I am going to share an example of Get contacts from your device phone directory.
For getting all contacts from your phone directory you can can simple use of Contact Intent.
Sample Code:
File: PickContactActivity.java
package com.sneha.pickcontact;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
public class PickContactActivity extends Activity
{
Button btn_contact;
String name;
private static final int PICK_CONTACT = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_pick_contact);
btn_contact = (Button) findViewById(R.id.button1);
btn_contact.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.pick_contact, menu);
return true;
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data)
{
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode)
{
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK)
{
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst())
{
name = c.getString(c.getColumnIndex (ContactsContract. Contacts. DISPLAY_NAME));
}
}
break;
}
}
}
File: activity_pick_contact.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/bg"
android:gravity="center">
<Button
android:id="@+id/button1"
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="@drawable/contact_btn"/>
</LinearLayout>
File: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sneha.pickcontact"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".PickContactActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You can download background image and contact button from here.
Background Image:
Contact Button Image:
Output:
Enjoy Coding...!!!
Here I am going to share an example of Get contacts from your device phone directory.
For getting all contacts from your phone directory you can can simple use of Contact Intent.
Sample Code:
File: PickContactActivity.java
package com.sneha.pickcontact;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
public class PickContactActivity extends Activity
{
Button btn_contact;
String name;
private static final int PICK_CONTACT = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_pick_contact);
btn_contact = (Button) findViewById(R.id.button1);
btn_contact.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.pick_contact, menu);
return true;
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data)
{
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode)
{
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK)
{
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst())
{
name = c.getString(c.getColumnIndex (ContactsContract. Contacts. DISPLAY_NAME));
}
}
break;
}
}
}
File: activity_pick_contact.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/bg"
android:gravity="center">
<Button
android:id="@+id/button1"
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="@drawable/contact_btn"/>
</LinearLayout>
File: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sneha.pickcontact"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".PickContactActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You can download background image and contact button from here.
Background Image:
Contact Button Image:
Output:
Enjoy Coding...!!!
No comments:
Post a Comment