demo demo
demo demo

Reputation: 69

Webview not loaging url

I am simply tring to load url in my web view but its not getting load ,it simply showing me white screen but the same url works well when i hit it on web. here is my code to load url in webview :-

public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		Bundle bundle = this.getArguments();
		content = bundle.getString("content");
		url = bundle.getString("url");
		mRootView = inflater.inflate(R.layout.empty_fragment, container, false);
		mTextView = (TextView) mRootView
				.findViewById(R.id.txt_content_discription);
		mTextView.setText(Html.fromHtml(content));
		WebView mWebView = (WebView) mRootView
				.findViewById(R.id.webView_empty_fragment);
		WebSettings settings = mWebView.getSettings();
		mWebView.setWebViewClient(new MyBrowser());
		settings.setLoadsImagesAutomatically(true);
		settings.setDomStorageEnabled(true);
		mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.loadUrl(url);

		return mRootView;
	}

	private class MyBrowser extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}

		@Override
		public void onReceivedSslError(WebView view, SslErrorHandler handler,
				SslError error) {
			super.onReceivedSslError(view, handler, error);
			handler.proceed();
		}
	}

Here is my xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".1"
        android:background="#FFFFFF"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/txt_content_discription"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textColor="@color/orange_color"
            android:textStyle="bold|italic" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".9"
        android:orientation="vertical" >

        <WebView
            android:id="@+id/webView_empty_fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </WebView>
    </LinearLayout>

</LinearLayout>

Any help will be appreciated

Thanks

Upvotes: 0

Views: 433

Answers (4)

King of Masses
King of Masses

Reputation: 18775

I already answered the same type of question here. You can check it.

In addition to the @Rajesh Jadhav answer, if your drive having a login session you may need to provide the session access as well.

But If you use the view only url the user is not propted to login to there google account.

So in your second url case, simply you can use like this::

webView.loadUrl("https://docs.google.com/viewer?url="+ pdfURL);

Here pdf url vll be your tentative url which your trying to load

Still your facing the problem, then you have to follow the link below and setup content access either as Anyone with the link or Public web access, please be reminded that the URL would be something else as well.

Change your sharing settings

Upvotes: 1

Rajesh Jadav
Rajesh Jadav

Reputation: 12861

Unfortunately, Android does not support viewing PDF in a WebView.

WebView does not contain a PDF plugin that allow you to display a PDF document.

Luckily, Google allows you to perform this very task quite easily using Google Docs.

Try like this:

String pdf_url = "http://skyjack.com/sites/default/files/warranty.pdf";
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdf_url);

I hope it helps!

Upvotes: 2

Malte
Malte

Reputation: 604

The Webview does not show .pdf-files. If you want to show pdf u have to download it and open in a pdfviewer, for example adobe acrobat. u can send a broadcastintent to let the user select the application with which he wants to open the file.

Upvotes: 0

user4584887
user4584887

Reputation:

url= "https://docs.google.com/viewer?url="+myUrl+"&embedded=true";

webview.loadUrl(url);

in myurl specify your url.

The url with .pdf extension you have to open in google doc. Hope this helps!!

Upvotes: 2

Related Questions