12

I am using WebView to show websites inside my application, and I was wondering is it possible to change link color and style which is by default blue and underlined?

I searched about it but there are all questions and solutions about removing the highlight around the link, nothing about the link itself. I just want to know is there some solution as easy as TextView's android:textColorLink or will I need to alter the website body somehow?

Thanks!

ecem
  • 3,574
  • 3
  • 27
  • 40
  • 1
    Possible duplicate of: http://stackoverflow.com/questions/11225296/how-set-text-color-of-webview-in-android or http://stackoverflow.com/questions/3376721/can-i-remove-android-default-link-styling-in-webview – Luis Mendo Sep 19 '13 at 20:41
  • The second one is about the highlight thing, and like I stated in the question, I am not concerned about the highlight around the link. The first one is building html by its own as far as I understood, so it is not my case either. – ecem Sep 19 '13 at 20:46
  • 1
    I think you will have to alter the website. You should be able to add in some style like `a{color: red;}` to change all of the links to red. If you don't want it to be that way on the page itself you could try pulling the html in your app, slipping that in with some ` – FoamyGuy Sep 22 '13 at 22:41
  • I was just trying that actually, created a css file and now I'm trying to load the page with some style for links, no success so far :) – ecem Sep 22 '13 at 22:49
  • oh that worked! I managed to load the page with a custom css file, this is the working way then. – ecem Sep 22 '13 at 22:56

2 Answers2

20

Okay, I managed to do it and wanted to share my way for the future visitors.

First I created a css file with the desired style, named style.css, saved under assets folder

a {color:purple; text-decoration:none}

Then, in the code I loaded the page as follows where content is the actual html content of the page

String htmlBody = "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />" + content;
mWebView.loadDataWithBaseURL("file:///android_asset/", htmlBody, "text/html", "utf-8",
            null);

That's it! Hope it helps somebody.

ecem
  • 3,574
  • 3
  • 27
  • 40
  • 4
    Thanks ecem, I used your solution but doing it inline by appending "" to htmlBody – Fraser Apr 23 '15 at 22:14
  • 3
    don't forget to add "a" before {color. I thought it is a misprint but actully it indicates that color should be applied to links only. – nutella_eater Apr 07 '17 at 08:52
  • Thanks for this answer , I try to use this code but this nit help full. can you give me some other solution ? – Pans Mar 03 '18 at 07:48
3

Here is an example of how to add html links and customize them:

WebSettings webViewSettings = webView.getSettings();
webViewSettings.setDefaultFontSize(AppSettings.defaultFontSize);
webView.setBackgroundColor(Color.TRANSPARENT);

webView.loadDataWithBaseURL("file:///android_asset",  Util.replaceLinkTags(myText), "text/html", "utf-8", null);
webView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith("file")) {
            Intent intent = new Intent(MyActivity.this, MyActivity.class);
            intent.putExtra("word", Uri.parse(url).getLastPathSegment());
            startActivity(intent);
            return true;
        } else
            return false;
    }
});

public static String replaceLinkTags(String text) {
    text = "<html><head>"
            + "<style type=\"text/css\">body{color:" + "#424242" + ";} a{color:#00B8D4; text-decoration:none; font-weight:bold;}" 
            + "</style></head>"
            + "<body>" + text + "</body></html>";

    String str;
    while ((text.indexOf("\u0082") > 0)) {
        if ((text.indexOf("\u0082") > 0) && (text.indexOf("\u0083") > 0)) {
            str = text.substring(text.indexOf("\u0082") + 1, text.indexOf("\u0083"));
            text = text.replaceAll("\u0082" + str + "\u0083", "<a href=\"" + str + "\">" + str + "</a>");
        }
    }    
    return text;
}
live-love
  • 48,840
  • 22
  • 240
  • 204
  • Thanks for sharing this answer, solutions working fine for me and very kind of you for this question @ecem. – MohanRaj S Mar 27 '19 at 06:25