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 7794811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:53:06+00:00 2026-06-01T22:53:06+00:00

I have an activity with a button and an image and a webview. My

  • 0

I have an activity with a button and an image and a webview.
My goal is to load a page, then execute javascript on it to draw graphics using HTML5 etc.

The height of the image drawn by the html5 is unknown/variable , but the width should always be the same as the phone’s width/or browser window.

I have a javascript interface to get a callback containing info that it has now finished calling the drawing function, and it is giving me the height.

Here is the setup of the web-view

mWeb = (WebView) findViewById(R.id.webView1);
mWeb.clearCache(true);
mWeb.addJavascriptInterface(new IJavascriptHandler(), "cpjs");

mWeb.getSettings().setJavaScriptEnabled(true);

mWeb.loadUrl(" http://theURL.com ");
mWeb.setInitialScale(100);
mWeb.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);


mWeb.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url){
                mWidth = mWeb.getWidth();
                mHeight = mWeb.getHeight();

                view.loadUrl("javascript: Tools.adjustWidth(" + mWidth + " )");

                String javaCouponRender = "renderCoupon(" + kupongJson + ", 0); void(0)";
                view.loadUrl("javascript: " + javaCouponRender);

            }
             });



        }

And the button I have in my view, I have used to initiate render my webview to a texture with this function:

void renderCoupon(int width,int height){


    bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    final Canvas c =new Canvas(bitmap);
    mWeb.draw(c);
    imgView.setMinimumWidth(width);
    imgView.setMinimumHeight(height);
    imgView.setImageBitmap(bitmap);
    imgView.setVisibility(View.VISIBLE);
}

This works. Pressing the button, renders the drawn content to the texture and the imageview has a correct result. The only cavaet is I have to wait until the page has loaded/drawn before pressing the button.

Now, I have implemented the callback I get from javascript like this:

final class IJavascriptHandler {
       IJavascriptHandler() {
       }

       public void couponRenderedCallback(final int height){

           mHasGotRenderCallback = true;
           mHeight = height;
              Toast t = Toast.makeText(getApplicationContext(), "see if it helps sleeping", 2000);
              t.show();
              try{
                Thread.sleep(5000);
            }
            catch(InterruptedException ex){

            }
              runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        Toast t2 = Toast.makeText(getApplicationContext(), "and now we render to texture", 2000);
                        t2.show();       
                        renderCoupon(mWidth, mHeight);
                    }
                });

       }

    }

And every time, this callback function renders a white page. The funny thing is that if I physically press the render-to-texture button as soon as I see this callback is called, it shows the expected result. Is it runOnUiThread that is failing somehow?

The sleep is something I added to see if there was some sort of funny delay of the webview rendering and the callback I get from javascript.

I have also tried moving the sleep into the runOnUiThread , which also did not help.

I hope someone knows in what direction I should look to solve this problem. Any help is appreciated.

  • 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-06-01T22:53:07+00:00Added an answer on June 1, 2026 at 10:53 pm

    Found a solution, which was to change the renderCoupon to this:

    void renderCoupon(final int width,final int height){
    
        mWeb.postDelayed(new Runnable() {
    
            @Override
            public void run() {
                // TODO Auto-generated method stub
                bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
                final Canvas c =new Canvas(bitmap);
                mWeb.draw(c);
    
                imgView.setMinimumWidth(width);
                imgView.setMinimumHeight(height);
                imgView.setImageBitmap(bitmap);
                imgView.setVisibility(View.VISIBLE);
            }
        }, 100);
    }
    

    And of course I removed the Sleep stuff in my callback. Thanks to my collegue who tipped me about this. 🙂

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

Sidebar

Related Questions

I have an activity which have a button and a listview(with chechbox, image and
What i'm trying to do is just simply have the button load an image
friends, i have a page on which i am displaying image from gallery then
I have button of pick image from android gallery in one activity name DrawingActivity.
I have 3 activities. 1st activity has a 'go right' image button. (to move
I have an Activity A which has a "down" button (see image above). When
A button in my WebView is used to go back using the history.back() JavaScript
I have an activity with a grid of 3x4 Button. See the image below.
I am developing an activity which have an Update button as menu for android.
So I have my main activity, and when you press a button it launches

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.