dwinnbrown
dwinnbrown

Reputation: 4029

Android - Displaying WebView in Fragment

So I have a fragment with the following in the XML layout file:

<RelativeLayout 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.dwinnbrown.myapplicationv20.MainFragment">

    <!-- TODO: Update blank fragment layout -->
    <WebView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/webView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

I am now trying to load a url into the web view through the java file associated with the fragment. However I am running into an error "cannot resolve findViewByID(int)". Any ideas?

Here is the code I am trying to use for the fragment:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;


/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment extends Fragment {

    public MainFragment() {
        // Required empty public constructor
    }


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


        String url = "http://winn-brown.co.uk/";
        WebView view = (WebView) this.findViewById(R.id.webView);
        view.getSettings().setJavaScriptEnabled(true);
        view.loadUrl(url);
    }



}

Upvotes: 2

Views: 12256

Answers (3)

Priya Vasoya
Priya Vasoya

Reputation: 215

Below is the xml for fragment:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".WebActivity">

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

</RelativeLayout>

And try this for fragment class

public class MainFragment extends Fragment {

WebView webView;

public MainFragment() {

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    webView=(WebView)getActivity().findViewById(R.id.webview);
    WebSettings webSettings=webView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    webView.loadUrl("your_url");

    webView.setWebViewClient(new Mybrowser());

    if (savedInstanceState != null)
        webView.restoreState(savedInstanceState);
    else
        webView.loadUrl("your_url");
}

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

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

Upvotes: 1

Tunuguntla Saravan
Tunuguntla Saravan

Reputation: 9

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


        String url = "http://winn-brown.co.uk/";
        WebView wv = (WebView) view.findViewById(R.id.webView);
        wv.setWebViewClient(new WebViewClient());
        wv.getSettings().setJavaScriptEnabled(true);
        wv.loadUrl(url);
    }

Upvotes: 0

Masum
Masum

Reputation: 4979

Do Something like this

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

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

    String url = "http://winn-brown.co.uk/";
    WebView view = (WebView) rootView.findViewById(R.id.webView);
    view.getSettings().setJavaScriptEnabled(true);
    view.loadUrl(url);

    return rootView ;
}

Upvotes: 2

Related Questions