-2

I have created a simple browser application with a edittext , go button and history button. Please suggest what coding do i need to implement for history button such that when i press it my browsing history gets displayed on the hi.xml layout. Please suggest what coding changes do i need to make in my xml file as well as java file .

MainActivity.java:

package com.example.history;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends Activity implements OnClickListener{

EditText t;
Button g,h;
WebView w;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    g=(Button) findViewById(R.id.button1);
    h=(Button) findViewById(R.id.button2);
    t=(EditText) findViewById(R.id.editText1);
    w=(WebView) findViewById(R.id.webView1);
    h.setOnClickListener(this);
    g.setOnClickListener(this);
    w.getSettings().setJavaScriptEnabled(true);
    w.getSettings().setLoadWithOverviewMode(true);
    w.getSettings().setUseWideViewPort(true);
    w.setWebViewClient(new Callback());


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch(arg0.getId())
    {
    case R.id.button1:
        String c;
        c=t.getText().toString();
        String theWebsite=("http://").concat(c);
        t.setText(theWebsite);
        w.loadUrl(theWebsite);
        InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(w.getWindowToken(),0);


        break;
    case R.id.button2:
        Intent a=new Intent(MainActivity.this,h.class);
        startActivity(a);

        break;



    }


}

}
class Callback extends WebViewClient{   

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    return (false);
}

}

activity_main.xml:

<LinearLayout 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:orientation="vertical"
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" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="16dp"
    android:ems="10" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="34dp"
    android:layout_toRightOf="@+id/textView1"
    android:text="Go" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_centerVertical="true"
    android:text="History" />

<WebView
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

h.java:

package com.example.history;

import android.app.Activity;
import android.os.Bundle;

public class h extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.hi);
}

}

hi.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

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

</LinearLayout>
Kunal
  • 82
  • 3
  • 11
  • It looks like you posted a new Android project. You should delete all the stock wizard code and show us the code you wrote and what you have tried. I wish I could vote twice to close this question... [Google search: android browser history site:stackoverflow.com](https://www.google.com/search?q=android+browser+history+site:stackoverflow.com). – jww May 01 '14 at 07:52

2 Answers2

1

When you open a URL using the WebView it gets stored in the phone's web history.

You can access it by using the below code.Later add it to the listview you use in hi.xml

try{

   Cursor mCur = null;
    try{
     String sortOrder = Browser.BookmarkColumns.DATE + " ASC";
     mCur= context.getContentResolver().query(Browser.BOOKMARKS_URI, Browser.HISTORY_PROJECTION, null, null, sortOrder);

     mCur.moveToFirst();
     if (mCur.moveToFirst() && mCur.getCount() > 0) {
      while (mCur.isAfterLast() == false) {   
            String title = mCur.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX);
            String url = mCur.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
            long date = mCur.getLong(Browser.HISTORY_PROJECTION_DATE_INDEX);  
            mCur.moveToNext();
      }
         }else{
         mCur.close();
        }

    }catch(Exception e){

    }finally{
     mCur.close();
    }

  }catch(Exception e){

  }

Also you need to give the below permission in your manifest file. Hope this helps.

<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
Kris
  • 133
  • 5
  • thanks for code can u say me two things firstly what is context in the above code and how do i link it to my listview in hi.xml. – Kunal May 01 '14 at 09:06
  • context is the application context, you can use getApplicationContext() method or your ClassName.class (for ex: Test.class). I will give you a code sample in the next comment – Kris May 01 '14 at 09:38
  • Give me your email id will send you the sample java class. – Kris May 01 '14 at 09:45
  • u can mail me in this id – Kunal May 01 '14 at 09:56
  • Just sent you an email – Kris May 01 '14 at 09:57
  • [Most things related to Bookmarks-Browser were removed in API Level 23 (Android 6.0+)](https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-bookmark-browser) – Aashish Kumar Feb 09 '18 at 04:34
0

You need to implement the onPageFinished Method on the WebView Client Class

and you need to save your loaded url to the localdatabase or if you want that history for perticular session of the app then you can also use the other structure like arraylist...

class Callback extends WebViewClient{   

   public boolean shouldOverrideUrlLoading(WebView view, String url) {
   return (false);
 }
  @Override
public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);

           /// Save the url to the localdatabase or to any structure if you want temporary
   }

}

and while user press on the history button you need to fetch the data from localdatabase to cursor

and use cursoradapter to display the list of the history url

If you have any further doubt then feel free to ask me...

Sandy
  • 985
  • 5
  • 13
  • Please do explain in detail as i am a beginner regarding how to use cursor adapter and what coding do i need to place in my java file(h.java) in order to use the list view to display the browsing history – Kunal May 01 '14 at 09:18
  • @krunal first you need to understand how to store data and retrive data from local data here is a good tutorial http://www.vogella.com/tutorials/AndroidSQLite/article.html and for cursor adapter this link will give you example http://stackoverflow.com/questions/5457699/cursor-adapter-and-sqlite-example – Sandy May 01 '14 at 10:57