• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Update 'src' of image in html

pinkfloydim

Lurker
Dec 28, 2012
9
0
I have this code:

[HIGH]/*
* 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>";
mDbHelper.close();

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();
webSettings.setJavaScriptEnabled(true);
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 */
@JavascriptInterface
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?
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones