Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how set text color of webview in android

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.

like image 835
Youddh Avatar asked Jun 27 '12 11:06

Youddh


3 Answers

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"); 
like image 85
Furqi Avatar answered Oct 19 '22 07:10

Furqi


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);
like image 38
Faakhir Avatar answered Oct 19 '22 07:10

Faakhir


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); 
like image 28
ρяσѕρєя K Avatar answered Oct 19 '22 07:10

ρяσѕρєя K