Reputation: 3
I have a webview in a tab activity fragment. However, my code only produces a blank page. The code does work in regular activities.
Main2Activity.java (Tab activity)
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
View rootView = inflater.inflate(R.layout.fragment_subpage1, container, false);
return rootView;
} else if (getArguments().getInt(ARG_SECTION_NUMBER) == 2) {
View rootView = inflater.inflate(R.layout.fragment_subpage2, container, false);
return rootView;
} else {
View rootView = inflater.inflate(R.layout.fragment_subpage3, container, false);
return rootView;
}
}
}
subpage2.java
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
final WebView browser = (WebView) getView().findViewById(R.id.webView);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_subpage1, container, false);
browser.getSettings().setJavaScriptEnabled(true);
browser.setWebViewClient(new WebViewClient() {
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
browser.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return false;
}
});
} else {
browser.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
}
}
});
browser.loadUrl("www.example.com");
return v;
}
And fragment_subclass.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="layout.subpage2">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView" />
Upvotes: 0
Views: 1974
Reputation: 3
Solved it by re-doing my tab fragments without unnecessary code from android studio. This time I did like in this tutorial.
Upvotes: 0
Reputation: 23881
Initialize webview like this:
View v = inflater.inflate(R.layout.fragment_subpage1, container, false);
final WebView browser = (WebView) v.findViewById(R.id.webView);
try this: add http
at front of the url
browser.getSettings().setJavaScriptEnabled(true);
browser.loadUrl("http://www.example.com"); //add http at front
browser.getSettings().setUseWideViewPort(true);
browser.getSettings().setLoadWithOverviewMode(true);
In manifest you have to add permission:
<uses-permission android:name="android.permission.INTERNET" />
Upvotes: 1