Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to show the html contents to the webview using android

Following is my html content which i want to show in the webview using android sdk. It will displays only

//Please

But when I put this HTML content into the browser then it shows differently.

<br /><br />Read the handouts please for tomorrow.<br /><br /><!--homework help homework   help help with homework homework assignments elementary school high school middle school    // --><font color="#60c000" size="4"><strong>Please!</strong></font> 

Please suggest how to resolve this problem

I have another problem that in HTML content there is a tag

<img src="http://www.homeworknow.com/hwnow/upload/images/tn_star300.gif" border="0" /> 

in this images does not shows.

like image 809
Amit Thaper Avatar asked Sep 02 '10 05:09

Amit Thaper


People also ask

How do I display HTML content in WebView?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken web view to show html content.

Which method is used to load HTML content in WebView?

The loadUrl() and loadData() methods of Android WebView class are used to load and display web page.

Is it possible to get data from HTML forms into android while WebView?

Yes you can, you can use javascript to get webpage content. Then use the webview jsInterface to return the content to you java code.


2 Answers

  1. Use web.loadDataWithBaseURL instead of web.loadData (And don't forget to escape strings where it's needed)
  2. You need to add internet permission to download images and view them in your manifest file.

This example works for me:

public class SimpleMusicStream extends Activity {     @Override     public void onCreate(Bundle icicle) {         super.onCreate(icicle);         setContentView(R.layout.main);          WebView wv = (WebView) findViewById(R.id.WebView01);                  final String mimeType = "text/html";         final String encoding = "UTF-8";         String html = "<br /><br />Read the handouts please for tomorrow.<br /><br /><!--homework help homework" +                 "help help with homework homework assignments elementary school high school middle school" +                 "// --><font color='#60c000' size='4'><strong>Please!</strong></font>" +                 "<img src='http://www.homeworknow.com/hwnow/upload/images/tn_star300.gif'  />";           wv.loadDataWithBaseURL("", html, mimeType, encoding, "");     }  } 

And don't forget to add:

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 

in your AndroidManifest.xml file

like image 143
droidgren Avatar answered Sep 20 '22 15:09

droidgren


Either you can do it as above or put html file into the asset folder and the use it loke this to display it

view.loadUrl("file:///android_asset/FILENAME.html"); 
like image 25
Android Avatar answered Sep 18 '22 15:09

Android