Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6770945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:22:27+00:00 2026-05-26T15:22:27+00:00

hello i’m using this example http://lexandera.com/2009/01/extracting-html-from-a-webview/ to get the HTML from a webview. But

  • 0

hello i’m using this example http://lexandera.com/2009/01/extracting-html-from-a-webview/ to get the HTML from a webview. But i need to use it in my superclass and i dont know how to do this. I just can see the html on a AlertDialog but i cant use it. How can I return it to my main class as String?

final Context myApp = this;  

/* An instance of this class will be registered as a JavaScript interface */  
class MyJavaScriptInterface  
{  
    @SuppressWarnings("unused")  
    public void showHTML(String html)  
    {  
        new AlertDialog.Builder(myApp)  
            .setTitle("HTML")  
            .setMessage(html)  
            .setPositiveButton(android.R.string.ok, null)  
        .setCancelable(false)  
        .create()  
        .show();  
    }  
}  

final WebView browser = (WebView)findViewById(R.id.browser);  
/* JavaScript must be enabled if you want it to work, obviously */  
browser.getSettings().setJavaScriptEnabled(true);  

/* Register a new JavaScript interface called HTMLOUT */  
browser.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");  

/* WebViewClient must be set BEFORE calling loadUrl! */  
browser.setWebViewClient(new WebViewClient() {  
    @Override  
    public void onPageFinished(WebView view, String url)  
    {  
        /* This call inject JavaScript into the page which just finished loading. */  
        browser.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");  
    }  
});  

/* load a web page */  
browser.loadUrl("http://lexandera.com/files/jsexamples/gethtml.html");  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:22:28+00:00Added an answer on May 26, 2026 at 3:22 pm

    Do the Followings :

    Setting up WebView

    First add JavaScriptInterface to webView as follows. Here “Android” will be used later to call functions in JavaScriptInterface later.

      webView = (WebView) findViewById(R.id.webView);
      WebSettings webSettings = webView.getSettings();
      webSettings.setJavaScriptEnabled(true);
      webView.addJavascriptInterface(new JavaScriptInterface(this), "MyAndroid");
    

    The JavaScriptInterface Class:

     public class JavaScriptInterface {
         Context mContext;
         JavaScriptInterface(Context c) {
             mContext = c;
         }
    }
    

    HTML File:

      <html>
        <head>
        <script type="text/javascript">
        function getValue()
          {
            var x=document.getElementById("content").innerHTML;
            //  alert(x);
            MyAndroid.receiveValueFromJs(x);
          }
        </script>
        </head>
        <body>
        <div id="content">
        This is html content <hr/> 
        Other contents;
        <input type="button" onclick="getValue()" value="Get Content" />
        </div>
        </body>
        </html>
    

    Calling Java Script Function

      //this calls javascript function
      webView.loadUrl("javascript:getValue()");
    

    Adding Callback function in JavaScriptInterface for MyAndroid.receiveValueFromJs(val):

    public void receiveValueFromJs(String str) {
      Toast.makeText(mContext, "Received Value from JS: " + str,Toast.LENGTH_SHORT).show();
    }
    

    Here you have returned HTML in str variable.

    You can see my blog : http://ganeshtiwaridotcomdotnp.blogspot.com/2011/10/calling-javascript-function-from.html
    for details.

    I found another solution : Is it possible to get the HTML code from WebView

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello again stackoverflow! So i build this script: <?php $xmlurl = 'http://api.ipinfodb.com/v2/ip_query.php?key=apikey&ip=127.0.0.100&timezone=false'; $xml =
Hello I am compiling a program with make but I get the error of
Hello i been trying to get a tokenizer to work using the boost library
hello i wanted to build I get $date and $date1 from form xxx. I
Hello I have this query... if (isset($_REQUEST['deletePost'])) { $q = $dbc -> prepare(DELETE FROM
Hello I'm trying to take backup from mysql command line client. I'm using mysqldump
Hello again ladies and gents! OK, following on from my other question on ASP.NET
Hello can anybody solve this please I'm creating the object in the action class
Hello I am working with a simulator that uses rcS scripts to boot, this
hello friends i am trying to get a list of user id and want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.