How to load external webpage inside WebView

How to load external webpage inside WebView



My problem is that the webpage is not loaded inside the webview.



mWebview.loadUrl("http://www.google.com"); launches the web browser...


mWebview.loadUrl("http://www.google.com");



This is the code of my activity:


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

public class Main extends Activity

private WebView mWebview;

@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);

mWebview = new WebView(this);
mWebview.loadUrl("http://www.google.com");
setContentView(mWebview);




I added the required permission in the Manifest:


<uses-permission android:name="android.permission.INTERNET" />






Hello Gilbou your Code is Working in 2.2

– Dipak Keshariya
Sep 5 '11 at 8:17






But not with me :( Neither on HTC Wildfire, neither on the Emulator... I just don't understand.

– Gilbou
Sep 5 '11 at 8:45






Ok... now it works. But it launches the web browser instead of showing the page inside the webview.

– Gilbou
Sep 5 '11 at 9:00







Hello, this means If U R Run this code the Browser is open.

– Dipak Keshariya
Sep 5 '11 at 10:25




9 Answers
9



Thanks to this post, I finally found the solution. Here is the code:


import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import android.annotation.TargetApi;

public class Main extends Activity

private WebView mWebview ;

@Override
public void onCreate(Bundle savedInstanceState)

super.onCreate(savedInstanceState);

mWebview = new WebView(this);

mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript

final Activity activity = this;

mWebview.setWebViewClient(new WebViewClient()
@SuppressWarnings("deprecation")
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();

@TargetApi(android.os.Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr)
// Redirect to deprecated method, so you can use it in all SDK versions
onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());

);

mWebview .loadUrl("http://www.google.com");
setContentView(mWebview );









Nice thing but i want to Display the web contain page wise.it`s possible using webview?

– Zala Janaksinh
Aug 16 '12 at 6:37






Dont forget to add Permission <uses-permission android:name="android.permission.INTERNET" />

– star18bit
Jun 5 '13 at 5:38







Actually, in order to handle with the url inside your webview you should set a webviewclient, if you do not set a client, default behaviour is to launch an application that handles URLs. See this link

– erdemlal
Nov 22 '13 at 15:59






Enabling java script opens up your app to potential security issues and I am sure android sdk warns you against it when you use it in code. Don't do this unless you can control which websites you display in your webview and enabling java script should not be seen as an easy solution.

– LostPuppy
Jun 13 '14 at 22:41






Shouldn't that onReceivedError be an @Override?

– Adrian Seeley
Jan 21 '15 at 20:40


onReceivedError


@Override



try this



webviewlayout.xml:


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



In your Activity:


WebView webView;
setContentView(R.layout.webviewlayout);
webView = (WebView)findViewById(R.id.help_webview);
webView.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.google.com");



Update



Add webView.setWebViewClient(new WebViewController()); to your Activity.


webView.setWebViewClient(new WebViewController());



WebViewController class:


public class WebViewController extends WebViewClient

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
view.loadUrl(url);
return true;







still have the same problem after implementing your solution...

– Gilbou
Sep 5 '11 at 8:37







sorry. seems I changed something in the manifest and now it works too. but still, the page is loaded in the web browser, not in the webview.

– Gilbou
Sep 5 '11 at 9:03






I have updated the code to force load page in webview

– Farhana Haque
Sep 5 '11 at 10:26






this line worked for me: webView.getSettings().setJavaScriptEnabled(true);

– pixparker
Sep 4 '14 at 17:06






mWebview.getSettings().setJavaScriptEnabled(true); worked for me, we have to add this line

– neena
Aug 18 '16 at 10:49


public class WebViewController extends WebViewClient
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
view.loadUrl(url);
return true;


webView.setWebViewClient(new WebViewController());






It'd be even better if you explained the code you posted.

– user1114055
Nov 2 '12 at 11:29






I am setting a custom webViewClient to my webview. In my custom made WebViewClient, I have overloaded the shouldOverrideUrlLoading method to load my url. I am passing my url with this loc: webview.loadUrl("URL");

– Rahul
Aug 28 '13 at 9:57







This is a bad way of doing it. shouldOverrideUrlLoading is called for all pages loaded in the webview. This includes iFrames, meaning that if the page loads an iFrame, the page will be replaced by the iFrame. This is discouraged on the relevant android docs page.

– Penn
Jan 13 '18 at 8:19


shouldOverrideUrlLoading



Please use this code:-



Main.Xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/background">
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="@drawable/top_heading"
android:id="@+id/rlayout1">
<TextView android:layout_width="wrap_content"
android:layout_centerVertical="true" android:layout_centerHorizontal="true"
android:textColor="#ffffff" android:textSize="22dip"
android:textStyle="bold" android:layout_height="wrap_content"
android:text="More Information" android:id="@+id/txtviewfbdisplaytitle" />
</RelativeLayout>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_below="@+id/rlayout1"
android:id="@+id/rlayout2">
<WebView android:id="@+id/webview1" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0" />
</RelativeLayout>
</RelativeLayout>



MainActivity.Java


public class MainActivity extends Activity
private class MyWebViewClient extends WebViewClient
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
view.loadUrl(url);
return true;


Button btnBack;
WebView webview;
@Override
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

webview=(WebView)findViewById(R.id.webview1);
webview.setWebViewClient(new MyWebViewClient());
openURL();


/** Opens the URL in a browser */
private void openURL()
webview.loadUrl("http://www.google.com");
webview.requestFocus();




Try this code if any query ask me.






it works. But you missed 2 things. First, you did not show internet permission is needed, Second, use of background image. thanks

– Sumon Bappi
Oct 17 '15 at 9:10



just go into XML file and give id to your webView then in java paste these line:


public class Main extends Activity

private WebView mWebview;

@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.Your_layout_file_name);

mWebview = (WebView)findViewById(R.id.id_you_gave _to_your_wenview_in_xml);
mWebview.loadUrl("http://www.google.com");




You can do like this.


webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("Your URL goes here");



It's very simple try integrate these lines of code
first take permission in the Android Manifest file


<uses-permission android:name="android.permission.INTERNET" />



then write some code in you Activity.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.MainActivity">

<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/help_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"

/>

</LinearLayout>



Then write these code in your MainActivity.java


import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity
private WebView mWebview ;
String link = "";// global variable
Resources res;// global variable
@Override


protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_modernherbal_main);
mWebview = (WebView) findViewById(R.id.help_webview);
WebSettings webSettings = mWebview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);



final Activity activity = this;

mWebview.setWebViewClient(new WebViewClient()
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();



);

mWebview .loadUrl("http://www.example.com");






Try this it'll help you to solve your problem



try this;


webView.loadData("<iframe src='http://www.google.com' style='border: 0; width: 100%; height: 100%'></iframe>", "text/html; charset=utf-8", "UTF-8");



Add below method in your activity class.Here browser is nothing but your webview object.



Now you can view web contain page wise easily.


@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
if ((keyCode == KeyEvent.KEYCODE_BACK) && browser.canGoBack())
browser.goBack();
return true;

return false;






Doesnt answer the question

– CocoNess
Oct 6 '12 at 10:46






This adds a functionality to the "back" button at the bottom of the Android phone: It works as a "back" browser button.

– Don Larynx
Mar 11 '15 at 20:30



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)