WebView in a fragment Android Studio

1

I'm trying to show a web page in a web container, WebView, I've already done it but in a normal activity, now I want to do it but in a fragment, since I'm using a Navigation Drawer. I previously had the following code which worked well for me.

    String url="http://www.uniagustiniana.edu.co/";
    WebView view=(WebView) this.findViewById(R.id.webView01);
    view.getSettings().setJavaScriptEnabled(true);
    view.loadUrl(url);

Now I want to know how to use it but in a fragment. thank you very much;

    
asked by Ivan Alfredo 06.06.2017 в 23:01
source

2 answers

1

The way to load a url in a WebView in a Fragment or a Activity is identical, but instead of this use getActivity() to get the context of the activity that contains the Fragment , this is an example of how to do it:

We have a layout fragment_layout.xml which contains a WebView with id webView01 :

 @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        //View view = inflater.inflate(R.layout.fragment_layout, container);


         String url="http://www.uniagustiniana.edu.co/";
         WebView view=(WebView) getActivity().findViewById(R.id.webView01);
         view.getSettings().setJavaScriptEnabled(true);
         view.loadUrl(url);


        return view;
    }
    
answered by 07.06.2017 / 00:36
source
2

You can create a class for the fragment where you will have the webview. I'll give you an example that I've tried to see if it works.

WebviewFragment.java:

public class WebviewFragment extends Fragment {
    WebView webView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_layout,container);
        webView = (WebView)view.findViewById(R.id.webView1);
        webView.setWebViewClient(new WebViewClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("https://es.stackoverflow.com/");
        return view;
    }

}

fragment_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

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

MainActivity.java:

public class MainActivity extends AppCompatActivity {
    WebviewFragment fragment;
    android.app.FragmentManager fragmentManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragmentManager = getFragmentManager();
        fragment = (WebviewFragment) 
        fragmentManager.findFragmentById(R.id.fragment);

    }
}
    
answered by 06.06.2017 в 23:37