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