5

i am trying to change text color of webview with this code

String message ="<font color='white'>"+"<u>"+
"text in white"+ "<br>" +
"<font color='cyan'>"+"<font size='2'>"+
" text in blue color "+"</font>";
webview.loadData(message, "text/html", "utf8"); 

but i have some html pages. store in my sdcard then how can i change text color..

i use

webViewRead.loadUrl(url);

url is path of my file.

Youddh
  • 1,511
  • 4
  • 34
  • 51
  • refer this http://stackoverflow.com/questions/3624171/how-to-show-the-html-contents-to-the-webview-using-android – Mohammed Azharuddin Shaikh Jun 27 '12 at 11:44
  • but i want to display html pages not text..wv.loadDataWithBaseURL("", html, mimeType, encoding, ""); in this method where should i put my path(file) plz dis-crib. – Youddh Jun 27 '12 at 11:54
  • If you want to load html file then put the HTML file into the assets folder. Refer this link http://stackoverflow.com/a/10844221/1263679 – Rahul Patel Jun 27 '12 at 12:30

4 Answers4

11

You have to give the path of that file like this.

String extStorageDirectory = Environment.getExternalStorageDirectory()
                .toString() + "/folder_name";

File directory = new File(extStorageDirectory);
File fileInDirectory = new File(directory,file_name.html);

//Read text from file
StringBuilder html_text = new StringBuilder();

try {
    BufferedReader br = new BufferedReader(new FileReader(fileInDirectory));
    String line;

    while ((line = br.readLine()) != null) {
        html_text.append(line);
        html_text.append('\n');
    }
}
catch (IOException e) {
    //You'll need to add proper error handling here
}

then use this html code for edit

String message ="<font color='white'>"+"<u>"+"text in white"+ "<br>" +"<font color='cyan'>"+"<font size='2'>"+" text in blue color "+"</font>"; 
 webview.loadData(message, "text/html", "utf8"); 
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
Furqi
  • 2,403
  • 1
  • 26
  • 32
2
htmlDetail = dbValues.getContent(3);
        tvDescription3.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

        String finalHtml = "<html><head>"
                  + "<style type=\"text/css\">li{color: #00f} span {color: #000}"
                  + "</style></head>"
                  + "<body>"                          
                  + htmlDetail
                  + "</body></html>";

    tvDescription3.loadData(finalHtml, "text/html; charset=UTF-8", null);
Fakhar
  • 3,946
  • 39
  • 35
1

put your file path as

String htmlPath = "file:///mnt/sdcard/test/11.html"; 
String baseUrl = "file:///mnt/sdcard/test/"; 
webView.loadDataWithBaseURL(baseUrl, message, "text/html", "utf-8", null); 
webView.loadUrl(htmlPath); 
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
0

In order to change a WebView’s background color there is a standard way:

mWebView.setBackgroundColor(Color.Black);

In order to change a WebView’s text font color there is no standard way: Either you change the font through the html code, or you do this:

htmlData="<font color='black'>" + htmlData + "</font>";
mWebView.loadDataWithBaseURL(null, htmlData, "text/html", "UTF-8", null);
merdan
  • 1,229
  • 1
  • 11
  • 26