Home Android Tutorials Browser Bookmark Example in Android

Browser Bookmark Example in Android

0
7194

This example shows how you can list browser bookmarks into your application.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it BrowserBookmarkExample.

2.) Add following permission into your manifest file:

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

3.) Run for output.

Steps:

1.) Create a project named BrowserBookmarkExample and set the information as stated in the image.

Build Target: Android 4.0
Application Name: BrowserBookmarkExample
Package Name: com. example. BrowserBookmarkExample
Activity Name: BrowserBookmarkExample
Min SDK Version: 8

2.) Open BrowserBookmarkExample.java file and write following code there:

package com.example.browserbookmarkexample;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Browser;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class BrowserBookmarkExample extends ListActivity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		String[] selection = { Browser.BookmarkColumns._ID,
				Browser.BookmarkColumns.URL, Browser.BookmarkColumns.TITLE };
		String[] displayFields = { Browser.BookmarkColumns.URL,
				Browser.BookmarkColumns.TITLE };
		int[] viewFields = { android.R.id.text1, android.R.id.text2 };
		Cursor cursor = managedQuery(Browser.BOOKMARKS_URI, selection, null,
				null, null);
		startManagingCursor(cursor);
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
				android.R.layout.simple_list_item_2, cursor, displayFields,
				viewFields);
		setListAdapter(adapter);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		TextView view = (TextView) v.findViewById(android.R.id.text2);
		Toast.makeText(this, view.getText(), Toast.LENGTH_SHORT).show();
	}
}

3.) Compile and build the project.

Output

Become An Insider!

Discover Latest News, Tech Updates & Exciting offers!
JOIN NOW
close-link