Android-How to use a fragment to display a webview

21,083

Have you thought about just extending WebViewFragment?

Share:
21,083
James andresakis
Author by

James andresakis

Thanks to those that helped along the way.....

Updated on April 03, 2020

Comments

  • James andresakis
    James andresakis about 4 years

    Im updating some of my older projects and using fragments to update the look of things. I tried to use a fragment to launch a webview but when I try to run it I get the following error in my logcat.

     E/Web Console(22464): Uncaught TypeError: Cannot read property 'addEventListener' of null at    
     http://m.yahoo.com/?.tsrc=yahoo&mobile_view_default=true:1
    

    The way I used to use a webview was to just create a class that was its own activity that took place in a webview but I would like to have a small view within a fragment and then when I wanted to use the class I would launch it via intent and pass anything I needed to the webview like a url and other parameters in extras within the intent. I tried just setting up a webview within a fragment but I havent gotten it to work yet. This is the code Im using for the moment.

        public class WebViewer extends Fragment {
    WebView Wv;
    String url = "http://www.yahoo.com";
    Activity act;
    public WebViewer(){}
    
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
    }
    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.act = activity;
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle  savedInstanceState) {
    
        View mView = inflater.inflate(R.layout.webview, container);
    
          Wv = (WebView) mView.findViewById(R.id.webview1);
          Wv.getSettings().setJavaScriptEnabled(true);
          Wv.getSettings().setRenderPriority(RenderPriority.HIGH);
          Wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
          Wv.getSettings().setDomStorageEnabled(true);
          Wv.setWebViewClient(new HelloWebViewClient());
          Wv.getSettings().setBuiltInZoomControls(true);
          Wv.setWebChromeClient(new WebChromeClient() {
                    public void onProgressChanged(WebView view, int progress)
                    {
                        act.setTitle("Loading...");
                        act.setProgress(progress * 100);             
                        if(progress == 100)
                            getActivity().setTitle(R.string.app_name);
                    }
                });
          Wv.loadUrl(url);
    
        return mView;
    
    }
    }
    

    And then this is the layout for the activity that uses this fragment.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/bggreydotted"
     >
    
    <fragment 
        android:id="@+id/webFragment"
        android:layout_width="150dip"
        android:layout_height="match_parent"
        android:name="my.proj.WebViewer"></fragment>
    
    
    </LinearLayout>
    

    So how can I get a webview to open inside a fragment I can use in a view.

  • Christian Davén
    Christian Davén over 4 years
    Note that this answer is from 2013, and the documentation says, "This class was deprecated in API level 28."