Issue
I want to populate webView with HTML by using the following:
String str = "<html>" +
"<body>" +
"<style> \n" +
"h3 {\"font-family:helvetica,arial,sans-serif;font-size:24px;line-height:1.3;\"} \n" +
"p {\"font-family:helvetica,arial,sans-serif;font-size:12px;line-height:1.3;\"} \n" +
"</style> \n" +
"<h3> Open-Source licenses</h3>\n" +
"<p> A huge thanks from all of us at to the great people behind the great programs listed below.</p>\n" +
"<ul>\n" +
"<li>Asynchronous Http Client for Android</li>\n" +
"<li>Glide</li>\n" +
"<li>Google Play Service</li>\n" +
"<li>Gson</li>\n" +
"<li>Picasso</li>\n" +
"</ul>\n" +
"<hr>" +
"<h3>Asynchronous Http Client for Android</h3>\n" +
"<p><a href=\"https://github.com/android-async-http/android-async-http\">https://github.com/android-async-http/android-async-http</a></p> \n" +
"<p>Unless required by applicable law or agreed to in writing, software<br>distributed under the License is distributed on an "AS IS" BASIS,<br>WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.<br>See the License for the specific language governing permissions and<br>limitations under the License.</p>\n" +
"</body></html>";
wb_OpenSource.loadDataWithBaseURL( null, str, "text/html", "utf-8", null );
However, it seems like the style does not make any difference.
Any way to make it work instead of adding style to each line?
UPDATE:
As was suggest below, I created a .css file that looks as follows:
#topHeader {
font-family:helvetica,arial,sans-serif;font-size:24px;line-height:1.3;
}
#regHeader {
font-family:helvetica,arial,sans-serif;font-size:20px;line-height:1.3;
}
#topText {
font-family:helvetica,arial,sans-serif;font-size:14px;line-height:1.3;
}
#regText {
font-family:helvetica,arial,sans-serif;font-size:12px;line-height:1.3;
}
#links {
font-family:helvetica,arial,sans-serif;font-size:16px;line-height:1.3;
}
And changed my HTML to be:
"<body>" +
"<link rel=\"stylesheet\" href=\"cssStyles.css\"> \n" +
"<h3 id=\"topHeader\"> Open-Source licenses</h3>\n" +
"<p id=\"topText\"> A huge thanks from all of us at to the great people behind the great programs listed below</p>\n" +
"<ul>\n" +
"<li id=\"regText\"> Asynchronous Http Client for Android </li>\n" +
"<li id=\"regText\"> ExpandableTextView Android Arsenal </li>\n" +
and
wb_OpenSource.loadDataWithBaseURL( "file://android_asset/cssStyles.css", str, "text/html", "utf-8", null );
But it doesn't seem to work.
Thank you
Solution
You can create a separate .css
file for styling the elements. Place the .css file in the assets folder and include it in your html code.
Now load the HTML in the webview like below:
webView.loadDataWithBaseURL("file:///android_asset", HTML_STRING, "text/html","utf-8",null);
Answered By - Alpha 1
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.