Webview ( or WebViewFragment ) - android

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

Related

White Bar on LEFT of webview

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);

Webview activity crashes [duplicate]

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);

how to correctly load webview based on sample of creating swipe view

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;
}

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
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

Android-How to use a fragment to display a webview

Im updating some of my older projects and using fragments to update the look of things. I tried to use a fragment to launch a webview but when I try to run it I get the following error in my logcat.
E/Web Console(22464): Uncaught TypeError: Cannot read property 'addEventListener' of null at
http://m.yahoo.com/?.tsrc=yahoo&mobile_view_default=true:1
The way I used to use a webview was to just create a class that was its own activity that took place in a webview but I would like to have a small view within a fragment and then when I wanted to use the class I would launch it via intent and pass anything I needed to the webview like a url and other parameters in extras within the intent. I tried just setting up a webview within a fragment but I havent gotten it to work yet. This is the code Im using for the moment.
public class WebViewer extends Fragment {
WebView Wv;
String url = "http://www.yahoo.com";
Activity act;
public WebViewer(){}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.act = activity;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mView = inflater.inflate(R.layout.webview, container);
Wv = (WebView) mView.findViewById(R.id.webview1);
Wv.getSettings().setJavaScriptEnabled(true);
Wv.getSettings().setRenderPriority(RenderPriority.HIGH);
Wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
Wv.getSettings().setDomStorageEnabled(true);
Wv.setWebViewClient(new HelloWebViewClient());
Wv.getSettings().setBuiltInZoomControls(true);
Wv.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
act.setTitle("Loading...");
act.setProgress(progress * 100);
if(progress == 100)
getActivity().setTitle(R.string.app_name);
}
});
Wv.loadUrl(url);
return mView;
}
}
And then this is the layout for the activity that uses this fragment.
<?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"
android:orientation="vertical"
android:background="#drawable/bggreydotted"
>
<fragment
android:id="#+id/webFragment"
android:layout_width="150dip"
android:layout_height="match_parent"
android:name="my.proj.WebViewer"></fragment>
</LinearLayout>
So how can I get a webview to open inside a fragment I can use in a view.
Have you thought about just extending WebViewFragment?

Categories

Resources