Noman Hamid Noman Hamid -4 years ago 94
Android Question

Load Webpage when an Item is clicked in a ListView

i have a ListView that simly shows items on it.
I want to load a webpage e.g; google.com , when i click an item of the list view.
how can i do this? plz explain with a code sample.
i m new to android.

here is my list view code:

package com.androidListActivity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MyList extends ListActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);

// setContentView(R.layout.main);

String[] names = new String[]
{
"One",
"Two"
};
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.rowlayout, names));

}

protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l,v,position,id);
}
}


Plz edit this for example.
Thanks in advance

Answer Source
protected void onListItemClick(ListView l, View v, int position, long id)
{
    super.onListItemClick(l,v,position,id);
    Intent in = new Intent(this,WebViewPage.class);
    startActivity(in);
}

WebViewPage.java

public class WebViewPage extends Activity{

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webviewpage);
            WebView wbView = (WebView) findViewById(R.id.WebView);
            wbView.getSettings().setJavaScriptEnabled(true); 
            wbView.loadUrl("http://www.google.com");
   }
}

webviewpage.xml

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

And finally add permission in Manifest file

 <activity android:name=".WebViewPage" />


 <uses-permission android:name="android.permission.INTERNET" />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download