Blog : How to pass html string to webview on android

How to pass html string to webview on android


Hi I am parsing xml and then loading it to web view, after parsing I am creating four strings so that I could append all string to one view. I am able to get two views on the web view but not the first two strings.

Pls suggest me with my code, where am I going wrong and what is the correct way to get the formatted html strings on the web view. Pls have a look at my code and help me solve this issue.

@Override
  public View getView(int position, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
  String chapterTitle = "";
  String SubChapterTitle="";
  String chapterIntro ="";
  String chapterContent="";
  View view = convertView;
  if (convertView == null) {
  // view = inflater.inflate(resourceid, null);
  view = getLayoutInflater().inflate(R.layout.webviewitem, null);
  }
  synchronized (view) {
  WebView wv = (WebView) view.findViewById(R.id.contentWebView);

  WebSettings settings = wv.getSettings();
  settings.setUseWideViewPort(true);
  settings.setLoadWithOverviewMode(true);
  settings.setJavaScriptEnabled(true);
  settings.setDefaultZoom(ZoomDensity.FAR);
  // wv.setBackgroundColor(0);
  wv.setVerticalScrollBarEnabled(false);
  wv.setHorizontalScrollBarEnabled(false);
  /*String txtChapTitle = Intro.book.getsecretList().get(position)
  .getChtitle().toString();*/

  if (!(Intro.book.getsecretList().get(position).getChtitle()
  .toString().equals(""))){
  chapterTitle = ""+Intro.book.getsecretList().get(position)
  .getChtitle().toString()+"
";
  }
  if (!(Intro.book.getsecretList().get(position)
  .getSubtitle() == null)) {
  SubChapterTitle = ""+Intro.book.getsecretList().get(position)
  .getSubtitle().toString()+"
";
  }
  if (!(Intro.book.getsecretList().get(position)
  .getIntro() == null)) {
  chapterIntro = ""+Intro.book.getsecretList().get(position)
  .getIntro().toString()+"
";
  }
  if (!(Intro.book.getsecretList().get(position)
  .getContent() == null)) {
  chapterContent = ""+Intro.book.getsecretList().get(position)
  .getContent().toString()+"";
  }

  StringBuilder content = new StringBuilder();
  content.append(chapterTitle+SubChapterTitle+chapterIntro+chapterContent);

  JsInterface Jsi = new JsInterface();
  Jsi.wordDef = content ;
  Log.v("Content", "" +content);
  wv.addJavascriptInterface(Jsi, "interfaces");

  wv.setWebViewClient(new WebViewClient() {
  @Override
  public void onPageFinished(WebView view, String url) {
  view.setHapticFeedbackEnabled(false);
  }
  });

  wv.setWebChromeClient(new WebChromeClient() {
  @Override
  public boolean onJsAlert(WebView view, String url,
  String message, JsResult result) {
  return super.onJsAlert(view, url, message, result);
  }
  });

  wv.loadUrl("file:///android_asset/wordview.html");
  }
  return view;
  }
}I am able to get chapterIntro and chaptercontent on the web view but not the first two strings pls help me friends

i have successfully done by below line

 //data == html data which you want to load
 WebView webview = (WebView)this.findViewById(R.id.webview);
 webview.getSettings().setJavaScriptEnabled(true);
 webview.loadDataWithBaseURL("", data, "text/html", "UTF-8", "");


To load your data in WebView. Call loadData() method of WebView

wv.loadData(yourData, "text/html", "UTF-8");You can check this example

http://developer.android.com/reference/android/webkit/WebView.html

Passing null would be better. The full codes is like:

WebView wv = (WebView)this.findViewById(R.id.myWebView);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadDataWithBaseURL(null, "...", "text/html", "utf-8", null);

Hi Change Title creation string to:

if (!(Intro.book.getsecretList().get(position).getChtitle()
  .toString().equals(""))){
  chapterTitle = ""+Intro.book.getsecretList().get(position)
  .getChtitle().toString()+"
";
}