Wednesday, 22 March 2017

WebView Dynamic Loading

public class PrintAct extends AppCompatActivity {

    private WebView myWebView;
    String mailReceivedTime;
    String mailPreview;
    String mailSubject;
    String folderPath;
    String mailFromName;
    int uid;
    public static int count = 5;
    static StringBuffer sb;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_print_sample);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Count();



     

        WebView webView = new WebView(this);
        webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view,
                                                    String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                createWebPrintJob(view);
                myWebView = null;
            }
        });

        Log.i("from ", mailFromName);

        String htmlDocument =
                "<html><body><h1> From:" + mailFromName + "</h1>" + "</br>" + "<h1> To:" + mailFromName + "</h1></br>" + "<h1>Subject:" + mailSubject + "</h1></br>" + "<h2>" + mailReceivedTime + "</h2></br>" +
                        "<h3><p>" + mailPreview + "</p></h3>" + "<h1>" + sb.toString() + "</h1>" +
                        "</body></html>";

        webView.loadDataWithBaseURL(null, htmlDocument,
                "text/HTML", "UTF-8", null);

        myWebView = webView;
    }


    private void createWebPrintJob(WebView webView) {

        PrintManager printManager = (PrintManager) this
                .getSystemService(Context.PRINT_SERVICE);

        PrintDocumentAdapter printAdapter =
                null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            printAdapter = webView.createPrintDocumentAdapter();
        }

        String jobName = getString(R.string.app_name) + " Print Test";

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            printManager.print(jobName, printAdapter,
                    new PrintAttributes.Builder().build());
        }
    }





    private static void Count() {
        sb = new StringBuffer();
        for (int j = 0; j <= 5; j++) {
            sb.append("<img src=file:///android_res/drawable/other_large_icon_thumbnail.png />" + "</br></br>");

        }
    }

}



http://www.androidsnippets.com/webview-with-custom-html-and-local-images.html

No comments:

Post a Comment

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...