panthro
panthro

Reputation: 24099

Web View in a Fragment - Android

How can I load a web view into a fragment?

I've tried the obvious but it opens a new browser and my nav disappears:

public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
{

View mainView = (View) inflater.inflate(R.layout.frag_a, group, false);
    myWebView = (WebView) mainView.findViewById(R.id.webview);
   myWebView.loadUrl("http://www.bbc.co.uk");
    return mainView;

}

I've also search stack overflow and the web, tried some of the examples but they do not work either, any help would be appreciated.

Thanks

Upvotes: 0

Views: 2542

Answers (3)

monopoint
monopoint

Reputation: 729

Setting a new WebViewClient to you webView will let the WebView handle urls internally.

webViewer = (WebView) mainView.findViewById(R.id.webview);
webViewer.setWebViewClient(new WebViewClient());

this is supported by the documentation: http://developer.android.com/guide/webapps/webview.html

Upvotes: 0

marwinXXII
marwinXXII

Reputation: 1446

I often had such situation, when after redirect page was opened in an external browser. Try to set WebViewClient with overriden shouldOverrideUrlLoading method which returns false.

Upvotes: 1

Paresh Mayani
Paresh Mayani

Reputation: 128458

Try:

public class DetailsFragment extends Fragment {
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
    return(inflater.inflate(R.layout.details_fragment, container, false));
  }

  public void loadUrl(String url) {
    ((WebView)(getView().findViewById(R.id.browser))).loadUrl(url);
  }
}

Upvotes: 0

Related Questions