Site icon C1CTech

Android Content Provider Example using SQLite Database

<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>ContentProvider<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>A <strong><span style&equals;"color&colon; &num;008000&semi;">ContentProvider<&sol;span><&sol;strong> provides data from one application to another&comma; when requested&period; It manages access to a structured set of data&period; It provides mechanisms for defining data security&lpar;i&period;e&period; by enforcing read&sol;write permissions&rpar;<em>&period; <&sol;em>ContentProvider offer a standard interface that connects data in one process with code running in another process&period;<&sol;p>&NewLine;<p>When an application wants to access the data of a ContentProvider&comma; it makes a request&period; These requests are handled by the <span style&equals;"color&colon; &num;008000&semi;"><strong>ContentResolver<&sol;strong><&sol;span> object&comma; which communicates with the ContentProvider as a client&period; The provider object receives data requests from clients&comma; performs the requested action&comma; and returns the results in <strong><span style&equals;"color&colon; &num;008000&semi;">cursor<&sol;span><&sol;strong> format&period;<&sol;p>&NewLine;<p>In this tutorial&comma; I will create two apps <strong><span style&equals;"color&colon; &num;0000ff&semi;">MyContacts App<&sol;span><&sol;strong> and <strong><span style&equals;"color&colon; &num;0000ff&semi;">ContactList App<&sol;span><&sol;strong> and I will demonstrate you how ContactList App will use the  MyContact App&&num;8217&semi;s data &lpar;stored in SQLite database &rpar; using contentProvider&period;<&sol;p>&NewLine;<p>Get <span style&equals;"color&colon; &num;000080&semi;"><strong>GITHUB<&sol;strong><&sol;span> code from <span style&equals;"color&colon; &num;00ff00&semi;"><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;contentprovider"><strong>Here<&sol;strong><&sol;a><&sol;span>&period;<&sol;span><&sol;p>&NewLine;<p><amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"900" data-videoid&equals;"FCvmrXcwzeU" title&equals;"Android Content Provider Example using SQLite Database"><a placeholder href&equals;"https&colon;&sol;&sol;www&period;youtube&period;com&sol;watch&quest;v&equals;FCvmrXcwzeU"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;FCvmrXcwzeU&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Android Content Provider Example using SQLite Database"><&sol;a><&sol;amp-youtube><&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Creating a New Project<&sol;span><&sol;strong><&sol;h3>&NewLine;<p><strong>1<&sol;strong>&period; Create a new project  named <span style&equals;"color&colon; &num;0000ff&semi;"><strong>MyContacts<&sol;strong><&sol;span> in <span style&equals;"color&colon; &num;008000&semi;"><strong>Android Studio<&sol;strong><&sol;span> from <strong><span style&equals;"color&colon; &num;008000&semi;">File<&sol;span> &Implies; <span style&equals;"color&colon; &num;008000&semi;">New Project<&sol;span><&sol;strong> by filling the required details&period; When it prompts you to select the activity&comma; choose <span style&equals;"color&colon; &num;008000&semi;"><strong>Empty Activity<&sol;strong><&sol;span> and continue&period;<&sol;p>&NewLine;<p><strong>2<&sol;strong>&period; Open <span style&equals;"color&colon; &num;008000&semi;"><strong>build&period;gradle<&sol;strong><&sol;span> located in app level and add the following dependency&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">build&period;gradle<&sol;span><&sol;strong><&sol;p>&NewLine;<pre><code>dependencies &lbrace;&NewLine; &NewLine; compile 'com&period;android&period;support&colon;design&colon;27&period;1&period;1'&NewLine; &NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;recyclerView<&sol;strong><&sol;span>&NewLine; implementation 'com&period;android&period;support&colon;recyclerview-v7&colon;27&period;0&period;2'&NewLine; &NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Create SQLite Database for MyContacts App<&sol;strong><&sol;span><&sol;h3>&NewLine;<p><strong>3<&sol;strong>&period; Create a new Java class <span style&equals;"color&colon; &num;008000&semi;"><strong>DBOpenHelper<&sol;strong><&sol;span> and add the following code&period; This is our DBHelper class&comma; here we are creating the Contacts Database&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">DBOpenHelper&period;Java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre><code>package com&period;c1ctech&period;mycontacts&semi;&NewLine;&NewLine;import android&period;content&period;Context&semi;&NewLine;import android&period;database&period;sqlite&period;SQLiteDatabase&semi;&NewLine;import android&period;database&period;sqlite&period;SQLiteOpenHelper&semi;&NewLine;&NewLine;public class DBOpenHelper extends SQLiteOpenHelper &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Constants for db name and version<&sol;span><&sol;strong>&NewLine; private static final String DATABASE&lowbar;NAME &equals; "contacts&period;db"&semi;&NewLine; private static final int DATABASE&lowbar;VERSION &equals; 1&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Constants for table and columns<&sol;span><&sol;strong>&NewLine; public static final String TABLE&lowbar;CONTACTS &equals; "contacts"&semi;&NewLine; public static final String CONTACT&lowbar;ID &equals; "&lowbar;id"&semi;&NewLine; public static final String CONTACT&lowbar;NAME &equals; "contactName"&semi;&NewLine; public static final String CONTACT&lowbar;PHONE &equals; "contactPhone"&semi;&NewLine; public static final String CONTACT&lowbar;CREATED&lowbar;ON &equals; "contactCreationTimeStamp"&semi;&NewLine;&NewLine; public static final String&lbrack;&rsqb; ALL&lowbar;COLUMNS &equals;&NewLine; &lbrace;CONTACT&lowbar;ID&comma; CONTACT&lowbar;NAME&comma; CONTACT&lowbar;PHONE&comma; CONTACT&lowbar;CREATED&lowbar;ON&rcub;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Create Table<&sol;span><&sol;strong>&NewLine; private static final String CREATE&lowbar;TABLE &equals;&NewLine; "CREATE TABLE " &plus; TABLE&lowbar;CONTACTS &plus; " &lpar;" &plus;&NewLine; CONTACT&lowbar;ID &plus; " INTEGER PRIMARY KEY AUTOINCREMENT&comma; " &plus;&NewLine; CONTACT&lowbar;NAME &plus; " TEXT&comma; " &plus;&NewLine; CONTACT&lowbar;PHONE &plus; " TEXT&comma; " &plus;&NewLine; CONTACT&lowbar;CREATED&lowbar;ON &plus; " TEXT default CURRENT&lowbar;TIMESTAMP" &plus;&NewLine; "&rpar;"&semi;&NewLine;&NewLine; public DBOpenHelper&lpar;Context context&rpar; &lbrace;&NewLine; super&lpar;context&comma; DATABASE&lowbar;NAME&comma; null&comma; DATABASE&lowbar;VERSION&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onCreate&lpar;SQLiteDatabase sqLiteDatabase&rpar; &lbrace;&NewLine; sqLiteDatabase&period;execSQL&lpar;CREATE&lowbar;TABLE&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onUpgrade&lpar;SQLiteDatabase sqLiteDatabase&comma; int i&comma; int i1&rpar; &lbrace;&NewLine; sqLiteDatabase&period;execSQL&lpar;"DROP TABLE IF EXISTS " &plus; TABLE&lowbar;CONTACTS&rpar;&semi;&NewLine; onCreate&lpar;sqLiteDatabase&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;<&sol;code><&sol;pre>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Create Custom Android Content Provider<&sol;strong><&sol;span><&sol;h3>&NewLine;<p><strong>4<&sol;strong>&period; Create a new Java class named <span style&equals;"color&colon; &num;008000&semi;"><strong>ContactsProvider<&sol;strong><&sol;span> and make it as subclass of <span style&equals;"color&colon; &num;008000&semi;"><strong>ContentProvider<&sol;strong><&sol;span> and add the below code&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating ContentUri<&sol;strong><&sol;span><&sol;h4>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">ContactsProvider<&sol;span><&sol;strong> is a subclass of <span style&equals;"color&colon; &num;008000&semi;"><strong>ContentProvider<&sol;strong><&sol;span> and in order to identify the data in the provider we use <strong><span style&equals;"color&colon; &num;008000&semi;">Content URIs<&sol;span><&sol;strong>&period; A Content URI  has the following format&colon;<&sol;p>&NewLine;<div id&equals;"highlighter&lowbar;705376" class&equals;"syntaxhighlighter ">&NewLine;<div class&equals;"lines">&NewLine;<pre class&equals;"line alt1"><strong><span style&equals;"color&colon; &num;000080&semi;">content&colon;&sol;&sol;&lt&semi;authority&gt&semi;&sol;&lt&semi;path&gt&semi;&sol;&lt&semi;id&gt&semi;<&sol;span><&sol;strong><&sol;pre>&NewLine;<div><strong><span style&equals;"color&colon; &num;0000ff&semi;">content&colon;&sol;&sol; &colon;<&sol;span><&sol;strong> Standard prefix indicating the data is controlled by content provider&period;<&sol;div>&NewLine;<div><strong><span style&equals;"color&colon; &num;0000ff&semi;">Authority &colon; <&sol;span><&sol;strong>Uniquely identify the particular content provider&lpar;package name&rpar;&period;<&sol;div>&NewLine;<div><strong><span style&equals;"color&colon; &num;0000ff&semi;">path &colon;<&sol;span><&sol;strong> Used to determine the type of data&lpar;table or file&rpar; being requested&period;<&sol;div>&NewLine;<div><strong><span style&equals;"color&colon; &num;0000ff&semi;">id &colon;<&sol;span><&sol;strong> specifies which record&lpar;row&rpar; is being requested&period;&lpar;optional&rpar;&period;<&sol;div>&NewLine;<div><&sol;div>&NewLine;<div><&sol;div>&NewLine;<div>&NewLine;<pre><code>private static final String AUTHORITY &equals; "com&period;c1ctech&period;mycontacts"&semi;&NewLine;private static final String BASE&lowbar;PATH &equals; "contacts"&semi;&NewLine;public static final Uri CONTENT&lowbar;URI &equals; Uri&period;parse&lpar;"content&colon;&sol;&sol;" &plus; AUTHORITY &plus; "&sol;" &plus; BASE&lowbar;PATH&rpar;&semi;<&sol;code><&sol;pre>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating UriMatcher<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>Remember the ContentResolver is not aware of the internal implementation of our Content Provider class&comma; so whenever we receive a call from the ContentResolver we have to perform an internal check to see if the call was for a the whole table or for a single row in the table&period;<&sol;p>&NewLine;<p>The way we determine if the call is for a specific row on the table is if the calls contains any qualifier after the table name such as <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;authority&sol;path&sol;table&sol;2<&sol;strong><&sol;span> and if the calls is the format of say  <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;authority&sol;path&sol;table<&sol;span><&sol;strong> then we know it is a query for the table&period;<&sol;p>&NewLine;<p>the provider API includes the convenience class <span style&equals;"color&colon; &num;008000&semi;"><strong>UriMatcher<&sol;strong><&sol;span>&comma; which maps content URI &&num;8220&semi;patterns&&num;8221&semi; to integer values&period; Add the following code to your class &comma; the &OpenCurlyDoubleQuote;<strong><span style&equals;"color&colon; &num;008000&semi;">&num;<&sol;span><&sol;strong>” represents any number after the table indicating a table row&period;<&sol;p>&NewLine;<pre><code>private static final UriMatcher uriMatcher &equals; new UriMatcher&lpar;UriMatcher&period;NO&lowbar;MATCH&rpar;&semi;&NewLine;&NewLine;static &lbrace;&NewLine; uriMatcher&period;addURI&lpar;AUTHORITY&comma; BASE&lowbar;PATH&comma; CONTACTS&rpar;&semi;&NewLine; uriMatcher&period;addURI&lpar;AUTHORITY&comma; BASE&lowbar;PATH &plus; "&sol;&num;"&comma; CONTACT&lowbar;ID&rpar;&semi;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Override ContentProvider&&num;8217&semi;s Methods<&sol;span><&sol;strong><&sol;h4>&NewLine;<p>Here is the list of methods which you need to override in <strong><span style&equals;"color&colon; &num;008000&semi;">ContentProvider<&sol;span><&sol;strong> class to have your ContentProvider working&period;All of these methods except <strong><span style&equals;"color&colon; &num;008000&semi;">onCreate&lpar;&rpar;<&sol;span><&sol;strong> are called by a client application that is attempting to access your content provider&colon;<&sol;p>&NewLine;<ul>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">onCreate&lpar;&rpar; <&sol;span><&sol;strong>method is called when the provider is started&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">query&lpar;&rpar;<&sol;span><&sol;strong> method retrieves data from your provider&period;The result is returned as a Cursor object&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">insert&lpar;&rpar;<&sol;span><&sol;strong> method inserts a new record into the content provider&period;Return a content URI for the newly-inserted row&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">delete&lpar;&rpar;<&sol;span><&sol;strong> method deletes an existing record from the content provider&period;Return the number of rows deleted&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">update&lpar;&rpar;<&sol;span><&sol;strong> method updates an existing record from the content provider&period;Return the number of rows updated&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">getType&lpar;&rpar;<&sol;span><&sol;strong> method returns the MIME type corresponding to a content URI&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">ContactsProvider&period;Java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre><code>package com&period;c1ctech&period;mycontacts&semi;&NewLine;&NewLine;import android&period;content&period;ContentProvider&semi;&NewLine;import android&period;content&period;ContentUris&semi;&NewLine;import android&period;content&period;ContentValues&semi;&NewLine;import android&period;content&period;UriMatcher&semi;&NewLine;import android&period;database&period;Cursor&semi;&NewLine;import android&period;database&period;SQLException&semi;&NewLine;import android&period;database&period;sqlite&period;SQLiteDatabase&semi;&NewLine;import android&period;net&period;Uri&semi;&NewLine;import android&period;support&period;annotation&period;Nullable&semi;&NewLine;&NewLine;public class ContactsProvider extends ContentProvider &lbrace;&NewLine;&NewLine; private static final String AUTHORITY &equals; "com&period;c1ctech&period;mycontacts"&semi;&NewLine; private static final String BASE&lowbar;PATH &equals; "contacts"&semi;&NewLine; public static final Uri CONTENT&lowbar;URI &equals; Uri&period;parse&lpar;"content&colon;&sol;&sol;" &plus; AUTHORITY &plus; "&sol;" &plus; BASE&lowbar;PATH&rpar;&semi;&NewLine;&NewLine; private static final int CONTACTS &equals; 1&semi;&NewLine; private static final int CONTACT&lowbar;ID &equals; 2&semi;&NewLine;&NewLine; private static final UriMatcher uriMatcher &equals; new UriMatcher&lpar;UriMatcher&period;NO&lowbar;MATCH&rpar;&semi;&NewLine;&NewLine; static &lbrace;&NewLine; uriMatcher&period;addURI&lpar;AUTHORITY&comma; BASE&lowbar;PATH&comma; CONTACTS&rpar;&semi;&NewLine; uriMatcher&period;addURI&lpar;AUTHORITY&comma; BASE&lowbar;PATH &plus; "&sol;&num;"&comma; CONTACT&lowbar;ID&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; private SQLiteDatabase database&semi;&NewLine;&NewLine; &commat;Override&NewLine; public boolean onCreate&lpar;&rpar; &lbrace;&NewLine; DBOpenHelper helper &equals; new DBOpenHelper&lpar;getContext&lpar;&rpar;&rpar;&semi;&NewLine; database &equals; helper&period;getWritableDatabase&lpar;&rpar;&semi;&NewLine; return true&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Nullable&NewLine; &commat;Override&NewLine; public Cursor query&lpar;Uri uri&comma; String&lbrack;&rsqb; strings&comma; String s&comma; String&lbrack;&rsqb; strings1&comma; String s1&rpar; &lbrace;&NewLine; Cursor cursor&semi;&NewLine; switch &lpar;uriMatcher&period;match&lpar;uri&rpar;&rpar; &lbrace;&NewLine; case CONTACTS&colon;&NewLine; cursor &equals; database&period;query&lpar;DBOpenHelper&period;TABLE&lowbar;CONTACTS&comma; DBOpenHelper&period;ALL&lowbar;COLUMNS&comma;&NewLine; s&comma; null&comma; null&comma; null&comma; DBOpenHelper&period;CONTACT&lowbar;NAME &plus; " ASC"&rpar;&semi;&NewLine;&NewLine;&NewLine; break&semi;&NewLine; default&colon;&NewLine; throw new IllegalArgumentException&lpar;"This is an Unknown URI " &plus; uri&rpar;&semi;&NewLine; &rcub;&NewLine; cursor&period;setNotificationUri&lpar;getContext&lpar;&rpar;&period;getContentResolver&lpar;&rpar;&comma; uri&rpar;&semi;&NewLine;&NewLine; return cursor&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Nullable&NewLine; &commat;Override&NewLine; public String getType&lpar;Uri uri&rpar; &lbrace;&NewLine;&NewLine; switch &lpar;uriMatcher&period;match&lpar;uri&rpar;&rpar; &lbrace;&NewLine; case CONTACTS&colon;&NewLine; return "vnd&period;android&period;cursor&period;dir&sol;contacts"&semi;&NewLine; default&colon;&NewLine; throw new IllegalArgumentException&lpar;"This is an Unknown URI " &plus; uri&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine; &commat;Nullable&NewLine; &commat;Override&NewLine; public Uri insert&lpar;Uri uri&comma; ContentValues contentValues&rpar; &lbrace;&NewLine; long id &equals; database&period;insert&lpar;DBOpenHelper&period;TABLE&lowbar;CONTACTS&comma; null&comma; contentValues&rpar;&semi;&NewLine;&NewLine; if &lpar;id &gt&semi; 0&rpar; &lbrace;&NewLine; Uri &lowbar;uri &equals; ContentUris&period;withAppendedId&lpar;CONTENT&lowbar;URI&comma; id&rpar;&semi;&NewLine; getContext&lpar;&rpar;&period;getContentResolver&lpar;&rpar;&period;notifyChange&lpar;&lowbar;uri&comma; null&rpar;&semi;&NewLine;&NewLine; return &lowbar;uri&semi;&NewLine; &rcub;&NewLine; throw new SQLException&lpar;"Insertion Failed for URI &colon;" &plus; uri&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public int delete&lpar;Uri uri&comma; String s&comma; String&lbrack;&rsqb; strings&rpar; &lbrace;&NewLine; int delCount &equals; 0&semi;&NewLine; switch &lpar;uriMatcher&period;match&lpar;uri&rpar;&rpar; &lbrace;&NewLine; case CONTACTS&colon;&NewLine; delCount &equals; database&period;delete&lpar;DBOpenHelper&period;TABLE&lowbar;CONTACTS&comma; s&comma; strings&rpar;&semi;&NewLine; break&semi;&NewLine; default&colon;&NewLine; throw new IllegalArgumentException&lpar;"This is an Unknown URI " &plus; uri&rpar;&semi;&NewLine; &rcub;&NewLine; getContext&lpar;&rpar;&period;getContentResolver&lpar;&rpar;&period;notifyChange&lpar;uri&comma; null&rpar;&semi;&NewLine; return delCount&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public int update&lpar;Uri uri&comma; ContentValues contentValues&comma; String s&comma; String&lbrack;&rsqb; strings&rpar; &lbrace;&NewLine; int updCount &equals; 0&semi;&NewLine; switch &lpar;uriMatcher&period;match&lpar;uri&rpar;&rpar; &lbrace;&NewLine; case CONTACTS&colon;&NewLine; updCount &equals; database&period;update&lpar;DBOpenHelper&period;TABLE&lowbar;CONTACTS&comma; contentValues&comma; s&comma; strings&rpar;&semi;&NewLine; break&semi;&NewLine; default&colon;&NewLine; throw new IllegalArgumentException&lpar;"This is an Unknown URI " &plus; uri&rpar;&semi;&NewLine; &rcub;&NewLine; getContext&lpar;&rpar;&period;getContentResolver&lpar;&rpar;&period;notifyChange&lpar;uri&comma; null&rpar;&semi;&NewLine; return updCount&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Register the Content Provider<&sol;strong><&sol;span><&sol;h3>&NewLine;<p><strong>5<&sol;strong>&period; We need to provide an entry for our custom Android Content Provider in the<span style&equals;"color&colon; &num;008000&semi;"> <strong>AndroidManifest&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<p>Open your <span style&equals;"color&colon; &num;008000&semi;"><strong>AndroidManifest&period;xml<&sol;strong><&sol;span> and add the permissions and provider entry as in the following code&period;<&sol;p>&NewLine;<p>we need to provide the authority and read and write permissions here&period;<br &sol;>&NewLine;using <span style&equals;"color&colon; &num;008000&semi;"><strong>android&colon;exported&equals;”true”<&sol;strong><&sol;span> our provider becomes available to other applications&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>AndroidManifest&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre><code>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;manifest xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; package&equals;"com&period;c1ctech&period;mycontacts"&gt&semi;&NewLine;&NewLine; &lt&semi;permission android&colon;name&equals;"com&period;c1ctech&period;mycontacts&period;READ&lowbar;DATABASE" android&colon;protectionLevel&equals;"normal" &sol;&gt&semi;&NewLine; &lt&semi;permission android&colon;name&equals;"com&period;c1ctech&period;mycontacts&period;WRITE&lowbar;DATABASE" android&colon;protectionLevel&equals;"normal" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;application&NewLine; android&colon;allowBackup&equals;"true"&NewLine; android&colon;icon&equals;"&commat;mipmap&sol;ic&lowbar;launcher"&NewLine; android&colon;label&equals;"&commat;string&sol;app&lowbar;name"&NewLine; android&colon;roundIcon&equals;"&commat;mipmap&sol;ic&lowbar;launcher&lowbar;round"&NewLine; android&colon;supportsRtl&equals;"true"&NewLine; android&colon;theme&equals;"&commat;style&sol;AppTheme"&gt&semi;&NewLine; &lt&semi;activity android&colon;name&equals;"&period;MainActivity"&gt&semi;&NewLine; &lt&semi;intent-filter&gt&semi;&NewLine; &lt&semi;action android&colon;name&equals;"android&period;intent&period;action&period;MAIN" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;category android&colon;name&equals;"android&period;intent&period;category&period;LAUNCHER" &sol;&gt&semi;&NewLine; &lt&semi;&sol;intent-filter&gt&semi;&NewLine; &lt&semi;&sol;activity&gt&semi;&NewLine;&NewLine; &lt&semi;provider&NewLine; android&colon;authorities&equals;"com&period;c1ctech&period;mycontacts"&NewLine; android&colon;name&equals;"&period;ContactsProvider"&NewLine; android&colon;exported&equals;"true"&NewLine; android&colon;readPermission&equals;"com&period;c1ctech&period;mycontacts&period;READ&lowbar;DATABASE"&NewLine; android&colon;writePermission&equals;"com&period;c1ctech&period;mycontacts&period;WRITE&lowbar;DATABASE"&NewLine; &sol;&gt&semi;&NewLine; &lt&semi;&sol;application&gt&semi;&NewLine;&lt&semi;&sol;manifest&gt&semi;&NewLine;<&sol;code><&sol;pre>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating Layouts<&sol;strong><&sol;span><&sol;h3>&NewLine;<p><strong>6<&sol;strong>&period; Open <span style&equals;"color&colon; &num;008000&semi;"><strong>activity&lowbar;main<&sol;strong><&sol;span> and write the below code&period;It contains one recyclerView and one floatingActionButton&lpar;ic&lowbar;input&lowbar;add&rpar; for adding contacts in recyclerview&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">activity&lowbar;main&period;xml<&sol;span><&sol;strong><&sol;p>&NewLine;<pre><code>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;android&period;support&period;design&period;widget&period;CoordinatorLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; tools&colon;context&equals;"&period;MainActivity"&gt&semi;&NewLine;&NewLine; &lt&semi;android&period;support&period;design&period;widget&period;AppBarLayout&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;theme&equals;"&commat;style&sol;AppTheme&period;AppBarOverlay"&gt&semi;&NewLine;&NewLine; &lt&semi;android&period;support&period;v7&period;widget&period;Toolbar&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;toolbar"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"&quest;attr&sol;actionBarSize"&NewLine; android&colon;background&equals;"&quest;attr&sol;colorPrimary"&NewLine; app&colon;popupTheme&equals;"&commat;style&sol;AppTheme&period;PopupOverlay" &sol;&gt&semi;&NewLine;&NewLine;&NewLine; &lt&semi;&sol;android&period;support&period;design&period;widget&period;AppBarLayout&gt&semi;&NewLine; &lt&semi;RelativeLayout&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&gt&semi;&NewLine;&NewLine; &lt&semi;android&period;support&period;v7&period;widget&period;RecyclerView&NewLine; android&colon;layout&lowbar;marginTop&equals;"70dp"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;contact&lowbar;list"&sol;&gt&semi;&NewLine;&NewLine; &lt&semi;&sol;RelativeLayout&gt&semi;&NewLine;&NewLine; &lt&semi;android&period;support&period;design&period;widget&period;FloatingActionButton&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;fab"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;gravity&equals;"bottom&vert;end"&NewLine; android&colon;layout&lowbar;margin&equals;"16dp"&NewLine; app&colon;srcCompat&equals;"&commat;android&colon;drawable&sol;ic&lowbar;input&lowbar;add" &sol;&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;android&period;support&period;design&period;widget&period;CoordinatorLayout&gt&semi;<&sol;code><&sol;pre>&NewLine;<p><strong>7<&sol;strong>&period; Create a new layout <strong><span style&equals;"color&colon; &num;008000&semi;">dialog&lowbar;contact&lowbar;details<&sol;span> <&sol;strong>and write the below code&period;It represents the layout set for an <span style&equals;"color&colon; &num;008000&semi;"><strong>Alertdialog<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">dialog&lowbar;contact&lowbar;details&period;xml<&sol;span><&sol;strong><&sol;p>&NewLine;<pre><code>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;LinearLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;layout&lowbar;root"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;orientation&equals;"vertical"&NewLine; android&colon;padding&equals;"10dp"&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;text&lowbar;view&lowbar;emp&lowbar;id"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;gravity&equals;"center"&NewLine; android&colon;text&equals;"&commat;string&sol;enter&lowbar;contact&lowbar;details"&NewLine; android&colon;textSize&equals;"20sp"&NewLine; android&colon;textStyle&equals;"bold" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;EditText&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;editTextDialogNameInput"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;hint&equals;"&commat;string&sol;enterName" &sol;&gt&semi;&NewLine;&NewLine;&NewLine; &lt&semi;EditText&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;editTextDialogPhoneInput"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;hint&equals;"&commat;string&sol;enterPhoneNumber" &sol;&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;LinearLayout&gt&semi;<&sol;code><&sol;pre>&NewLine;<p><strong>8<&sol;strong>&period; Under <span style&equals;"color&colon; &num;008000&semi;"><strong>res-&gt&semi;menu<&sol;strong><&sol;span> folder create a new menu resource file <span style&equals;"color&colon; &num;008000&semi;"><strong>main&lowbar;menu<&sol;strong><&sol;span> and write the below code&period;It contains one item <span style&equals;"color&colon; &num;008000&semi;"><strong>deleteAllContacts<&sol;strong><&sol;span> used to delete all contacts stored in provider&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>main&lowbar;menu&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre><code>&lt&semi;menu xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; tools&colon;context&equals;"com&period;androidtutorialpoint&period;mycontacts&period;MainActivity"&gt&semi;&NewLine; &lt&semi;item&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;deleteAllContacts"&NewLine; android&colon;orderInCategory&equals;"100"&NewLine; android&colon;title&equals;"&commat;string&sol;delete&lowbar;all&lowbar;contacts"&NewLine; app&colon;showAsAction&equals;"never" &sol;&gt&semi;&NewLine;&lt&semi;&sol;menu&gt&semi;&NewLine;<&sol;code><&sol;pre>&NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Creating Adapter For RecyclerView<&sol;span><&sol;strong><&sol;h4>&NewLine;<p><strong>9<&sol;strong>&period; Create a model class <strong><span style&equals;"color&colon; &num;008000&semi;">Contact<&sol;span><&sol;strong> in your root folder&lpar;com&period;c1ctech&period;mycontacts&rpar; used while working with recyclerView&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Contact&period;Java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre><code>package com&period;c1ctech&period;mycontacts&semi;&NewLine;&NewLine;public class Contact &lbrace;&NewLine;&NewLine; private String contact&lowbar;name&semi;&NewLine; private String contact&lowbar;phone&semi;&NewLine;&NewLine; public Contact&lpar;String contact&lowbar;name&comma; String contact&lowbar;phone&rpar; &lbrace;&NewLine; this&period;contact&lowbar;name &equals; contact&lowbar;name&semi;&NewLine; this&period;contact&lowbar;phone &equals; contact&lowbar;phone&semi;&NewLine; &rcub;&NewLine;&NewLine; public String getContact&lowbar;name&lpar;&rpar; &lbrace;&NewLine; return contact&lowbar;name&semi;&NewLine; &rcub;&NewLine;&NewLine; public void setContact&lowbar;name&lpar;String contact&lowbar;name&rpar; &lbrace;&NewLine; this&period;contact&lowbar;name &equals; contact&lowbar;name&semi;&NewLine; &rcub;&NewLine;&NewLine; public String getContact&lowbar;phone&lpar;&rpar; &lbrace;&NewLine; return contact&lowbar;phone&semi;&NewLine; &rcub;&NewLine;&NewLine; public void setContact&lowbar;phone&lpar;String contact&lowbar;phone&rpar; &lbrace;&NewLine; this&period;contact&lowbar;phone &equals; contact&lowbar;phone&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<p><strong>10<&sol;strong>&period; Create a new layout <span style&equals;"color&colon; &num;008000&semi;"><strong>contact&lowbar;list&lowbar;item<&sol;strong><&sol;span> and write the below code&period;It represents the layout of a single item of a <strong><span style&equals;"color&colon; &num;008000&semi;">recyclerview<&sol;span><&sol;strong> &period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">contact&lowbar;list&lowbar;item&period;xml<&sol;span><&sol;strong><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<pre><code>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;RelativeLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;gravity&equals;"center&lowbar;vertical"&NewLine; android&colon;padding&equals;"4dp"&gt&semi;&NewLine;&NewLine; &lt&semi;ImageView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;imageDocIcon"&NewLine; android&colon;layout&lowbar;width&equals;"72dp"&NewLine; android&colon;layout&lowbar;height&equals;"72dp"&NewLine; android&colon;src&equals;"&commat;drawable&sol;profile&lowbar;pic" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;nameTextView"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"36dp"&NewLine; android&colon;layout&lowbar;alignParentEnd&equals;"true"&NewLine; android&colon;layout&lowbar;alignParentRight&equals;"true"&NewLine; android&colon;layout&lowbar;marginLeft&equals;"15dp"&NewLine; android&colon;layout&lowbar;marginTop&equals;"5dp"&NewLine; android&colon;layout&lowbar;toEndOf&equals;"&commat;&plus;id&sol;imageDocIcon"&NewLine; android&colon;layout&lowbar;toRightOf&equals;"&commat;&plus;id&sol;imageDocIcon"&NewLine; android&colon;text&equals;"Dummy Text"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;black"&NewLine; android&colon;textSize&equals;"20sp"&NewLine; android&colon;textStyle&equals;"bold" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;phoneTextView"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"36dp"&NewLine; android&colon;layout&lowbar;alignParentEnd&equals;"true"&NewLine; android&colon;layout&lowbar;alignParentRight&equals;"true"&NewLine; android&colon;layout&lowbar;below&equals;"&commat;&plus;id&sol;nameTextView"&NewLine; android&colon;layout&lowbar;marginLeft&equals;"15dp"&NewLine; android&colon;layout&lowbar;toEndOf&equals;"&commat;&plus;id&sol;imageDocIcon"&NewLine; android&colon;layout&lowbar;toRightOf&equals;"&commat;&plus;id&sol;imageDocIcon"&NewLine; android&colon;text&equals;"Dummy Text"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;black"&NewLine; android&colon;textSize&equals;"15sp" &sol;&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;RelativeLayout&gt&semi;<&sol;code><&sol;pre>&NewLine;<p><strong>11<&sol;strong>&period; Create a class named <strong><span style&equals;"color&colon; &num;008000&semi;">ContactsAdapter<&sol;span><&sol;strong> and write the below code&period;Its object is used to set as adapter in recyclerView&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">ContactsAdapter&period;Java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre><code>package com&period;c1ctech&period;mycontacts&semi;&NewLine;&NewLine;import android&period;content&period;Context&semi;&NewLine;import android&period;support&period;annotation&period;NonNull&semi;&NewLine;import android&period;support&period;v7&period;widget&period;RecyclerView&semi;&NewLine;import android&period;view&period;LayoutInflater&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;import android&period;view&period;ViewGroup&semi;&NewLine;&NewLine;import android&period;widget&period;TextView&semi;&NewLine;&NewLine;import java&period;util&period;List&semi;&NewLine;&NewLine;public class ContactsAdapter extends RecyclerView&period;Adapter&lt&semi;ContactsAdapter&period;ViewHolder&gt&semi; &lbrace;&NewLine;&NewLine;&NewLine; Context mContext&semi;&NewLine; private List&lt&semi;Contact&gt&semi; items&semi;&NewLine;&NewLine; public ContactsAdapter&lpar;Context mContext&comma; List&lt&semi;Contact&gt&semi; items&rpar; &lbrace;&NewLine; this&period;mContext &equals; mContext&semi;&NewLine; this&period;items &equals; items&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;NonNull&NewLine; &commat;Override&NewLine; public ViewHolder onCreateViewHolder&lpar;ViewGroup parent&comma; int viewType&rpar; &lbrace;&NewLine; View v &equals; LayoutInflater&period;from&lpar;mContext&rpar;&period;inflate&lpar;R&period;layout&period;contact&lowbar;list&lowbar;item&comma; parent&comma; false&rpar;&semi;&NewLine; return new ViewHolder&lpar;v&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onBindViewHolder&lpar;ViewHolder holder&comma; int position&rpar; &lbrace;&NewLine; Contact item &equals; items&period;get&lpar;position&rpar;&semi;&NewLine; holder&period;phone&lowbar;no&period;setText&lpar;item&period;getContact&lowbar;phone&lpar;&rpar;&rpar;&semi;&NewLine; holder&period;name&period;setText&lpar;item&period;getContact&lowbar;name&lpar;&rpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public int getItemCount&lpar;&rpar; &lbrace;&NewLine; return items&period;size&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; public static class ViewHolder extends RecyclerView&period;ViewHolder &lbrace;&NewLine; public TextView name&semi;&NewLine; public TextView phone&lowbar;no&semi;&NewLine;&NewLine; public ViewHolder&lpar;View itemView&rpar; &lbrace;&NewLine; super&lpar;itemView&rpar;&semi;&NewLine; name &equals; &lpar;TextView&rpar; itemView&period;findViewById&lpar;R&period;id&period;nameTextView&rpar;&semi;&NewLine; phone&lowbar;no &equals; &lpar;TextView&rpar; itemView&period;findViewById&lpar;R&period;id&period;phoneTextView&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<p><strong>12<&sol;strong>&period; Now open <strong><span style&equals;"color&colon; &num;008000&semi;">MainActivity<&sol;span><&sol;strong> and write the below code&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">MainActivity&period;Java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre><code>package com&period;c1ctech&period;mycontacts&semi;&NewLine;&NewLine;import android&period;app&period;LoaderManager&semi;&NewLine;import android&period;content&period;ContentValues&semi;&NewLine;import android&period;content&period;CursorLoader&semi;&NewLine;import android&period;content&period;DialogInterface&semi;&NewLine;import android&period;content&period;Loader&semi;&NewLine;import android&period;database&period;Cursor&semi;&NewLine;import android&period;net&period;Uri&semi;&NewLine;import android&period;support&period;design&period;widget&period;FloatingActionButton&semi;&NewLine;import android&period;support&period;v7&period;app&period;AlertDialog&semi;&NewLine;import android&period;support&period;v7&period;app&period;AppCompatActivity&semi;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;support&period;v7&period;widget&period;DividerItemDecoration&semi;&NewLine;import android&period;support&period;v7&period;widget&period;LinearLayoutManager&semi;&NewLine;import android&period;support&period;v7&period;widget&period;RecyclerView&semi;&NewLine;import android&period;support&period;v7&period;widget&period;Toolbar&semi;&NewLine;import android&period;util&period;Log&semi;&NewLine;import android&period;view&period;LayoutInflater&semi;&NewLine;import android&period;view&period;Menu&semi;&NewLine;import android&period;view&period;MenuItem&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;import android&period;widget&period;EditText&semi;&NewLine;import android&period;widget&period;Toast&semi;&NewLine;&NewLine;import java&period;util&period;ArrayList&semi;&NewLine;import java&period;util&period;List&semi;&NewLine;&NewLine;public class MainActivity extends AppCompatActivity implements LoaderManager&period;LoaderCallbacks&lt&semi;Cursor&gt&semi; &lbrace;&NewLine;&NewLine; ContactsAdapter adapter&semi;&NewLine; RecyclerView rv&lowbar;list&semi;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine;&NewLine;&NewLine; rv&lowbar;list &equals; &lpar;RecyclerView&rpar; findViewById&lpar;R&period;id&period;contact&lowbar;list&rpar;&semi;&NewLine; rv&lowbar;list&period;addItemDecoration&lpar;new DividerItemDecoration&lpar;this&comma; DividerItemDecoration&period;VERTICAL&rpar;&rpar;&semi;&NewLine; rv&lowbar;list&period;setLayoutManager&lpar;new LinearLayoutManager&lpar;this&rpar;&rpar;&semi;&NewLine;&NewLine; Toolbar toolbar &equals; &lpar;Toolbar&rpar; findViewById&lpar;R&period;id&period;toolbar&rpar;&semi;&NewLine; setSupportActionBar&lpar;toolbar&rpar;&semi;&NewLine; getLoaderManager&lpar;&rpar;&period;initLoader&lpar;0&comma; null&comma; this&rpar;&semi;&NewLine; FloatingActionButton fab &equals; &lpar;FloatingActionButton&rpar; findViewById&lpar;R&period;id&period;fab&rpar;&semi;&NewLine; fab&period;setOnClickListener&lpar;new View&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onClick&lpar;View view&rpar; &lbrace;&NewLine;&NewLine; LayoutInflater li &equals; LayoutInflater&period;from&lpar;MainActivity&period;this&rpar;&semi;&NewLine; View getEmpIdView &equals; li&period;inflate&lpar;R&period;layout&period;dialog&lowbar;contact&lowbar;details&comma; null&rpar;&semi;&NewLine;&NewLine; AlertDialog&period;Builder alertDialogBuilder &equals; new AlertDialog&period;Builder&lpar;MainActivity&period;this&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; set dialog&lowbar;contact&lowbar;details&period;xmll to alertdialog builder<&sol;span><&sol;strong>&NewLine; alertDialogBuilder&period;setView&lpar;getEmpIdView&rpar;&semi;&NewLine;&NewLine; final EditText nameInput &equals; &lpar;EditText&rpar; getEmpIdView&period;findViewById&lpar;R&period;id&period;editTextDialogNameInput&rpar;&semi;&NewLine; final EditText phoneInput &equals; &lpar;EditText&rpar; getEmpIdView&period;findViewById&lpar;R&period;id&period;editTextDialogPhoneInput&rpar;&semi;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; set dialog message<&sol;span><&sol;strong>&NewLine;&NewLine; alertDialogBuilder&NewLine; &period;setCancelable&lpar;false&rpar;&NewLine; &period;setPositiveButton&lpar;"Add"&comma; new DialogInterface&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; public void onClick&lpar;DialogInterface dialog&comma; int id&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; get user input and set it to result&NewLine; &sol;&sol; edit text<&sol;span><&sol;strong>&NewLine; insertContact&lpar;nameInput&period;getText&lpar;&rpar;&period;toString&lpar;&rpar;&comma; phoneInput&period;getText&lpar;&rpar;&period;toString&lpar;&rpar;&rpar;&semi;&NewLine; restartLoader&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine; &rcub;&rpar;&period;create&lpar;&rpar;&NewLine; &period;show&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&NewLine; private void insertContact&lpar;String contactName&comma; String contactPhone&rpar; &lbrace;&NewLine; ContentValues values &equals; new ContentValues&lpar;&rpar;&semi;&NewLine; values&period;put&lpar;DBOpenHelper&period;CONTACT&lowbar;NAME&comma; contactName&rpar;&semi;&NewLine; values&period;put&lpar;DBOpenHelper&period;CONTACT&lowbar;PHONE&comma; contactPhone&rpar;&semi;&NewLine; Uri contactUri &equals; getContentResolver&lpar;&rpar;&period;insert&lpar;ContactsProvider&period;CONTENT&lowbar;URI&comma; values&rpar;&semi;&NewLine; Toast&period;makeText&lpar;this&comma; "Created Contact " &plus; contactName&comma; Toast&period;LENGTH&lowbar;LONG&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; private void deleteAllContacts&lpar;&rpar; &lbrace;&NewLine;&NewLine; getContentResolver&lpar;&rpar;&period;delete&lpar;ContactsProvider&period;CONTENT&lowbar;URI&comma; null&comma; null&rpar;&semi;&NewLine; restartLoader&lpar;&rpar;&semi;&NewLine; Toast&period;makeText&lpar;this&comma; "All Contacts Deleted"&comma; Toast&period;LENGTH&lowbar;LONG&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public boolean onCreateOptionsMenu&lpar;Menu menu&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; Inflate the menu&semi; this adds items to the action bar if it is present&period;<&sol;span><&sol;strong>&NewLine; getMenuInflater&lpar;&rpar;&period;inflate&lpar;R&period;menu&period;menu&lowbar;main&comma; menu&rpar;&semi;&NewLine; return true&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public boolean onOptionsItemSelected&lpar;MenuItem item&rpar; &lbrace;&NewLine; &NewLine; switch &lpar;item&period;getItemId&lpar;&rpar;&rpar; &lbrace;&NewLine; case R&period;id&period;deleteAllContacts&colon;&NewLine; deleteAllContacts&lpar;&rpar;&semi;&NewLine; return true&semi;&NewLine; &rcub;&NewLine;&NewLine; return super&period;onOptionsItemSelected&lpar;item&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; private void restartLoader&lpar;&rpar; &lbrace;&NewLine; getLoaderManager&lpar;&rpar;&period;restartLoader&lpar;0&comma; null&comma; this&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; public Loader&lt&semi;Cursor&gt&semi; onCreateLoader&lpar;int i&comma; Bundle bundle&rpar; &lbrace;&NewLine; return new CursorLoader&lpar;this&comma; ContactsProvider&period;CONTENT&lowbar;URI&comma; null&comma; null&comma; null&comma; null&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onLoadFinished&lpar;Loader&lt&semi;Cursor&gt&semi; loader&comma; Cursor cursor&rpar; &lbrace;&NewLine;&NewLine; List&lt&semi;Contact&gt&semi; list &equals; new ArrayList&lt&semi;&gt&semi;&lpar;&rpar;&semi;&NewLine;&NewLine; while &lpar;cursor&period;moveToNext&lpar;&rpar;&rpar; &lbrace;&NewLine; int index1 &equals; cursor&period;getColumnIndex&lpar;DBOpenHelper&period;CONTACT&lowbar;NAME&rpar;&semi;&NewLine; int index2 &equals; cursor&period;getColumnIndex&lpar;DBOpenHelper&period;CONTACT&lowbar;PHONE&rpar;&semi;&NewLine; String name &equals; cursor&period;getString&lpar;index1&rpar;&semi;&NewLine; String phone&lowbar;no &equals; cursor&period;getString&lpar;index2&rpar;&semi;&NewLine;&NewLine; Contact contact &equals; new Contact&lpar;name&comma; phone&lowbar;no&rpar;&semi;&NewLine; list&period;add&lpar;contact&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; adapter &equals; new ContactsAdapter&lpar;this&comma; list&rpar;&semi;&NewLine; rv&lowbar;list&period;setAdapter&lpar;adapter&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onLoaderReset&lpar;Loader&lt&semi;Cursor&gt&semi; loader&rpar; &lbrace;&NewLine;&NewLine; &rcub;&NewLine;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<p>In the <span style&equals;"color&colon; &num;008000&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span> we are implementing the <span style&equals;"color&colon; &num;008000&semi;"><strong>LoaderManager&period;LoaderCallbacks<&sol;strong><&sol;span> interface to work with loader&period;<&sol;p>&NewLine;<p>In the <strong><span style&equals;"color&colon; &num;008000&semi;"><em>onCreate&lpar;&rpar;<&sol;em><&sol;span><&sol;strong> method on clicking the <span style&equals;"color&colon; &num;008000&semi;"><strong>FloatingActionButton<&sol;strong><&sol;span> we are invoking an <span style&equals;"color&colon; &num;008000&semi;"><strong>AlertDialog<&sol;strong><&sol;span> to input the Contact’s Name and Phone number&period;<&sol;p>&NewLine;<p>On clicking the <span style&equals;"color&colon; &num;008000&semi;"><strong>Add<&sol;strong><&sol;span> button of the Dialog&comma; <strong><span style&equals;"color&colon; &num;008000&semi;"><em>insertContact&lpar;&rpar;<&sol;em><&sol;span><&sol;strong>  method is called&comma; which uses the <span style&equals;"color&colon; &num;008000&semi;"><strong><em>insert&lpar;&rpar;<&sol;em><&sol;strong><&sol;span> method of our custom Android Content Provider to insert a new record into the database&period;<&sol;p>&NewLine;<p>Similarly&comma; we have an option in the Menu to delete all the existing contacts by calling the <strong><span style&equals;"color&colon; &num;008000&semi;"><em>delete&lpar;&rpar;<&sol;em><&sol;span><&sol;strong> method of our custom Android Content Provider &period;<&sol;p>&NewLine;<p>Next&comma; we have implemented the methods for the <span style&equals;"color&colon; &num;008000&semi;"><strong>LoaderManager&period;LoaderCallbacks<&sol;strong><&sol;span> interface<&sol;p>&NewLine;<ol>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;"><em>restartLoader&lpar;int id&comma; Bundle args&comma; LoaderCallbacks callback&rpar;<&sol;em> <&sol;span><&sol;strong>&colon; It restarts an existing Loader with the particular id&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;"><em>onCreateLoader&lpar;int id&comma; Bundle args&rpar;<&sol;em><&sol;span><&sol;strong> &colon; whenever we initialize loader using the <strong><span style&equals;"color&colon; &num;008000&semi;">initLoader<&sol;span><&sol;strong> like this&colon;&NewLine;<pre><strong><span style&equals;"color&colon; &num;008000&semi;">getLoaderManager&lpar;&rpar;&period;initLoader&lpar;0&comma; null&comma; this&rpar;&semi;<&sol;span><&sol;strong><&sol;pre>&NewLine;<p>This methods get automatically triggered &comma;runs in a separate thread&comma;loads the data from the provider and return a new Loader for the given id &period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;"><em>onLoadFinished&lpar;Loader loader&comma; Cursor cursor&rpar;<&sol;em><&sol;span><&sol;strong> &colon; This method is Called when a previously created loader has finished its load&period;It runs in main thread &period;&commat;param <strong><span style&equals;"color&colon; &num;008000&semi;">loader<&sol;span><&sol;strong> indicates the Loader that has finished and &commat;param <strong><span style&equals;"color&colon; &num;008000&semi;">cursor<&sol;span><&sol;strong> indicates the data generated by the Loader&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;"><em>onLoaderReset&lpar;Loader loader&rpar;<&sol;em><&sol;span><&sol;strong> &colon; This method is called when a previously created loader is being reset&comma; and thus making its data unavailable&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p>Now run the app and add few contacts&period; It should create a list sorted by the Name in the alphabetical order like this&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-816 " src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;09&sol;Screenshot&lowbar;1537442057-576x1024&period;png" alt&equals;"" width&equals;"349" height&equals;"621" &sol;><&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-817 " src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;09&sol;Screenshot&lowbar;1537442321-576x1024&period;png" alt&equals;"" width&equals;"344" height&equals;"611" &sol;><&sol;p>&NewLine;<h4><&sol;h4>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating the ContactList App&lpar;consumer app&rpar;   <&sol;strong><&sol;span><&sol;h3>&NewLine;<p><strong>13<&sol;strong>&period; Now we will create another app <span style&equals;"color&colon; &num;008000&semi;"><strong>ContactList<&sol;strong><&sol;span> in the same way we have created the <strong><span style&equals;"color&colon; &num;008000&semi;">MyContacts<&sol;span><&sol;strong> App earliar&period;<&sol;p>&NewLine;<p>To retrieve data from a provider&comma; follow these basic steps&colon;<&sol;p>&NewLine;<ol>&NewLine;<li>Request the read access permission for the provider&period;<&sol;li>&NewLine;<li>Define the code that sends a query to the provider&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Request the read access permission for the provider<&sol;strong><&sol;span><&sol;p>&NewLine;<p>Add the following permissions to use the ContactsProvider in the <span style&equals;"color&colon; &num;008000&semi;"><strong>AndroidManifest&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre><code>&lt&semi;uses-permission android&colon;name&equals;"com&period;c1ctech&period;mycontacts&period;READ&lowbar;DATABASE" &sol;&gt&semi;&NewLine;&lt&semi;uses-permission android&colon;name&equals;"com&period;c1ctech&period;mycontacts&period;WRITE&lowbar;DATABASE" &sol;&gt&semi;<&sol;code><&sol;pre>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Define the code that sends a query to the provider<&sol;strong><&sol;span><&sol;p>&NewLine;<pre><code>private static final String AUTHORITY &equals; "com&period;c1ctech&period;mycontacts"&semi;&NewLine;private static final String BASE&lowbar;PATH &equals; "contacts"&semi;&NewLine;public static final Uri CONTENT&lowbar;URI &equals; Uri&period;parse&lpar;"content&colon;&sol;&sol;" &plus; AUTHORITY &plus; "&sol;" &plus; BASE&lowbar;PATH&rpar;&semi;&NewLine;&NewLine;&commat;Override&NewLine;public Loader&lt&semi;Cursor&gt&semi; onCreateLoader&lpar;int id&comma; Bundle bundle&rpar; &lbrace;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;loads data from the content provider<&sol;strong><&sol;span>&NewLine; if &lpar;id &equals;&equals; 0&rpar; &lbrace;&NewLine; return new CursorLoader&lpar;this&comma; CONTENT&lowbar;URI&comma; null&comma; null&comma; null&comma; null&rpar;&semi;&NewLine; &rcub;&NewLine; return null&semi;&NewLine;&NewLine;&rcub;&NewLine;&NewLine;&commat;Override&NewLine;public void onLoadFinished&lpar;Loader&lt&semi;Cursor&gt&semi; loader&comma; Cursor cursor&rpar; &lbrace;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Getting data from MyContacts App as cursor object<&sol;span><&sol;strong>&NewLine; List&lt&semi;Contact&gt&semi; list &equals; new ArrayList&lt&semi;&gt&semi;&lpar;&rpar;&semi;&NewLine;&sol;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;Adding cursor data in a list<&sol;span><&sol;strong>&NewLine; while &lpar;cursor&period;moveToNext&lpar;&rpar;&rpar; &lbrace;&NewLine;&NewLine; String name &equals; cursor&period;getString&lpar;1&rpar;&semi;&NewLine; String phone&lowbar;no &equals; cursor&period;getString&lpar;2&rpar;&semi;&NewLine;&NewLine; Contact contact &equals; new Contact&lpar;name&comma; phone&lowbar;no&rpar;&semi;&NewLine; list&period;add&lpar;contact&rpar;&semi;&NewLine; &rcub;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;passing list to adapter which will display the list items in recyclerView<&sol;span><&sol;strong>&NewLine; adapter &equals; new ContactsAdapter&lpar;this&comma; list&rpar;&semi;&NewLine; rv&lowbar;list&period;setAdapter&lpar;adapter&rpar;&semi;&NewLine;&NewLine;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<p><strong>14<&sol;strong>&period; Open activity&lowbar;main&period;xml and write the below code&period;It is similar to the layout of <strong><span style&equals;"color&colon; &num;008000&semi;">MyContacts<&sol;span><&sol;strong> except in place of FloatingAction Button with property <strong><span style&equals;"color&colon; &num;008000&semi;">app&colon;srcCompat&equals;”&commat;android&colon;drawable&sol;ic&lowbar;input&lowbar;add”<&sol;span><&sol;strong> we use <strong><span style&equals;"color&colon; &num;008000&semi;">app&colon;srcCompat&equals;”&commat;android&colon;drawable&sol;ic&lowbar;popup&lowbar;sync”&period;<&sol;span><&sol;strong><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre><code>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;android&period;support&period;design&period;widget&period;CoordinatorLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; tools&colon;context&equals;"&period;MainActivity"&gt&semi;&NewLine;&NewLine; &lt&semi;android&period;support&period;design&period;widget&period;AppBarLayout&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;theme&equals;"&commat;style&sol;AppTheme&period;AppBarOverlay"&gt&semi;&NewLine;&NewLine; &lt&semi;android&period;support&period;v7&period;widget&period;Toolbar&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;toolbar"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"&quest;attr&sol;actionBarSize"&NewLine; android&colon;background&equals;"&quest;attr&sol;colorPrimary"&NewLine; app&colon;popupTheme&equals;"&commat;style&sol;AppTheme&period;PopupOverlay" &sol;&gt&semi;&NewLine;&NewLine;&NewLine; &lt&semi;&sol;android&period;support&period;design&period;widget&period;AppBarLayout&gt&semi;&NewLine;&NewLine;&NewLine; &lt&semi;RelativeLayout&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&gt&semi;&NewLine;&NewLine; &lt&semi;android&period;support&period;v7&period;widget&period;RecyclerView&NewLine;&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;contact&lowbar;list"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;marginTop&equals;"70dp"&NewLine;&NewLine; &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;&sol;RelativeLayout&gt&semi;&NewLine;&NewLine; &lt&semi;android&period;support&period;design&period;widget&period;FloatingActionButton&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;fab"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;gravity&equals;"bottom&vert;end"&NewLine; android&colon;layout&lowbar;margin&equals;"16dp"&NewLine; app&colon;srcCompat&equals;"&commat;android&colon;drawable&sol;ic&lowbar;popup&lowbar;sync" &sol;&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;android&period;support&period;design&period;widget&period;CoordinatorLayout&gt&semi;<&sol;code><&sol;pre>&NewLine;<p><strong>15<&sol;strong>&period; Open <strong><span style&equals;"color&colon; &num;008000&semi;">MainActivity&period;Java<&sol;span><&sol;strong> and write following code&period;The code for <span style&equals;"color&colon; &num;008000&semi;"><strong>MainActivity<&sol;strong><&sol;span> is pretty much same as the <span style&equals;"color&colon; &num;008000&semi;"><strong>MyContacts<&sol;strong><&sol;span> app&comma; We have just removed the code for inserting and deleting the <span style&equals;"color&colon; &num;008000&semi;"><strong>Contacts<&sol;strong><&sol;span> and we are just displaying those records&period;<br &sol;>&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;">MainActivity&period;Java <&sol;span><&sol;strong><&sol;p>&NewLine;<pre><code>package com&period;c1ctech&period;contactlist&semi;&NewLine;&NewLine;import android&period;app&period;LoaderManager&semi;&NewLine;import android&period;content&period;CursorLoader&semi;&NewLine;import android&period;content&period;Loader&semi;&NewLine;import android&period;content&period;UriMatcher&semi;&NewLine;import android&period;database&period;Cursor&semi;&NewLine;import android&period;net&period;Uri&semi;&NewLine;import android&period;support&period;design&period;widget&period;FloatingActionButton&semi;&NewLine;import android&period;support&period;v7&period;app&period;AppCompatActivity&semi;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;support&period;v7&period;widget&period;DividerItemDecoration&semi;&NewLine;import android&period;support&period;v7&period;widget&period;LinearLayoutManager&semi;&NewLine;import android&period;support&period;v7&period;widget&period;RecyclerView&semi;&NewLine;import android&period;support&period;v7&period;widget&period;Toolbar&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;&NewLine;&NewLine;import java&period;util&period;ArrayList&semi;&NewLine;import java&period;util&period;List&semi;&NewLine;&NewLine;public class MainActivity extends AppCompatActivity implements LoaderManager&period;LoaderCallbacks&lt&semi;Cursor&gt&semi;&comma; View&period;OnClickListener &lbrace;&NewLine;&NewLine; ContactsAdapter adapter&semi;&NewLine; RecyclerView rv&lowbar;list&semi;&NewLine; FloatingActionButton fab&semi;&NewLine;&NewLine; private static final String AUTHORITY &equals; "com&period;c1ctech&period;mycontacts"&semi;&NewLine; private static final String BASE&lowbar;PATH &equals; "contacts"&semi;&NewLine; public static final Uri CONTENT&lowbar;URI &equals; Uri&period;parse&lpar;"content&colon;&sol;&sol;" &plus; AUTHORITY &plus; "&sol;" &plus; BASE&lowbar;PATH&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; Constant to identify the requested operation<&sol;span><&sol;strong>&NewLine; private static final int CONTACTS &equals; 1&semi;&NewLine; private static final int CONTACT&lowbar;ID &equals; 2&semi;&NewLine;&NewLine; private boolean firstTimeLoaded &equals; false&semi;&NewLine;&NewLine;&NewLine; private static final UriMatcher uriMatcher &equals; new UriMatcher&lpar;UriMatcher&period;NO&lowbar;MATCH&rpar;&semi;&NewLine;&NewLine; static &lbrace;&NewLine; uriMatcher&period;addURI&lpar;AUTHORITY&comma; BASE&lowbar;PATH&comma; CONTACTS&rpar;&semi;&NewLine; uriMatcher&period;addURI&lpar;AUTHORITY&comma; BASE&lowbar;PATH &plus; "&sol;&num;"&comma; CONTACT&lowbar;ID&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine;&NewLine; rv&lowbar;list &equals; &lpar;RecyclerView&rpar; findViewById&lpar;R&period;id&period;contact&lowbar;list&rpar;&semi;&NewLine;&NewLine; rv&lowbar;list&period;setLayoutManager&lpar;new LinearLayoutManager&lpar;this&rpar;&rpar;&semi;&NewLine; rv&lowbar;list&period;addItemDecoration&lpar;new DividerItemDecoration&lpar;this&comma; DividerItemDecoration&period;VERTICAL&rpar;&rpar;&semi;&NewLine; Toolbar toolbar &equals; &lpar;Toolbar&rpar; findViewById&lpar;R&period;id&period;toolbar&rpar;&semi;&NewLine; setSupportActionBar&lpar;toolbar&rpar;&semi;&NewLine;&NewLine; fab &equals; &lpar;FloatingActionButton&rpar; findViewById&lpar;R&period;id&period;fab&rpar;&semi;&NewLine; fab&period;setOnClickListener&lpar;this&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; public Loader&lt&semi;Cursor&gt&semi; onCreateLoader&lpar;int id&comma; Bundle bundle&rpar; &lbrace;&NewLine; if &lpar;id &equals;&equals; 0&rpar; &lbrace;&NewLine; return new CursorLoader&lpar;this&comma; CONTENT&lowbar;URI&comma; null&comma; null&comma; null&comma; null&rpar;&semi;&NewLine; &rcub;&NewLine; return null&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onLoadFinished&lpar;Loader&lt&semi;Cursor&gt&semi; loader&comma; Cursor cursor&rpar; &lbrace;&NewLine;&NewLine; List&lt&semi;Contact&gt&semi; list &equals; new ArrayList&lt&semi;&gt&semi;&lpar;&rpar;&semi;&NewLine;&NewLine; while &lpar;cursor&period;moveToNext&lpar;&rpar;&rpar; &lbrace;&NewLine;&NewLine; String name &equals; cursor&period;getString&lpar;1&rpar;&semi;&NewLine; String phone&lowbar;no &equals; cursor&period;getString&lpar;2&rpar;&semi;&NewLine;&NewLine; Contact contact &equals; new Contact&lpar;name&comma; phone&lowbar;no&rpar;&semi;&NewLine; list&period;add&lpar;contact&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; adapter &equals; new ContactsAdapter&lpar;this&comma; list&rpar;&semi;&NewLine; rv&lowbar;list&period;setAdapter&lpar;adapter&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onLoaderReset&lpar;Loader&lt&semi;Cursor&gt&semi; loader&rpar; &lbrace;&NewLine; &NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onClick&lpar;View view&rpar; &lbrace;&NewLine; switch &lpar;view&period;getId&lpar;&rpar;&rpar; &lbrace;&NewLine; case R&period;id&period;fab&colon;&NewLine; if &lpar;firstTimeLoaded &equals;&equals; false&rpar; &lbrace;&NewLine; getLoaderManager&lpar;&rpar;&period;initLoader&lpar;0&comma; null&comma; this&rpar;&semi;&NewLine; firstTimeLoaded &equals; true&semi;&NewLine; &rcub; else &lbrace;&NewLine; getLoaderManager&lpar;&rpar;&period;restartLoader&lpar;0&comma; null&comma; this&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; break&semi;&NewLine; default&colon;&NewLine; break&semi;&NewLine; &rcub;&NewLine;&NewLine; &rcub;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<p>When you run <span style&equals;"color&colon; &num;008000&semi;"><strong>ContactList App<&sol;strong><&sol;span> &comma;you find refresh button &comma;when you click on it you get all the contacts stored in <strong><span style&equals;"color&colon; &num;008000&semi;">MyContacts App <&sol;span><&sol;strong><span style&equals;"color&colon; &num;000000&semi;">like this&colon;<&sol;span><&sol;p>&NewLine;<p><img class&equals;"aligncenter wp-image-815 " src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;09&sol;Screenshot&lowbar;1537442085-576x1024&period;png" alt&equals;"" width&equals;"419" height&equals;"745" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>I hope this tutorial will help you in understanding how to work with <strong><span style&equals;"color&colon; &num;000080&semi;">contentProvider<&sol;span><&sol;strong> in sharing data from one application to another&period;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;&NewLine;

Exit mobile version