1

I have made an android app by using the android webview. This app is actually a mobile site which contains the different courses.

In the website: When I open the course page there is a course pdf link which when clicked opens in a new tab.

In the android app When the site is opened in the mobile app, the course link (In menu) itself is getting opened in a browser.

My requirement is that the pdf link in the course page should open in a new tab.

Below is the code which I have tried.

MainActivity.java

package com.education.xxxxxxx;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "";
    private WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myWebView = (WebView)findViewById(R.id.webview);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
//        myWebView.loadUrl("http://xxxxxxx.com/");
//        myWebView.setWebViewClient(new WebViewClient());
        String myPdfUrl = "http://xxxxxxx.com/";
        if(myPdfUrl.endsWith(".pdf")){
         String googledocs="https://docs.google.com/viewer?url=";
         String pdf_url=googledocs+myPdfUrl;
            myWebView.loadUrl(pdf_url);
        }
        else{
            myWebView.loadUrl(myPdfUrl);
        }

    }

    @Override
    public void onBackPressed() {
        if(myWebView.canGoBack()){
            myWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        // getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement


        return super.onOptionsItemSelected(item);
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
5eeker
  • 1,016
  • 1
  • 9
  • 30
  • set webviewClient for webview, there should be a method in it shouldOverrideUrlLoading(), – Manohar Feb 19 '17 at 17:00
  • I tried this..but now the pdf is not getting opened. – 5eeker Feb 19 '17 at 17:08
  • I don't know if webview has support for tabbed content. Default is: all it's opened at the only one existing tab, the main one. What you may do is open the pdf inside a new activity containing another webview. – statosdotcom Feb 19 '17 at 17:10
  • @adi have a look at my answer – Manohar Feb 19 '17 at 17:11
  • @adi , confirmed, webview cannot afford tabs. Please, would you mind to check my answer as the correct one? I will plus one you for it. Thank you. – statosdotcom Mar 23 '17 at 05:09

2 Answers2

1

WebView does not support tabs. All is opened at the only one existing tab, the main one. What you may do is open the pdf inside a new activity containing another WebView.

statosdotcom
  • 3,109
  • 2
  • 17
  • 40
0

The pdf wont open directly you may use google doc viewer

 WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true); 
String pdf = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);

have a look at this

Community
  • 1
  • 1
Manohar
  • 22,116
  • 9
  • 108
  • 144