1

I have put a WebView in the layout of my Fragment:

<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.deped.k12.GradeSheet"
    android:background="#fafafa">

    <!-- TODO: Update blank fragment layout -->

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

</FrameLayout>

But when I try to load any website from it. I just get a blank WebView: enter image description here

I tried looking for error messages in the Android Monitor but there are none.

Here's the code I use in the Fragment to load a URL:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_grade_sheet, container, false);

        String url = "http://www.google.com/";
        WebView wv = (WebView)view.findViewById(R.id.webView);
        WebSettings settings = wv.getSettings();
        wv.setWebChromeClient(new WebChromeClient() {
        });
        final String mimeType = "text/html";
        final String encoding = "UTF-8";
        String html = "";
        settings.setJavaScriptEnabled(true);
        wv.loadDataWithBaseURL(url, html, mimeType, encoding, "");


        return view;
    }

I also added the correct permission:

enter image description here

What might be the problem here?

Jayson Tamayo
  • 2,741
  • 3
  • 49
  • 76

2 Answers2

1

The following code works for me:

WebView webView = (WebView) view.findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);

I don't really know if you just have to add a WebViewClient.

More informations: WebViewClient vs WebChromeClient

Community
  • 1
  • 1
P-Zenker
  • 385
  • 2
  • 10
0

Your issue is:

String html = "";
settings.setJavaScriptEnabled(true);
wv.loadDataWithBaseURL(url, html, mimeType, encoding, "");

You're loading your empty html String into the webview.

Try something like: wv.loadUrl("http://example.com/"); instead or update your html string accordingly.

You should have a look at the API documentation for the loadDataWithBaseURL method.

Passer by
  • 562
  • 9
  • 14