WebView in fragment (android.support.v4)

I got a tab menu using ViewPager . Each tab contains fragments from the android.support.v4 package (compatibility with old SDKs). One snippet is a WebView (called FragmentWeb ), and I want it to stay in the pager layout. The problem is that when my WebView , it works in full screen mode.

Is there a way to keep the web browser under my tabs?

thanks

My snippet class: FragmentWeb.java

 public class FragmentWeb extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View mainView = (View) inflater.inflate(R.layout.fragment_web, container, false); WebView webView = (WebView) mainView.findViewById(R.id.webview); webView.loadUrl("http://www.google.com"); return mainView; } } 

My fragment snippet: fragment_web.xml

 <?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" > <WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> 
+4
source share
2 answers

This can be done by adding the following code to your onCreateView in the code of the fragment and inserting a call to WebViewClient:

  webview.setWebViewClient(new MyWebViewClient()); webview.getSettings().setPluginsEnabled(true); webview.getSettings().setBuiltInZoomControls(false); webview.getSettings().setSupportZoom(false); webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); webview.getSettings().setAllowFileAccess(true); webview.getSettings().setDomStorageEnabled(true); webview.loadUrl(mTabURL); } return v; } public class MyWebViewClient extends WebViewClient { /* (non-Java doc) * @see android.webkit.WebViewClient#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String) */ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.endsWith(".mp4")) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse(url), "video/*"); view.getContext().startActivity(intent); return true; } else { return super.shouldOverrideUrlLoading(view, url); } } 
+6
source

You can simply adapt the current implementation of WebViewFragment to your needs by replacing:

 import android.app.Fragment; 

by

 import android.support.v4.app.Fragment; 

in your own copy of the source of WebViewFragment.java .

+7
source

Source: https://habr.com/ru/post/1396425/


All Articles