1

I am trying to display 'WebView' in 'AlertDialog'. For that I referred one of the question posted here: Displaying WebView in AlertDialog It successfully opens the dialog window. But I dont know some how its not showing the web content. This is my 'print_webview' file, which I am inflating in AlertDialog window:

'<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/root">
  <WebView
  android:id="@+id/dialog_webview"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>    
</LinearLayout>'

This is my Java file:

'public class CloudPrintTest extends Activity {
    static final int GOOGLE_CLOUD_PRINT_DIALOG = 1; //dialog ID for google cloud print

    /** Called when the activity is first created. */
    Button printButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        printButton = (Button)findViewById(R.id.button1);
        printButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //startActivity(new Intent(CloudPrintTest.this,PrintDialog.class));
                showDialog(GOOGLE_CLOUD_PRINT_DIALOG);
            }
        });
    }
    @Override
    protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
        switch(id){
        case GOOGLE_CLOUD_PRINT_DIALOG:
            //LayoutInflater layoutInflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //final View dialogLayout = layoutInflator.inflate(R.layout.print_webview, null);
            LayoutInflater inflater = LayoutInflater.from(CloudPrintTest.this);
            View alertDialogView = inflater.inflate(R.layout.print_webview, null);
            WebView myWebView = (WebView)alertDialogView.findViewById(R.id.dialog_webview);
            myWebView.getSettings().setJavaScriptEnabled(true);
            myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            myWebView.setWebViewClient(new PrintTestWebViewClient());
            myWebView.loadUrl("http://www.google.com/");
            AlertDialog.Builder builder = new AlertDialog.Builder(CloudPrintTest.this);
            builder.setView(alertDialogView);
            builder.setTitle("Google Cloud Print");
            builder.setCancelable(true);
            AlertDialog printDialog = builder.create();
            return printDialog;
        }
        return null;
    }

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

I have tried all the possible ways, but didnt get the result. Please help. Regards

1

1 Answer 1

0

What i would suggest is you creating an activity.

And when you register it in your manifest. Give it the Dialog look with

<activity android:theme="@android:style/Theme.Dialog">
1
  • I am not starting any activity as you can see I have commented the code 'startActivity(...)'. So there isn't any need to declare the above tag in manifest file.
    – YuDroid
    Sep 23, 2011 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.