1
votes
    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(''+document.getElementsByTagName('html')[0].innerHTML+'');");  
        }  
    });  

    /* load a web page */  
    browser.loadUrl("http://lexandera.com/files/jsexamples/gethtml.html");  

In to the above code after



             new AlertDialog.Builder(myApp)  
                .setTitle("HTML")  
                .setMessage(html)  
                .setPositiveButton(android.R.string.ok, null)  
            .setCancelable(false)  
            .create()  
            .show(); 


I want to set visibility of the button true and false but it gives me error does any one have any idea why its happens and have any solution? Thanks in advance

1
Please post the relevant output from logcat into your question. - Jim Blackler
i get the following error:- 04-26 15:40:43.485: INFO/myapp(656): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. - Android

1 Answers

0
votes

Finally I got the solution of the error. I'm using:

btn.post(new Runnable() {
    @Override
    public void run() {
        btn.requestFocus();
        btn.setVisibility(0);
    }
}

And after that I'm starting a new thread when I want to show the button