I am getting this html content in Response via the variable: String mresult=
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Lorem Ipsum</title>
</head>
<body style="width:300px; color: #00000; ">
<p><strong> About us</strong> </p>
<p><strong> Lorem Ipsum</strong> is simply dummy text .</p>
<p><strong> Lorem Ipsum</strong> is simply dummy text </p>
<p><strong> Lorem Ipsum</strong> is simply dummy text </p>
</body></html>
It could not load in webview, I have to try to load it like this:
web.loadDataWithBaseURL(null, mresult, "text/html", "UTF-8", null);
still webview displays a Blank white page.
In the xml file:
<WebView
android:id="#+id/wV"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:background="#android:color/transparent"
android:cacheColorHint="#00000000" />
In the Activity:
String mresult="Here my given data";
WebView web = (WebView)findViewById(R.id.wV);
web.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
web.loadData(mresult, "text/html; charset=UTF-8", null);
Use this Html_value in this way:
String html_value = "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"><title>Lorem Ipsum</title></head><body style=\"width:300px; color: #00000; \"><p><strong> About us</strong> </p><p><strong> Lorem Ipsum</strong> is simply dummy text .</p><p><strong> Lorem Ipsum</strong> is simply dummy text </p><p><strong> Lorem Ipsum</strong> is simply dummy text </p></body></html>";
this is used into webview then only it will work because your html tag was not properly closed:
WebView browser = (WebView) findViewById(R.id.sample);
browser.getSettings().setJavaScriptEnabled(true);
browser.loadData(html_value, "text/html", "UTF-8");
output:
wb.loadDataWithBaseURL("", result, "text/html", "UTF-8", "");
Try this code for the WebView:
WebView wv = (WebView) findViewById(R.id.webView);
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
wv.loadData(mresult, "text/html; charset=UTF-8", null);
don't know y still it not loading in webview but given result is display in TextView in Html format like
TextView txtweb = (TextView) findViewById(R.id.txtweb);
txtweb.setText(Html.fromHtml(mresult));
Use this:
webView.loadDataWithBaseURL("file:///android_asset/", htmlParsing, "text/html", "utf-8", null);
webView.getSettings().setAllowFileAccess(true);
in htmlparsing: just pass urs html code.
It works....
Related
I am trying to load some HTML content (it is a hyperlink) in WebView. But when I click that link nothing is opening. But the same link is working in browser or iOS WebView perfectly.
The code I try:
htmlContent.getSettings().setJavaScriptEnabled(true);
htmlContent.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
htmlContent.loadDataWithBaseURL(null, Html.fromHtml(categoryItemsBean.getContent()).toString(), "text/html", "utf-8", null);
Value of categoryItemsBean.getContent().
<p><a title="كتاب مساعدة الأصدقاء"
href="https://www.manhal.com/platform/stories/demo/index.php?storyid=31"
target="_blank">كتاب مساعدة الأصدقاء </a></p>
Using loadData() method for loading html content on webview
Sample
String unencodedHtml ="<html><body>'%23' is the percent code for ‘#‘ </body></html>";
String encodedHtml = Base64.encodeToString(unencodedHtml.getBytes(),Base64.NO_PADDING);
htmlContent.loadData(encodedHtml, "text/html", "base64");
Convert your html content into base64
I wanted to justify the text of a TextView but I could not find any way to do that on the TextView so I have created a WebView.
The code to set the text on my WebView is the following:
WebView webview = (WebView) view.findViewById(R.id.webview);
webview.loadData(getString(R.string.webview), "text/html; charset=utf-8", "utf-8");
webview.setBackgroundColor(Color.TRANSPARENT);
And it works well, the text is being show justified (because I have created a body tag with style="text-align:justify;).
The problem is that, as I have loaded the text into the WebView, it spends some seconds to charge the text. Therefore, the rest of the layout is being shown before the text have appeared, making a strange visual effect.
I have tried to wait until the WebView is fully loaded (How to listen for a WebView finishing loading a URL?) but the text is never shown. Here is the code that I have by the moment:
webview.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
webview.loadData(getString(R.string.webview), "text/html; charset=utf-8", "utf-8");
webview.setBackgroundColor(Color.TRANSPARENT);
}
});
So, how can I show a justify text at the same time as the rest of the layout?
Thanks in advance!
this is the picture:
this is the code that I used in my project
it doesnt take so much to load
void initVebView(WebView wvContent_Detail, String body) {
String HTML1 = "<html><head><style type=\"text/css\">#font-face {font-family: MyFont;src: url(\"file:///android_asset/%s\")}body {font-family: MyFont;font-size: medium;text-align: justify; line-height: %spx;}</style></head><body dir='rtl'>";
String HTML2 = "</body></html>";
String HTML3 = "<span style=\"font-weight:bold\">%s<br/><br/>%s</span>";
String HTML4 = "<style>img{display: inline;height: auto;max-width: 100%;</style>";
String str = String.format(HTML1, "IRANSansMobile_UltraLight.ttf", 25);
String content = body.replace("text-align:", "");
content = content.replace("font-family:", "");
content = content.replace("line-height:", "");
content = content.replace("dir=", "");
content = content.replace("width=", "width=\"100%;\"");
String myHtmlString = str + content + HTML2;
wvContent_Detail.loadDataWithBaseURL(null, HTML4 + myHtmlString, "text/html", "UTF-8", null);
WebSettings webSettings = wvContent_Detail.getSettings();
webSettings.setDefaultFontSize(20);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
}
this is the xml :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="ir.tenthwindow.BaseModule.ui.FragmentVideo">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webview_product_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
</ScrollView>
</FrameLayout>
you can use your font instead.
Hope this helped .
Webview wv=(WebView)findViewById(R.id.webview1);
``String str="<html>
<body>
<img src='file:///android_asset/nag.png' height='300' width='200'/>
</body>
</html>";
wv.loadData(str,"text/html","UTF-8");
Try using this code
wv.loadDataWithBaseURL("file:///android_asset/", str, "text/html", "UTF-8",null);
Instead of
wv.loadData(str,"text/html","UTF-8");
I'm loading an html asset page into a WebView using
webMain.loadUrl("file:///android_asset/record.html");
which works fine, but inside the html are a number of places where I'd like to use information from the app. For instance, the HTML may contain text that reads "[Custom]". Is there a way I can replace that word with information passed from the application?
This is an old and already accepted question, however I am sure that the problem can be solved in more elegant way by using javascript.
Keep the html file in your assets folder and surround the text which you want to replace into with div elements with unique id's.
<html>
<head> ... <head>
<body>
Static text
<div id="replace1">replace me</div>
<div id="replace2">replace me too</div>
More static text ...
</body>
</html>
Now create a javascript function which will replace the innerHtml of a div with an id:
function replace(id, newContent)
{
document.getElementById(id).innerHTML = newContent;
}
This function will be best placed directly in the html file, update the <head> section to look like this:
<head>
...
<script type="text/javascript">
function replace(id, newContent)
{
document.getElementById(id).innerHTML = newContent;
}
</script>
</head>
Now we need to call the javascript function from from the WebView Android api:
WebView helpView = (WebView)findViewById(R.id.helpView);
helpView.getSettings().setJavaScriptEnabled(true);
helpView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.loadUrl("javascript:replace('replace1', 'new content 1')");
view.loadUrl("javascript:replace('replace2', 'new content 2')");
}
});
helpView.loadUrl("file:///android_asset/help.html");
Using this you will avoid reading potentially large data into memory and running expensive operations on it unnecessarily.
This is worked for me.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Payment Demo</title>
</head>
<body>
<div>
<input type="text" id="uname " name="uname " value="">
<input type="text" id="pass" name="pass" value="">
</div>
</body>
</html>
This is java code.
WebView wb = (WebView) findViewById(R.id.webView1);
wb.loadUrl("file:///android_asset/web1.html");
wb.getSettings().setJavaScriptEnabled(true);
wb.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView web, String url) {
// TODO Auto-generated method stub
String uname = "email#mail.com";
String pass = "******";
web.loadUrl("javascript:(function(){document.getElementById('uname').value = '"+uname+"';})()");
web.loadUrl("javascript:(function(){document.getElementById('pass').value = '"+pass+"';})()");
}
});
Actually I do not understand why the file size of record.html will affect maintainence of the code. Read the html string (using Java reader class or what ever) from the html file in asset, use replaceAll function with Regex to replace all the [Custom] in the html file. How long the html is should not really affect how you maintain the code. It should rather be a performance problem, or the string is really really long that exceeds the java String limit.
some code I have used before :
InputStream is = getApplicationContext().getAssets().open("details/product_jsmodify.html");
Reader r = new InputStreamReader(is);
String details = Utils.readertoString(r);
details = details.replace("%product_name%",productName );
Utils is my class doing the conversion to string. I am not using Regex here as I am only replacing word for once. Then I load the string like Cata does. It is quite clean I suppose.
Yes you can do that by loading your page in a String and then load that string in your WebView.
Eg:
String summary = "<html><body>You scored <b>192</b> points.</body></html>";
webview.loadData(summary, "text/html", null);
Taken from here
This one worked for me, with the html along with the text and images.
InputStream is = getAssets().open(html_name);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
String str = new String(buffer);
str = str.replace("InitialTextToBeReplaced", "TextAfterReplacement");
//Now instead of webview.loadURL(""), I needed to do something like -
webView.loadDataWithBaseURL("file:///android_asset/", str, "text/html", "UTF-8",null);
I have an HTML form inside a WebView. The form control looks like a regular button, but actually it is an html file with a transparent background. When I press the HTML submit button, I want the result to open in the browser, not in the webview. Currently it is opening in the current WebView which is wrecking my layout.
How do I do this?
Here is my Form
<string name="googlecheckout_html_button">
<![CDATA[
<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">
<head>
<title>Google Checkout Button</title>
<script type=\"text/javascript\" language=\"javascript\">
function pass() {
return javaInterface.checkboxPass();
}
</script>
</head>
<body>
<form style=\"width: 121px;margin-left: auto;margin-right: auto;\" onsubmit=\"return pass();\" action=\"https://%1$sapi/checkout/v2/checkoutForm/Merchant/%2$s\" id=\"BB_BuyButtonForm\" method=\"post\" name=\"BB_BuyButtonForm\">
<input name=\"item_name_1\" type=\"hidden\" value=\"%3$s\" />
<input name=\"item_description_1\" type=\"hidden\" value=\"%3$s\" />
<input name=\"item_quantity_1\" type=\"hidden\" value=\"1\" />
<input name=\"item_price_1\" type=\"hidden\" value=\"%4$s\" />
<input name=\"item_currency_1\" type=\"hidden\" value=\"%5$s\" />
<input name=\"_charset_\" type=\"hidden\" value=\"utf-8\" />
<input type=\"hidden\" name=\"shopping-cart.items.item-1.merchant-private-item-data\" value=\"%6$s\" />
<input alt=\"Pay With Google Checkout\" src=\"https://%1$sbuttons/buy.gif?merchant_id=%2$s&w=121&h=44&style=trans&variant=text&loc=en_US\" type=\"image\" />
</form>
</body>
</html>
]]>
</string>
I am then using String.format(arg...) to populate the fields with appropriate values.
Here is by WebView
String form = String.format(getString(R.string.googlecheckout_html_button),
host,
merchantId,
item_name_1,
item_price_1,
item_currency_1,
private_item_data
);
if(Logging.DEBUG) Log.d(TAG, form);
browser = new WebView(ActivityActivate.this);
browser.setBackgroundColor(0);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
browser.getSettings().setSupportZoom(true);
browser.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view,
String url) {
return false;
}
});
//browser.getSettings().setLoadsImagesAutomatically(true);
browser.addJavascriptInterface(new JavascriptInterface(), "javaInterface");
//browser.loadData(header+formData+footer, "text/html", "UTF-8");
browser.loadDataWithBaseURL("https://checkout.google.com", form, "text/html", "UTF-8", null);
llPaymentButtons.addView(browser);
Forms are submitted to servers not to browsers. Make sure you have a correct URL where you are submitting the form.
To solve your problem you must override WebViewClient.shouldOverrideUrlLoading(..):
webview.setWebViewClient( new WebViewClient() {
public void shouldOverrideUrlLoading (WebView view, String url) {
return false;
}
});