- activity_main.xml
<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:orientation="vertical"
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=".MainActivity" >
<EditText
android:id="@+id/edtDisplayName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:ems="10"
android:hint="@string/DisplayName" >
</EditText>
<EditText
android:id="@+id/edtMobileNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:ems="10"
android:hint="@string/MobileNumber"
android:inputType="phone" />
<EditText
android:id="@+id/edtHomeNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:ems="10"
android:hint="@string/HomeNumber"
android:inputType="phone" />
<EditText
android:id="@+id/edtWorkNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:ems="10"
android:hint="@string/WorkNumber"
android:inputType="phone" />
<EditText
android:id="@+id/edtemailID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:ems="10"
android:hint="@string/emailID"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/edtcompany"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:ems="10"
android:hint="@string/company" />
<EditText
android:id="@+id/edtjobTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:ems="10"
android:hint="@string/jobTitle" />
<Button
android:id="@+id/btnAddContact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:text="@string/AddInPhoneBook" />
</LinearLayout>
- AndroidManifest.xml
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
- MainActivity.java
public class MainActivity extends Activity {
private EditText edtDisplayName, edtMobileNumber, edtHomeNumber,
edtWorkNumber, edtemailID, edtcompany, edtjobTitle;
private Button btnAddContact;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtDisplayName = (EditText) findViewById(R.id.edtDisplayName);
edtMobileNumber = (EditText) findViewById(R.id.edtMobileNumber);
edtHomeNumber = (EditText) findViewById(R.id.edtHomeNumber);
edtWorkNumber = (EditText) findViewById(R.id.edtWorkNumber);
edtemailID = (EditText) findViewById(R.id.edtemailID);
edtcompany = (EditText) findViewById(R.id.edtcompany);
edtjobTitle = (EditText) findViewById(R.id.edtjobTitle);
btnAddContact = (Button) findViewById(R.id.btnAddContact);
btnAddContact.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation
.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE,
null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME,
null).build());
// -------------- Names --------------
if (edtDisplayName.getText().toString() != null) {
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
edtDisplayName.getText().toString())
.build());
}
// -------------- Mobile Number --------------
if (edtMobileNumber.getText().toString() != null) {
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.Phone.NUMBER,
edtMobileNumber.getText().toString())
.withValue(
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
.build());
}
// -------------- Home Numbers --------------
if (edtHomeNumber.getText().toString() != null) {
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.Phone.NUMBER,
edtHomeNumber.getText().toString())
.withValue(
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_HOME)
.build());
}
// --------------Work Numbers --------------
if (edtWorkNumber.getText().toString() != null) {
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.Phone.NUMBER,
edtWorkNumber.getText().toString())
.withValue(
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_WORK)
.build());
}
// -------------- Email --------------
if (edtemailID.getText().toString() != null) {
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.Email.DATA,
edtemailID.getText().toString())
.withValue(
ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.build());
}
// -------------- Organization --------------
if (!edtcompany.getText().toString().equals("")
&& !edtjobTitle.getText().toString().equals("")) {
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.Organization.COMPANY,
edtcompany.getText().toString())
.withValue(
ContactsContract.CommonDataKinds.Organization.TYPE,
ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
.withValue(
ContactsContract.CommonDataKinds.Organization.TITLE,
edtjobTitle.getText().toString())
.withValue(
ContactsContract.CommonDataKinds.Organization.TYPE,
ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
.build());
}
// Asking the Contact provider to create a new contact
try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY,
ops);
edtDisplayName.setText("");
edtMobileNumber.setText("");
edtHomeNumber.setText("");
edtWorkNumber.setText("");
edtemailID.setText("");
edtcompany.setText("");
edtjobTitle.setText("");
Toast.makeText(getApplicationContext(), "Contact Save",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"Exception: " + e.getMessage(), Toast.LENGTH_SHORT)
.show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
No comments:
Post a Comment