Ahmed Nawaz
Ahmed Nawaz

Reputation: 1644

I want to Show Static HTML files from Assets folder into Webview in Custom Dialog

I am trying to show a html file from assets folder into a Custom Dialog Box. But getting Errors. I am using following Codes.

My Custom Dialog Class

public class RobozoxDialog extends Activity{
Dialog my;
public RobozoxDialog(Context theContext){
    my= new Dialog(theContext);
}
public void showRobozoxDialog(){
    my.show();
}
public void setSource(String title,String url) {

    WebView myWebView = (WebView) findViewById(R.id.webview_dialog);
    myWebView.loadUrl(url); 
    my.setTitle(title);
}
}

Custom Dialog XML

<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview_dialog"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</WebView>

Calling Dialog Like this.

RobozoxDialog d = new RobozoxDialog(MainActivity.this);
d.setSource("Test", "file:///android_asset/test.html");
d.showRobozoxDialog();

But getting error and unable to solve the problem. When i show html file in webview of main activity xml it is showing. but when try to show in dialog webview it stops working..

Upvotes: 0

Views: 827

Answers (2)

Ahmed Nawaz
Ahmed Nawaz

Reputation: 1644

I done this by using LayoutInflater Code is below..

    LayoutInflater li = LayoutInflater.from(theContext);
    View promptsView = li.inflate(R.layout.dialog_layout, null);

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            theContext);
    alertDialogBuilder.setTitle("Registration");

    alertDialogBuilder.setView(promptsView);

    final WebView webview = (WebView) promptsView
            .findViewById(R.id.webview_dialog);
    webview.loadUrl("file:///android_asset/test.html");
    final AlertDialog alertDialog = alertDialogBuilder.create();

    alertDialog.show();

Upvotes: 1

SimonSays
SimonSays

Reputation: 10977

  1. Always add the logcat output to your question. We don't know what error you are getting.
  2. You never set any layout to your Dialog. See here how to create a custom Dialog.
  3. You try to get the WebView from the Activity instead of the Dialog. Try my.findViewById(R.id.webview_dialog)

Upvotes: 0

Related Questions