Android show bookmarks and history of browser

In this Android tip, I am going to show you how to access bookmarks and history data of browser. Android stores these data in the Browser.BOOKMARKS_URI table. From this table, you can read title, urls, icons, and so on. The column names of the table are defined in the Browser.BookmarkColumns and can be accessed statically. For example, to refer to the column title of the table, you will write Browser.BookmarkColumns.TITLE.


You can read all the bookmarks and history data from the table by using the query method of the ContentResolver instance. The ContentResolver instance can be obtained by calling the getContentResolver method from your activity. The query method returns a Cursor object from which you can process all rows of the table.

Now, to have a workable application to display the bookmarks and history of the browser, you will create a new Android project. Then modify the activity_main.xml file to add a ListView widget as shown below.

<RelativeLayout 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: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" >

   <ListView
  
     android:id="@+id/hb_list"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
  
   </ListView>

</RelativeLayout>


The ListView in this example displays icons, titles, and urls of bookmarks and history. So you need to define a layout file (bh_list_item.xml) that contains three widgets: one ImageView, and two TextViews. The layout file is used as a design template for each row of the ListView.

<?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="horizontal"
   android:padding="5dip" >
<ImageView
   android:id="@+id/icon"
   android:layout_width="30dp"
   android:layout_height="30dp"
   android:padding="5sp"
   android:contentDescription="icon_image"
/>
<LinearLayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="vertical"
   >

<TextView
     android:id="@+id/hbtitle"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:padding="1sp"
     android:textSize="20sp"
     android:scrollHorizontally="true"
   android:singleLine="true"
     android:textStyle="bold" >
</TextView>
<TextView
     android:id="@+id/hburl"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:padding="1sp"
     android:textSize="15sp"
     android:scrollHorizontally="true"
   android:singleLine="true"
     android:textColor="#0000ff">
</TextView>
</LinearLayout>

</LinearLayout>


You also have to create the BHAdapter class that extends the ArrayAdapter class. The instance of the BHAdapter class will be used to supply the bookmarks and history data to the ListView. Here is the content of the BHAdapter class.

  package com.example.andtip;

import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class BHAdapter extends ArrayAdapter<String>{
int groupid;
ArrayList<String> titles;
ArrayList<String> urls;
ArrayList<Bitmap> bitmaps;
Context context;
String path;
public BHAdapter(Context context, int vg, int id, ArrayList<String> titles,ArrayList<String> urls,ArrayList<Bitmap> bitmaps){
super(context,vg, id, titles);
this.context=context;
groupid=vg;
this.titles=titles;
this.urls=urls;
this.bitmaps=bitmaps;

}
public View getView(int position, View convertView, ViewGroup parent) {

     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

     View itemView = inflater.inflate(groupid, parent, false);
     ImageView imageView = (ImageView) itemView.findViewById(R.id.icon);
     imageView.setImageBitmap(bitmaps.get(position));
     TextView textTitle= (TextView) itemView.findViewById(R.id.hbtitle);
     String title=titles.get(position);
     textTitle.setText(title);
     TextView textURL = (TextView) itemView.findViewById(R.id.hburl);
     String url=urls.get(position);
     textURL.setText(url);
     //make the url clickable
     Linkify.addLinks(textURL, Linkify.ALL);
     return itemView;
}


}


In the MainActivty class, the code will be written to read the bookmarks and history data from the BOOKMARKS_URI table and display the data in the ListView.

  package com.example.andtip;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.Browser;
import android.view.Menu;
import android.widget.ListView;
import android.widget.Toast;


public class MainActivity extends Activity {

   Activity context;
   private ArrayList<String> titles;
   private ArrayList<String> urls;
   private ArrayList<Bitmap> icons;
   private ContentResolver cr;
   protected void onCreate(Bundle savedInstanceState) {
   //TODO Auto-generated method stub
   super.onCreate(savedInstanceState);
   context=this;
   setContentView(R.layout.activity_main);
   //create ArrayList objects to store titles and urls
     titles=new ArrayList<String>();
   urls=new ArrayList<String>();
   icons=new ArrayList<Bitmap>();
  

}

  

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.main, menu);
       return true;
   }
  
   public void onStart(){
   super.onStart();
   Bitmap icon;
   cr=getContentResolver();
   //specify field to sort the records
   String order=Browser.BookmarkColumns.DATE+" DESC";
   //specify columns to read the data
   String[] projection={Browser.BookmarkColumns.TITLE,Browser.BookmarkColumns.URL, Browser.BookmarkColumns.FAVICON};
   //read all rows from table of bookmarks and history
   Cursor rows=cr.query(Browser.BOOKMARKS_URI,projection, null,null,order);
   if(rows.getCount()>0){
      while(rows.moveToNext()) {
      //read title
      String title=rows.getString(rows.getColumnIndex(projection[0]));
      //read url
      String url=rows.getString(rows.getColumnIndex(projection[1]));
      //read icon
      byte[] bicon=rows.getBlob(rows.getColumnIndex(projection[2]));
      if(bicon!=null){
      //convert blob image data to Bitmap
      icon=BitmapFactory.decodeByteArray(bicon,0,bicon.length);
     
      }
      else{
      //default icon for history and bookmarks that do not icons
        icon=BitmapFactory.decodeResource(getResources(),R.drawable.noicon);
      }
     
      //title, url, and icon to lists
      titles.add(title);
      urls.add(url);
      icons.add(icon);
      }
     
      //show bookmarks and history
      showBookmarksHistory();
   }
   else{
   Toast.makeText(context,"Not bookmarks and history", Toast.LENGTH_LONG).show();
   }
  
   }
  
   public void showBookmarksHistory(){
   ListView bhlist=(ListView) findViewById(R.id.hb_list);
   if(bhlist!=null){
   if(titles.size()>0){
   BHAdapter aa=new BHAdapter(this,R.layout.bh_list_item,R.id.hbtitle,titles,urls,icons);
   bhlist.setAdapter(aa);
   }
   }
   }

}


Before running the project, you need to add a permission to read the bookmarks and history data in the AndroidManifest.xml file as below.

<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />

Android-show-bookmarks-and-history-of-browser

Posted by: Dara | post date: 06-08-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.