display pdf file from google drive - android

now i'm trying this code
public class MainActivity extends AppCompatActivity {
private String myPdfUrl = "https://drive.google.com/open?id=1rquwQNdXeds39Rlb32isX3bjYghHitCV/view";
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView=(WebView)findViewById(R.id.ve);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(myPdfUrl);
return true;
}
}
);
webView.loadUrl(myPdfUrl);
}
}
but it doesn't work

you try this code to make load PDF
i also implement in my application
String pdfURL = "your url";
webView.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdfURL);

Related

WebViewClient shouldOverrideUrlLoading doesn't seem to work

I'm trying to listen to url changes from a WebView client and just log the updates. I'm navigating through my webView and shouldOverrideUrlLoading() is never called. I don't know why.
Here is my code :
public class WebViewActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
Intent i = getIntent();
String url= i.getStringExtra("url");
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String currentUrl = url;
Log.i("currentUrl",": " + currentUrl);
view.loadUrl(url);
return true;
}
});
webView.loadUrl(url);
}
}

How to open a url other than defined url in webview?

I tried my best but it doesn't work for me. I want to open url other than http://google.com in default browser. What code should add inside, I seen android documentation and added the code but it doesn't work. Any suggestion is appreciated.
public class MainActivity extends Activity {
private WebView webview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
this.webview = (WebView) findViewById(R.id.activity_main_webview);
webview.loadUrl("http://google.com");
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webview.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
}
}
You can try to this hope this can help you..
public class MainActivity extends AppCompatActivity {
private TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Uri uri = Uri.parse("http://gmail.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
}
try this snippet and make sure you have Internet permission in manifest file
public class MainActivity extends Activity {
private WebView webview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView) findViewById(R.id.activity_main_webview);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
webview.loadUrl("http://google.com/");
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String myAlternativeURL = "http://yahoo.com";
if (!url.equals(myAlternativeURL)) {
view.loadUrl(myAlternativeURL);
return true;
}
}
});
});

Android WebView with http loadUrl shows blank/empty page

I am developing an Android Application. I want to show web site with WebView. But i don't it. A blank/empty page is opening. The other web site showing but Why this is not showing?
My code is below
Please Help me,
Thanks.
public class MainActivity extends ActionBarActivity{
private WebView ourWebSite;
private ProgressDialog pd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initViews();
setProgressDialog();
}
private void initViews(){
ourWebSite = (WebView) findViewById(R.id.ada_web_site);
ourWebSite.getSettings().setJavaScriptEnabled(true);
ourWebSite.setWebViewClient(new WebSiteWebViewClient());
ourWebSite.loadUrl("http://fahrikayahantaksi.com/");
}
private void setProgressDialog(){
pd = new ProgressDialog(MainActivity.this);
pd.setMessage(getResources().getString(R.string.loading));
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.show();
}
private class WebSiteWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
if (!pd.isShowing()) {
pd.show();
}
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
System.out.println("on finish");
if (pd.isShowing()) {
pd.dismiss();
}
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed(); // Ignore SSL certificate errors
}
}
}
I think in your code you missed this suppressLint
#SuppressLint("SetJavaScriptEnabled")
Try to change your code like this.. in this way i am able to load your [page]
private WebView mWebview=null ;
#SuppressLint("SetJavaScriptEnabled")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview.loadUrl("http://fahrikayahantaksi.com/");
setContentView(mWebview );
}
Don't forgot to add the Internet permission in your manifest !!
uses-permission android:name="android.permission.INTERNET" />
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
String url = getIntent().getStringExtra("url");
webView = new WebView(this);
webView.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
webView.loadUrl("http://docs.google.com/viewer?embedded=true&url=" + url);
setContentView(webView );
}
I used google docs to open pdf.

how to surf internet from web view in android

Hey guys I want to surf internet from webview in android and i am getting the page for which i have provided url in edittext.But when click on a link inside webview then it is unable to find that page.I have provided internet permission in manifest file and taken web view in layout.
This is my main activity which needs to be corrected somewhere.Thanks in advance and help done will be greatly appreciated.
public class MainActivity extends Activity {
EditText et1;
Button btn;
WebView wv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(EditText) findViewById(R.id.et1);
btn=(Button) findViewById(R.id.btn);
wv=(WebView) findViewById(R.id.wv);
wv.setWebViewClient(new mybrowser());
btn.setOnClickListener(new OnClickListener() {
#SuppressLint("SetJavaScriptEnabled") #Override
public void onClick(View v)
{
String url = et1.getText().toString();
wv.getSettings().setLoadsImagesAutomatically(true);
wv.getSettings().setJavaScriptEnabled(true);
wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv.loadUrl("https://"+url);
wv.requestFocus();
}
});
}
class mybrowser extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
String url1=et1.getText().toString();
view.loadUrl(url1);
return true;
}
}
}
Try to do like
class mybrowser extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
}
and remove String url1=et1.getText().toString();

facebook like on webview android shows blank screen after login

I utilize the facebook html like for my app to open a webview like this https://developers.facebook.com/docs/plugins/like-button/
The webview shows a Like and Share button, but after I login to facebook, it doesnt return to the Like and Share button, but a blank page, the share button works fine.
So how do I return to the facebook like url after logging in?
public class LikeFacebookActivity extends BaseActivity {
private WebView webView;
private final String URL = "facebookIDhere";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.like_facebook_webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
showLoading();
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl(URL);
ActionBar actionbar = getActionBar();
actionbar.setCustomView(R.layout.actionbar_top_like_facebook);
actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
Button backButton = (Button) findViewById(R.id.buttonGeneralBack);
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
#Override
public void onBackPressed() {
finish();
overridePendingTransition(R.anim.animation_slide_from_left,
R.anim.animation_slide_to_right);
}
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("something")) return true;
return false;
}
public void onPageFinished(WebView view, String url) {
hideLoading();
}
}
}
I have solved this, just use system.out.println to see which page does facebook load after loggin in
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("something")) return true;
return false; //Default is to not override unless our condition is met.
}
public void onPageFinished(WebView view, String url) {
hideLoading();
//String webUrl = webView.getUrl();
//System.out.println(webUrl);
if(url.startsWith("https://www.facebook.com/plugins/close_popup.php#_=_")){
String redirectUrl = URL;
view.loadUrl(redirectUrl);
return;
}
super.onPageFinished(view, url);
}
}

Categories

Resources