0

Iam developing an android app.I need to show my pdf file(which contains only one page) in popup window.here Iam starting new activity from popup.i have changed its theme in manifest.xml as:

 <activity
        android:name="com.example.myapp.Label"
        android:label="@string/title_activity_label"
        android:theme="@android:style/Theme.Dialog" > 
    </activity>

code of my Label.java is:

public class Label extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle b=getIntent().getExtras();

    String pdfurl=b.getString("url");
    Boolean dilg=b.getBoolean("isDialog");


    final String googleDocsUrl = "http://docs.google.com/viewer?url=";

    WebView mWebView=new WebView(Label.this);

   // mWebView.getSettings().setJavaScriptEnabled(true);
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setPluginState(PluginState.ON);

    mWebView.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url){

            view.loadUrl(url);
            return false; // then it is not handled by default action
       }
    });


    mWebView.loadUrl((googleDocsUrl + pdfurl));

    setContentView(mWebView);
}
}

My android version is:4.2

It is opening new activity in pop-up but not opening PDF.Is there any mistake in my code?

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
Neeha
  • 727
  • 3
  • 9
  • 21

1 Answers1

1

You need to display a custom dialog with webview.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<WebView
android:id="@+id/webview"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />

Display a dialog.

    Dialog dialog = new Dialog(Activity.this);
    dialog.setContentView(R.layout.web_dialog)
    WebView wb = (WebView) dialog.findViewById(R.id.webview);
    wb.getSettings().setJavaScriptEnabled(true);
    WebSettings webSettings = wv.getSettings();
    webSettings.setPluginState(PluginState.ON);
    wb.setWebViewClient(new WebViewClient() {
                                            public boolean shouldOverrideUrlLoading(WebView view, String url){

                                                view.loadUrl(image_urlpdf);
                                                return false; // then it is not handled by default action
                                           }
                                        });


    wb.loadUrl((googleDocsUrl + image_urlpdf));
    dialog.setCancelable(true);
    dialog.setTitle("WebView");
    dialog.show();
Raghunandan
  • 132,755
  • 26
  • 225
  • 256