In Android you can access your Browser history and Bookmarks. For more details about Android Browser check developers site.
In this tutorial we are going to fetch all bookmarks and browser history and show them over ListView.
For fetching Browser data we are going to use Browser.BookmarkColumns. This contains all the columns against Bookmark and History of browser.
Let’s start with example.
1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is MainActivity.java.
2. To work on Android History and Bookmarks we need a uses permission in Manifest file. This permission in mandatory.
1 |
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" /> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.bookmark_demo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="22" /> <!-- Permission required to work on Bookmarks --> <uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.bookmark_demo.MainActivity" 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> |
3. Now, create a xml layout contains ListView for displaying bookmarks. I named this layout as activity_main.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff" > <!-- List View to display bookmark --> <ListView android:id="@+id/bookmark_list" android:layout_width="fill_parent" android:layout_height="fill_parent" > </ListView> </LinearLayout> |
4. I am using a custom listview here , so i need a new xml layout i.e. custom_view.xml for custom view. If you are new to Custom ListView you can check its tutorial here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="5dp" > <!-- Bookmark Title --> <TextView android:id="@+id/bookmar_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:paddingLeft="5dp" android:paddingRight="5dp" android:paddingTop="5dp" android:textColor="#000000" android:textSize="17sp" /> <!-- Bookmark Url --> <TextView android:id="@+id/bookmark_url" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:padding="5dp" android:textColor="#dd44dd" android:textSize="17sp" /> </LinearLayout> |
5. Create a getter and setter java class naming BookmarkModel.java for Bookmark Listview.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.bookmark_demo; public class BookmarkModel { // Bookmark getter and setter private String bookmark_title, bookmark_url; public BookmarkModel(String bookmark_title, String bookmark_url) { this.bookmark_title = bookmark_title; this.bookmark_url = bookmark_url; } public String getBookmarkTitle() { return bookmark_title; } public String getBookmarkUrl() { return bookmark_url; } } |
6. Now, create a custom adapter naming Custom_Adapter.java. In this class we inflate a custom layout and set the bookmark title and Url over textview.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
package com.bookmark_demo; import java.util.ArrayList; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class Bookmark_Adapter extends BaseAdapter { private ArrayList<BookmarkModel> arrayList; private Context context; public Bookmark_Adapter(Context context, ArrayList<BookmarkModel> arrayList) { this.context = context; this.arrayList = arrayList; } @Override public int getCount() { return arrayList.size(); } @Override public BookmarkModel getItem(int position) { return arrayList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View view, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (view == null) { view = inflater.inflate(R.layout.custom_view, parent, false); TextView title = (TextView) view.findViewById(R.id.bookmar_title); TextView url = (TextView) view.findViewById(R.id.bookmark_url); title.setText(arrayList.get(position).getBookmarkTitle()); url.setText(arrayList.get(position).getBookmarkUrl()); } return view; } } |
7. We are going to use Cursor and Content Resolver to fetch Bookmarks details. And add the details into arrayList.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// Cursor to get Bookmar information Cursor cursor = getContentResolver().query( Browser.BOOKMARKS_URI, new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL }, null, null, null); // Note : " Browser.BookmarkColumns.BOOKMARK " - this will return 0 or // 1. '1' indicates a bookmark and '0' indicates history item. try { // Now loop to all items using cursor if (cursor != null && cursor.moveToFirst()) { do { // Add Bookmark title and Bookmark Url arrayList.add(new BookmarkModel(cursor.getString(0), cursor .getString(1))); } while (cursor.moveToNext()); // Move to next } } finally { // Close the cursor after use cursor.close(); } |
8. Finally come to MainActivity.java and add the following code. In this class we fetch the bookmark history and add the Bookmark Title and Bookmark URL to arrayList and set over listview.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
package com.bookmark_demo; import java.util.ArrayList; import android.database.Cursor; import android.os.Bundle; import android.provider.Browser; import android.support.v7.app.AppCompatActivity; import android.widget.ListView; public class MainActivity extends AppCompatActivity { private static ListView listView; private static ArrayList<BookmarkModel> arrayList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { arrayList = new ArrayList<BookmarkModel>(); listView = (ListView) findViewById(R.id.bookmark_list); // Cursor to get Bookmar information Cursor cursor = getContentResolver().query( Browser.BOOKMARKS_URI, new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL }, null, null, null); // Note : " Browser.BookmarkColumns.BOOKMARK " - this will return 0 or // 1. '1' indicates a bookmark and '0' indicates history item. try { // Now loop to all items using cursor if (cursor != null && cursor.moveToFirst()) { do { // Add Bookmark title and Bookmark Url arrayList.add(new BookmarkModel(cursor.getString(0), cursor .getString(1))); } while (cursor.moveToNext()); // Move to next } } finally { // Close the cursor after use cursor.close(); } Bookmark_Adapter adapter = new Bookmark_Adapter(MainActivity.this, arrayList); listView.setAdapter(adapter);// Set adapter } } |
9. Now, you are all done, run your app and you will get the output as shown in video.
Thanks.
Subscribe to us and get the latest news.
17 Comments
Raja Usman
Sunday, September 30th, 2018i can’t resolve this problem of (BOOKMARKS_URI and BookmarkColumns.URL) can you help me
Cursor cursor = getContentResolver().query(
Browser.BOOKMARKS_URI,
new String[] { Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.URL }, null, null, null);
Dr. Droid
Monday, October 8th, 2018Hi Raja,
Actually the code was written long back on Eclipse so may be that’s why its creating issue.
You can check this link : https://stackoverflow.com/questions/32535325/browser-bookmarks-uri-not-working-in-android-studio
May be it will help you.
Thanks
samid
Tuesday, January 22nd, 2019how to get bookmarks history to firebase??
Dr. Droid
Wednesday, January 23rd, 2019Hi Samir,
What exactly you want with firebase? Can you elaborate it?
Thanks
samid
Monday, February 18th, 2019how to save data bookmark to firebase??
Dr. Droid
Monday, February 18th, 2019Hi Samid,
From this article you got to know how to fetch bookmark and about how to save data in firebase see this link.
Thanks
stevemaster92
Thursday, February 21st, 2019Is this still possible? According to API doc https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-bookmark-browser this shouldn’t.
Is there another solution for Android 6 and later?
Dr. Droid
Thursday, February 21st, 2019Hi Steve,
Yes i think this history bookmark wont work for API level 6+.
Thanks
samid
Monday, February 25th, 2019hallo sir,
I completed this task, can this task be connected to Firebase?
Dr. Droid
Tuesday, February 26th, 2019Hi Samid,
In last comment reply i shared you the link of Firebase Realtime Database link. Have you gone through it?
If not then first go through it to check how to implement it and in case you face any issue let me know.
Thanks
samid
Friday, March 15th, 2019halo sir.
this app can running in background??
Dr. Droid
Sunday, March 17th, 2019Hi Samid,
I didn’t get you what you are trying to say?
Thanks
samid
Sunday, March 17th, 2019halo sir,
I completed this task,
how to run this application in the background and add bookmarks in real time without having to open the application again?
Dr. Droid
Tuesday, March 19th, 2019Hi Samid,
You have to make a service which will be running in background.
Thanks
samid
Monday, April 29th, 2019How to make service which will be running in background??
Dr. Droid
Tuesday, April 30th, 2019Hi Samid,
Check this link. You will get an idea how to run a service in background.
Thanks
Ajay
Monday, April 20th, 2020sir how i add only history button in webview brower
please sir help me