Question

I wanna load webpage inside fragment, but there is some problem.

1.without overriding shouldOverrideUrlLoading method, the website is open with default browser Chrome.

2.when overriding shouldOverrideUrlLoading, nothing happens. just new blank activity is created. This is my onCreateView method inside Fragment class

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View resultView = inflater.inflate(R.layout.container, container, false);

    if (getArguments() != null) {
        String url  = getArguments().getString(KEY_URL);

        WebView productDetailView = (WebView)resultView.findViewById(R.id.webView);
        productDetailView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return super.shouldOverrideUrlLoading(view, url);
            }
        });

        productDetailView.loadUrl(url);
    }
    return resultView;
}

targetSdkVersion is 19 and added INTERNET permission and XML file of Fragment is like below.

<FrameLayout 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"
    tools:context="com.example.ProductDetailFragment">


    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"
        android:layout_gravity="center" />

</FrameLayout>

what's the problem?

Was it helpful?

Solution

Try this code

webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

});
webView.loadUrl(url);

and start your activity with FLAG_ACTIVITY_NEW_TASK

OTHER TIPS

      webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    Intent intent = getIntent();
    webView.setSaveEnabled(false);
    String receivedName = (String) intent.getSerializableExtra("USERNAME");
    Log.d("receivedName ----- ", receivedName);
    webView.loadUrl("https://docs.google.com/gview?embedded=true&url=" +  
          receivedName);
    webView.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent,
                String contentDisposition, String mimetype,
                long contentLength) {
          Webview.this.finish();
            }
    });

Try this code...

Try this.

    final ProgressDialog pd = ProgressDialog.show(ActivityA.this, "", "Loading...", true);  
     wv = (WebView) findViewById(R.id.webview); 
     wv.getSettings().setJavaScriptEnabled(true);
     wv.loadUrl("<url");
     wv.setWebViewClient(new WebViewClient() {
         @Override
         public void onPageStarted(WebView view, String url, Bitmap favicon)
         {
             pd.show();
         }
         @Override
         public void onPageFinished(WebView view, String url) {
             pd.dismiss();
                 }
    });

Have you tried adding this <uses-permission android:name="android.permission.INTERNET" /> into Manifest file ? https://developer.android.com/reference/android/webkit/WebView.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top