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
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.
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);
}
}
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
Related
I am getting a solid white bar on the LEFT side of my webview from a fragment. I have seen examples where the bar was on the right but not on the left. I have tried every solution from those examples such as
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
or
mWebView.setVerticalScrollBarEnabled(false);
and within the XML layout setting scrollbars to
android:scrollbars="none"
Is there any way of removing this white bar? Additionally I have a navigation drawer on the left that I believe could be associated with the issue.
[]
WebFragment.java
public class WebFragment extends Fragment implements IOBackPressed {
public WebView mWebView;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_shop, container, false);
mWebView = (WebView) view.findViewById(R.id.webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new myWebViewClient());
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setScrollBarSize(0);
mWebView.loadUrl("");
return view;
}
#Override
public boolean onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
//backpress is not considered in the activity
return true;
} else {
return false;
}
}
}
I have checked your code , use this code before return view statement ,it will work fine .
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
I am trying to use a fragment to make some buttons that when clicked will open in a Webview with different URLs. Eg. when i click the first button i go to "https://google.com/", second button to "https://fb.com" and so on.
I have made methods like so for each redirect (as I was using intents to redirect to a web browser in previous version):
private void goToGoogle(View view) {
this.view = view;
goToUrl("https://google.com");
}
The goToURL(String URL) method is simply an intent to open the browser with the URL as parameter.
Now, I have been asked to implement an in-app webview. I have the following code:
public void openWebView (String url){
mWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl(url);
mWebView.setWebViewClient(new WebViewActivity());
}
which I call with openWebView(String.valueOf(R.string.googleURL));.
My onCreateView method is declared as follows (example for 1 button, there are 16 on the real thing):
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_imp_links, container, false);
LinearLayout activityLayout = new LinearLayout(getActivity());
Button1 = view.findViewById(R.id.goToGoogle);
Button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goToGoogle(v);
}
});
I have also got this class in my app to handle the WebView:
public class WebViewActivity extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
I am getting java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
Any help you can provide is highly appreciated. Thank you.
Definition of mWebView: private WebView mWebView = new WebView(getContext());
To get a string from the resources, do not use String.valueOf(R.string.googleURL).
Instead use Context#getString(R.string.googleURL).
This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 5 years ago.
I am using a navigation drawer in which while clicking on textview to open new activity(web view), but my activity is crashing
public class FirstFragment extends Fragment {
View myView;
private WebView mywebview;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.first_layout,container,false);
mywebview =(WebView) container.findViewById(R.id.web);
WebSettings webSettings=mywebview.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebview.loadUrl("http://www.proxbotcreatn.com/");
mywebview.setWebViewClient(new WebViewClient());
return myView;
}
}
You should pass myView instead of container.
Wrong
mywebview =(WebView) container.findViewById(R.id.web);
Proper Way
mywebview =(WebView) myView.findViewById(R.id.web);
I am new and learning to create a webview by selecting a tab, based on google's sample at http://developer.android.com/training/implementing-navigation/lateral.html.
I created a class for webview fragment:
public static class WebViewFragment<rootView> extends Fragment {
WebView rootView;
public WebView onCreate(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
inflater.inflate(R.layout.fragment_webview_1, container,false);
rootView.getSettings().setJavaScriptEnabled(true);
rootView.loadUrl("http://www.google.com");
return rootView;
}
}
However, it runs without error but the webview interface never shows up. Appreciate if you can provide any clues.
Above method is actually not called by Android. It is a simple onCreate overloaded method.
Change that method to:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// would be safe to check class
rootView = (WebView)inflater.inflate(R.layout.fragment_webview_1, container,false);
rootView.getSettings().setJavaScriptEnabled(true);
rootView.loadUrl("http://www.google.com");
return rootView;
}
I am kind of lost here so please give me some help.
I've been working on an app on ICS 4.0.2 for my GNex.
As of yet my app structure is as follows :
DemoActivity extends Activity contains three fragments that show up as tabs :
MapFragmentTab extends Fragment
SettingsFragmentTab extends Fragment
AboutFragmentTab extends Fragment
In the map fragment, I want to have a WebView display Google Maps.
This is my MapFragmentTab :
public class MapFragmentTab extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View mainView = (View) inflater.inflate(R.layout.map, container, false);
WebView webView = (WebView) mainView.findViewById(R.id.webview);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl("http://maps.google.com");
return mainView;
//return inflater.inflate(R.layout.map, container, false);
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
Right now, it seems to be working, however Google Maps ( although the page frame does load ) won't load.
I use Chrome Beta, haven't tried the default browser yet.
I'm really confused when it comes to WebViews, WebViewFragments and Fragments...
any help would be appreciated.
You need to enable JavaScript specifically on WebView, as it is disabled by default. Use something like: webView.getSettings().setJavaScriptEnabled(true);
From: http://developer.android.com/reference/android/webkit/WebSettings.html#setJavaScriptEnabled(boolean)
http://developer.android.com/resources/tutorials/views/hello-webview.html