PDF in webview Android 6.0 - android

I am trying to load pdf file from link in Android Marshmallow. It always shows blank screen while the code works fine with lower versions than 6.0. Please help.
Thanks
Below is code which i am using
webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
webView.loadUrl ("http://drive.google.com/viewerng/viewer?embedded=true&url="+url);
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
// showProgress(LoadPageByUrlActivity.this,"Please wait...");
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view, url);
// stopProgress();
}
});
String url = getIntent().getExtras().getString("url");
if(url.contains("pdf")){
webView.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url="+url);
}else
webView.loadUrl(url);
}

Related

ProgressBar in android webview not stop spinning

i'm creating a android webview app. Im facing a problem with progress bar. The progress bar is completely and fully implemented and keeps spinning. but it doesn't stop spinning even after the page loads completely. what should i do?
main activity layout::::
<ProgressBar android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
mainActivity.java:::
public class MainActivity extends ActionBarActivity {
WebView web;
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = (WebView) findViewById(R.id.webView1);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setBuiltInZoomControls(true);
web.loadUrl("http://google.com");
web.setWebViewClient(new WebViewClient()); }
public class webclient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
}
Where is the error here? please help me im also having problem with adview. here
You've created a custom WebViewClient class but you aren't using it.
Change web.setWebViewClient(new WebViewClient());
to web.setWebViewClient(new webclient());
Change these two methods.
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
progressBar.dismiss();
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
Change web.setWebViewClient(new WebViewClient());
to web.setWebViewClient(new webclient());

webview links opens into a default browser

i'm new to android programing.my problem is that in my webview when i click on any link its opening to a new web browser i dont need this i want it to be opened in my app itself.can someone help me??
this is my code
title = extras.getString("title");
url = extras.getString("url");
TextView text=(TextView) findViewById(R.id.textView1);
text.setText(title);
WebView myWebView =(WebView)findViewById(R.id.WebView);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
}
i tried this but my webview is not loading its showing a blank screen
web = (WebView) findViewById(R.id.webview01);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://www.google.com");
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}

How to hide page loading process inside webview while control is inside onpagestarted() in android?

i don't want to show page loading process inside webview while control is inside onpagestarted() android. what i want is, to show the the contents in web view when complete page is downloaded. the code i am using is`
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://iba.edu.pk/");
and `WebViewClient class is as follows
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
//web.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);
Log.d("Main"," onPageStarted");
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
Log.d("Main"," shouldOverrideUrlLoading");
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
Log.d("Main"," on Page finsihed");
progressBar.setVisibility(View.GONE);
}
}

How To Display Custom "Web Page Not Available" Error Page in WebView

pls help me to display custom Web Page Not Available error page...in webview..i tried many code and when i run the app it shows app has been stopped
You can override onReceivedError callback of WebViewClient to achieve the desired functionality.
Either you can display some dialog to denot your custom message or load the webview with any custom html available locally in your app.
USE this it working fine on my side. May be it will help you
private WebView webviewSites;
String URL="http://www.google.com";
webviewSites = (WebView) findViewById(R.id.webview_sites);
WebSettings webSettings = webviewSites.getSettings();
webSettings.setSupportMultipleWindows(true);
webSettings.setJavaScriptEnabled(true);
webviewSites.setWebViewClient(new myWebClient());
webviewSites.loadUrl(URL);
public class myWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(final WebView view, final String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
// mProgressBar.setVisibility(View.GONE);
Log.i("LOAD_URL", "" + url);
}

Show activity circle before loading webview - Android

I am using webview to show a webpage in my app. The webpage takes about 2-3 seconds to load.
Till the webpage loads, I want to show an activity circle progress bar like the one shown here. This is what an activity circle looks like :
How can I accomplish it?
I searched the web but couldn't find a satisfactory and proper explanation of how it is implemented. Thanks!
WebViewPage.java
public class Webpage extends Activity {
WebView web;
ProgressBar progressBar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_page);
Intent startwebpage = getIntent();
web = (WebView) findViewById(R.id.webView);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://www.google.com");
}
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
}
}
Display progressBar in onPageStarted method and dismiss it in onPageFinished.
private ProgressDialog progressBar;
Show progressBar in onPageStarted:
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
// prepare for a progress bar dialog
progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(true);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressBar.show();
}
2.Dismiss it in onPageFinished:
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.dismiss();
}
EDIT : Use ProgressBar instead of ProgressDialog.
Use Following code in your layout.
Use drawable of your choice for "#drawable/progress"
<ProgressBar
android:id="#+id/progress_bar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="gone"
android:indeterminateDrawable="#drawable/progress" >
</ProgressBar>
Inflate it in your activity. Make it visible in onPageStarted and gone in onPageFinished.
private ProgressBar prgrsBar;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
prgrsBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
prgrsBar.setVisibility(View.GONE);
}
This works for me:
ProgressDialog pb = null;
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
// prepare for a progress bar dialog
if (pb == null) {
pb = new ProgressDialog(Quiz.this);
pb.setCancelable(true);
pb.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pb.show();
}
}
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
pb.dismiss();
}

Categories

Resources