A
A
Anatoly2015-10-17 08:07:15
Android
Anatoly, 2015-10-17 08:07:15

How to make links work in html loaded in WebView?

Html is loaded from assets, other html, css, script are also located there.

String webData = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webData = getHtmlFromAsset();

        WebView webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setSaveFormData(true);
        webView.addJavascriptInterface(new JavaScriptIntefeise(this), "Android");
        webView.loadDataWithBaseURL(getAssets().toString(), webData, "text/html", "UTF-8", null);
    }

    private String getHtmlFromAsset() {
        InputStream is;
        StringBuilder builder = new StringBuilder();
        String htmlString = null;
        try {
            is = getAssets().open(getString(R.string.context));
            if (is != null) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                String line;
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }

                htmlString = builder.toString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return htmlString;
    }

    class JavaScriptIntefeise {

        Context ctx;

        public JavaScriptIntefeise(Context ctx) {
            this.ctx = ctx;
        }

    }

html structure
<!DOCTYPE html> 
<html>
<head>
<meta charset="utf-8">
<link href="style.css" rel="stylesheet" type="text/css">
<style>
a {
  text-decoration: none; /*подчеркивание ссылок*/
  color: #000000;			/*цвет ссылок*/
}
button {
  min-height: 8mm; /*мин высота пункта */
  width: 100%;		/*ширина пункта */
  text-align: left;	/*выравнивание*/
}
</style>

</head> 
<body> 

<form action="1.html"><button class="odd">I.</button></form>

<form action="2.html"><button class="even">II.</button></form>

<form action="3.html"><button class="odd">III. </button></form>

</body>
</html>

Moreover, the nested style works, but if you nest the script, it does not work.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question