Loading web pages in webview in android?
i have a link for example,"http://google.com" , i need to load the web page in to my app either in web view or some o开发者_如何学JAVAther View, but not in default Browser of android , whether it is possible or not.
Thanks
I think you want to load URL in webview within the application. If i am not wrong, then you can have:
WebView browser = (WebView)findViewById(R.id.browser);
browser.setWebViewClient(new WebViewClient() {
/* On Android 1.1 shouldOverrideUrlLoading() will be called every time the user clicks a link,
* but on Android 1.5 it will be called for every page load, even if it was caused by calling loadUrl()! */
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
/* intercept all page load attempts and load yahoo.com instead */
String myAlternativeURL = "http://yahoo.com";
if (!url.equals(myAlternativeURL)) {
view.loadUrl(myAlternativeURL);
return true;
}
return false;
}
});
browser.loadUrl("http://google.com");
And In general, you can also use webview.loadURL(URL)
method to load URL in webview.
Yes you can do it using WebView, use the API , webview.loadUrl(your_url); give internet access permission in Android Manifest file.
Check here,
http://developer.android.com/reference/android/webkit/WebView.html
check e.g. with loadUrl();
You need to extend WebViewClient, and launch the url within that.
public class WebActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
webview = (WebView) findViewById(R.id.wv);
webview.setWebViewClient(new WebC());
webview.loadUrl("http://google.com");
}
public class WebC extends WebViewClient
{
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
super.onReceivedError(view, errorCode, description, failingUrl);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
... etc.
And in your layout xml,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<WebView
android:id="@+id/wv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
精彩评论