Android - Displaying WebView In Fragment
So I have a fragment with the following in the XML layout file: Copy
Solution 2:
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);
}
}
Solution 3:
@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);
}
Post a Comment for "Android - Displaying WebView In Fragment"