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

  • SEARCH
  • Home
  • 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 7976087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:49:39+00:00 2026-06-04T08:49:39+00:00

So ive been trying to find the simplest way to append an image in

  • 0

So ive been trying to find the simplest way to append an image in a webview using javascript. i wrote out the html myself and included my own function that looked something like this:

<script type="text/javascript">

function image(src) {
    var img = document.createElement("IMG");
    img.src = src;
    document.getElementById('image').appendChild(img);
}
</script>

I found out that i cant just use
myview.loadurl(javascript:image(‘line1.png’));
So im trying to figure out how i can do this. I was thinking about relegating this function to one line of code i can fit in loadurl(), but i seem to be getting an error around that as well. Ive heard of using addJavascriptInterface but its seems complex and im not sure if it fits what i want. So whats my best course of action?

  • 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-04T08:49:41+00:00Added an answer on June 4, 2026 at 8:49 am

    That is so simple. Try the following

    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.webkit.WebView;
    
    public class StackOverFlowActivity extends Activity {
    
        private Handler mHandler = new Handler();
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            WebView view=(WebView)findViewById(R.id.webView1);
            view.getSettings().setJavaScriptEnabled(true);
            view.loadUrl("file:///android_asset/index.html");
            view.addJavascriptInterface(new MyJavaScriptInterface(), "Android");
        }
    
        final class MyJavaScriptInterface
        {
            public void ProcessJavaScript(final String scriptname, final String args)
                {             
                    mHandler.post(new Runnable()
                        {
                            public void run()
                                {
                        String url="file:///android_asset/img.jpg";
                                    webview.loadUrl("javascript:image(\""+url+"\")");
                                }
                        });
                }
        }
    }
    

    index.html:

    <script type="text/javascript">
     function getimage()
     {
       Android.ProcessJavaScript("image",null); 
     }
    
    function image(src) {
        var img = document.createElement("IMG");
        img.src = src;
        document.getElementById('image').appendChild(img);
    }
    </script>
    

    and add the following,

    <body onload="getimage()" >
    

    To pass more images:

    function image(src) {
    
            //var img = document.createElement("IMG");
            //img.src = src;
           // document.getElementById('image').appendChild(img);
                images = eval('(' + src+ ')');
                for (i = 0; i < images.length; i++) {    
                var img = document.createElement("IMG");
                img.src = images[i];            
                  document.getElementById('image').appendChild(img);
                }
        }
    

    and you pass String array instead of String in load url like

    final class MyJavaScriptInterface
            {
                public void ProcessJavaScript(final String scriptname, final String args)
                    {             
                        mHandler.post(new Runnable()
                            {
                                public void run()
                                    {
                            //String url="file:///android_asset/img.jpg";
                                        //webview.loadUrl("javascript:image(\""+url+"\")");
                                        ArrayList<String> url=new ArrayList<String>();
                                        url.add("file:///android_asset/img1.jpg");
                                        url.add("file:///android_asset/img2.jpg");
                                        url.add("file:///android_asset/img3.jpg");
                                        webview.loadUrl("javascript:image(\""+url+"\")");
                                    }
                            });
                    }
            }
    

    To call image script more times:

      final class MyJavaScriptInterface
                {
                    public void ProcessJavaScript(final String scriptname, final String args)
                        {             
                            mHandler.post(new Runnable()
                                {
                                    public void run()
                                        {
                                          // you can load url anywhere in this application. check below to call this inside the other function.
                                           callimageFirstTime(scriptname,args);
                                         // check below to loadurl in other class Test.java
                                          Test newtest=new Test();
                                          newtest.methodtoloadurl(scriptname,args);
                                        }
                                });
                        }
                }
    
    public void callimageFirstTime(String script, String arguments)
    {
      // add images with String array and pass here
      webview.loadUrl("javascript:image(\""+url+"\")");
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to find a way to write to a file when using
Ive been trying to sort out output using AWK, and have been pretty successful
Ive been trying to style a submit button using an image. I would use
I've been trying to find a simple way of formatting the output from difftime
I've been trying to find a way to combine 2 strings from a single
I've been trying to find a way to do this but, have not found
I've been trying to find out how to edit the tab width in the
Ive been reading about mysqli multi_query and couldnt find a way to do this
I've been trying to find a more elegant way to do this function but
I've been trying to find a nice neat and succinct way to declare RelayCommands

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.