Update 'src' of image in html

Last Updated:

  1. pinkfloydim

    pinkfloydim Member

    I have this code:

    * Load the Web with Data
    String css = "<html><head><link rel=\"stylesheet\" href=\"app.css\" type=\"text/css\" />";
    * Need Meta Tag for andriod to draw screen width correctly.
    String path = "towns/"+town_img;
    css += "<meta name = \"viewport\" content = \"width=320, user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1\"/>";
    css += "<meta name=\"format-detection\" content=\"telephone=no\" />";
    css += "</head>";
    css += "<body><div class=" + theme_class + ">";
    css += "<br />";
    css += "<div style=\"float:left;padding-top:10px;\"><img src=\"" + path + "\" width=\"45px\" height=\"37px\" /></div>";
    css += "<div style=\"float:left;padding-left:3px;padding-top:8px;\"><span id=\"welcome\">Welcome to</span></div>";
    css += "<div style=\"clear:both;\"></div>";
    css += "<div style=\"float:left;margin-top:-23px;padding-left:48px;\"><span style=\"font-family:Arial;font-size:24;color:'black'\">" + town_name + "</span></div>";
    css += "<div style=\"clear:both;\"></div>";
    css += "<div style=\"float:left;padding-top:5px;\" onClick=\"Android.showToast('Hello there!',"+image_index+");\"><img src=\"" + image_scroll_array[image_index] + "\" width=\"340px\" height=\"151px\" id=\"scroll_image\"/></div>";
    css += "<div style=\"clear:both;\"></div>";
    css += town_desc;
    css += "<br /></div>";
    css += "</body></html>";

    I need to update one of the images, using javascript.

    on the andriod side i Have :

    * Create WebView Object
    WebView townDetail = (WebView) view.findViewById(R.id.webContent);
    WebSettings webSettings = townDetail.getSettings();
    townDetail.addJavascriptInterface(new JSInterface(this), "Android");

    My jSInterface is:

    public class JSInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    JSInterface(Context c) {
    mContext = c;

    /** Show a toast from the web page */
    public void showToast(String toast,int index) {
    Toast.makeText(mContext, toast+":"+index, Toast.LENGTH_SHORT).show();

    }[/HIGH]Can I access the dom object, and update the src attribute, once I add an id?

  2. pinkfloydim

    pinkfloydim Member

    I originally wrote this in Titanium (Appcelerator) , and could access the DOM, so should be a similar feature, or do I need to include this in the js code inside the html in order to do this.

Share This Page