Free Shipping

Secure Payment

easy returns

24/7 support

  • Home
  • Blog
  • Android Custom Content Provider with Example

Android Custom Content Provider with Example

 July 8  | 0 Comments

Introduction:

ContentProvider is one of the pillars of Android. It is a component which

i)- hides database details (database name, table name, column info. etc.)

ii)- allows the application to share data among multiple applications.

Steps to create ContentProvider:

i)- Create ContentProvider subclass (android.ContentProvider).

ii)- Register ContentProvider in AndroidManifest.xml file using provider element. (<provider>).

To Register ContentProvider use following attributes:

i)- android:name -> ContentProvider subclass name.

ii)- android:authorities -> authority used for ContentProvider

iii)- android:exported -> true/false.

true: ContentProvider can be used in other applications.

false: ContentProvider can be used in local application.

By default, it is “true”.

URI:

i)- Stands for Universal Resource Identifier.

ii)- URI is used to identify a ContentProvider uniquely in the current device.

iii)- URI has a special format i.e.

Content://Authority/Path

where,

content -> protocol used to communicate with the ContentProvider.

Authority -> A unique identifier used to identify ContentProvider (generally Authority is the name of the package in which ContentProvider is registered).

Path -> It is a unique string that appears after authority which is used to identify the table used in an operation.

Let’s Create Custom ContentProvider programmatically with the help of Example.

Example with Code:

  1. Create an Application AndroidContentProviderExample.

Requirements:

StudentProvider.java

activity_main.xml

MainActivity.java

Let’s do it one by one.

StudentProvider.java:

Create a subclass of ContentProvider in your application and override its methods.

Add the code

Initialize the fields for your ContentProvider and database.

To know the coming URI is of which type, we use URIMatcher class, it matches the patterns.

Add the code in these methods onCreate(), query(), update(), delete(), insert(), getType().

onCreate():

Add the code in this method

query(….):

Add the code in this method

Here we set the table and maps all database column names.

insert(..):

Here, in this method, we add the record.

Add the code

update(..):

Add the code here

delete(..):

To delete all the records of the table.

Add the code

 

getType(..):

In this method, we get all the records or a particular record with using id.

Add the code here

activity_main.xml:

Create an XML file of the main layout.

Add the code inside RelativeLayout.

MainActivity.java:

Inside this, we implement three onclick methods for add, delete and show the records of students.

Add the code for adding a new student record inside addStudent() method:

Add the code for delete all the records inside deleteAllStudentsRecord() method:

Add the code for show all the records inside showAllStudentsRecord() method:

Conclusion:

This is the basic idea of how to create Custom ContentProvider in your application. We can create our own ContentProvider and use in many applications to share the data. It is one of the important components of Android and it is very popular in Android development.

Output:

 

Before Add

 

After Add

 

Show the Record

 

After Delete

 

After Delete when clicking on Show all the Students Button.

>