Course Content

Course Content

Description

Android Content Provider Example

In this Video tutorial we will learn about Android Content Provider Example in Hindi. First of all we will create an application named 'ContactsAdd', and then inside its 'AndroidManifest.xml' file, we will add two permissions,

<uses-permission android:name=""android.permission.READ_CONTACTS""/>

<uses-permission android:name=""android.permission.WRITE_CONTACTS""/>

 

After that we will design the application's layout in our 'activity_main.xml', for that we will be using five EditTexts for name, mobile phone, home phone, work email, and home email. Also we will use two buttons, one for Add contact, and other for Contact list.

Now we will open our 'MainActivity.java' file from 'java' folder, and now we have to write code for Adding Contact using content provider.

First of all we have to create one Listener for a button,

OnClickListener addOnClickListener=new OnClickListener ()

{

@Override

public void onClick (View v)

{

EditText etName=(EditText) findViewById(R.id.et_name);

EditText etMobile=(EditText) findViewById(R.id.et_mobile_phone);

EditText etHomePhone=(EditText) findViewById(R.id.et_home_phone);

EditText etHomeEmail=(EditText) findViewById(R.id.et_home_email);

EditText etWorkEmail=(EditText) findViewById(R.id.et_work_email);

 

Now we will use an array list,

ArrayList<ContentProviderOperation> opa=new ArrayList<ContentProviderOperation>();

int rawContactID=ops.size();

 

Now whatever options we want to provide we will use it with 'ops.add(_);' ,

ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI));

.withValue(RawContacts.ACCOUNT_NAME,null)

.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE,null)

.build()};

 

Similarly we have to add all the contents which we want,

ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)

.withValueBackReference(ContactContract.Data.RAW_CONTACT, rawContactID)

.withValue(ContactContract.Data.MIMETYPE, StructureName.CONTENT_ITEM_TYPE)

.withValue(StructureName.DISPLAY_NAME, etName.getText().toString()

}

.build()

};

 After that we need to define all the different contents which we used in our application like phone number, name, e-mail, work e-mail, etc...

So after that we will now generator toast notification when any contact is added,

Toast.makeText(getBaseContext(), ""Contact is successfully added"", Toast.LENGTH_SHORT).show();

Now to use OnClickListener we will design code for that and than our coding is complete. After that we will execute it.

As soon as we run an application we are displayed five editText and two buttons as we coded, and then when we fill all the details and click on Add Contact button, than our toast notification is also working completely showing ""Contact is successfully added"".

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
297K+ Downloads
App Download Section Circle 1
4.5 Rating
App Download Section Circle 2
10K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere