How to display html iso-8859-1 in Android Webview - android

I am loading an html page in a Webview but for special characters such as ü and ä. I got question marks in place of it. Is there a simple way to solve this or do I have to make it go through an input reader?
public class Termine extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_termine);
WebView terminListe = (WebView) findViewById(R.id.termin_liste);
terminListe.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
});
terminListe.loadUrl("http://www.arsdecora.net/termine.html");
}
}

After a bit of testing around and the comment by Kayaman, I have found out, that setting the html's meta charset to , everything is displayed fine.
Caution: This only works if you have access to the html directly!

Related

My webview is opening the webpage in the default browser

So I just implemented a simple webview application in which i was loading the stackoverflow main page. Earlier it was working just fine but now as I click on some link it opens that link in the default browser. I have implemented and override the shouldoverrideUrlLoading method by creating my custom webViewClient class.
I know that there are various question ask like these but I am writing this question only because they don't work for me.
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith(".com"))
return false;
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
final customEditText editText = findViewById(R.id.urlEditText);
Button button = findViewById(R.id.enterButtonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
webView.loadUrl("https://"+editText.getText().toString().trim().toLowerCase());
}
});
}
You can use this:
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("url");
Just implement the web client and set it before loadUrl. The simplest way is:
WebView.setWebViewClient(new WebViewClient());
So When I was reading a tutorial https://www.journaldev.com/9333/android-webview-example-tutorial in this it is given that when shouldOverrideUrlLoading( ) method provides false then it url opens in our webview and if it returns true, then it will not load the page at all. So I think that earlier my code was working was because I was opening .com extensioned websites but when i open other extension website then it redirect to the default browser.

WebView shouldOverrideUrlLoading() not called for invalid links

There are two types of links in the HTML file:
(1) A normal link like http://www.bbb.com/q?type=normal
(2) A short link like /q?type=short.
For the first kind, just load the url. For the second kind, I should prepend it with a fixed address like http://www.abc.com before loading the url.
I am trying to do this with overriding the shouldOverrideUrlLoading() function in WebViewClient. However this function doesn't gets called for the second type of link. I tried prepending the "http://www.abc.com" to the second type of links in the HTML file. Then the function does get called when I click the second kind of link.
I think what's happening is WebView will first check if the link is a valid url. Only if it is valid will the function gets called. Am I right? How can I solve this? Thanks in advance.
contentWebView = new WebView(context);
webViewClient = new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// String not in Logger.
Log.d(TAG, "Here!");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
context.startActivity(intent);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (hosted) {
contentWebView.setVisibility(VISIBLE);
} else {
summaryTextView.setVisibility(VISIBLE);
articleLinkButton.setVisibility(VISIBLE);
}
progressBar.setVisibility(View.GONE);
}
};
contentWebView.setWebViewClient(webViewClient);
contentWebView.getSettings().setJavaScriptEnabled(true);
contentWebView.loadData(fullString, "text/html", "utf-8");
contentWebView.setVisibility(GONE);
More on this:
I tried changing
contentWebView.loadData(fullString, "text/html", "utf-8");
to
contentWebView.loadDataWithBaseURL("http://www.abc.com", fullString, "text/html", "utf-8", null);
Then the function gets called.
If I change the short link to a full link in the html string manually. Then the function also gets called.
So I think this is probably what is happening: The WebView checks if the link URL is valid. Only when the URL is valid will the shouldOverrideUrlLoading() be called.
You're probably using the KitKat WebView. This is a known issue (I think it's outlined in the migration guide) where URLs that can't be resolved against the base URL are dropped on the floor (you won't get any callbacks for them, neither shouldOverrideUrlLoading nor onPageStarted).
The problem is that your base URL is a data url, so you're trying to resolve '/q?type=short' against 'data:text/html,...' which doesn't make much sense and so the whole attempt to navigate to the URL gets ignored.
This was different for the pre-KK WebView which used KURL instead of GURL for URL processing. GURL is generally more strict (and more secure) than KURL, which is the cause for some incompatibility between the two WebView versions.
Maybe try using onPageStarted method
solution that worked for me was to use loadDataWithBaseURL with an invalid baseUrl and detect that and remove it and replace with "http://" during setWebViewClient
public class MyActivity
extends Activity
{
private static final String badurl = "http://myappname.invalid/";
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
...
WebView wv = ((WebView)findViewById(R.id.webview));
WebSettings settings = wv.getSettings();
settings.setJavaScriptEnabled(false);
settings.setSupportMultipleWindows(true);
wv.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg)
{
handleUrlview.getHitTestResult().getExtra());
return true;
}
});
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
handleUrl(url);
return true;
}
});
wv.loadDataWithBaseURL(badurl,text,"text/html","utf-8",null);
}
private void handleUrl(String url)
{
if (url.startsWith(badurl))
url = "http://"+url.substring(badurl.length());
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
} catch (ActivityNotFoundException e) { }
}
}
i faced this problem too and solved it by replacing my html response. In my html response there is no any host in "href" html tags. Then i replaced it following codes and thats working like a charm now :)
String htmlString = AppCache.homePageResponse.showcaase.replace("href=\"/", "href=\"" + "evidea://" );
I found that if your page runs in an iframe, clicking on external (http://www...) links does NOT trigger shouldOverrideUrlLoading() !
See shouldOverrideUrlLoading() not called for external links from iframe
Try this
private static WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //should be activity_main
webView = (WebView) findViewById(R.id.web);
webView.setWebViewClient(new webviewclient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.yahoo.com");
}
public class webviewclient extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.toString());
return true;
}

When I Embed a website (wordpress blog) on my android app it opens in another browser?

I just start android developing and I want to embed my website in the app.
When I want to open "http://www.google.com" , webpage opens in My app, but when I change address to my blog it wants to open it on external browser.
This is My activity code that I used to embed my site!
public class WebPage extends Activity {
#SuppressLint("SetJavaScriptEnabled") #Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_page);
//web view start
WebView med = (WebView) findViewById(R.id.webView1);
med.getSettings().setJavaScriptEnabled(true);
med.getSettings().
med.loadUrl("http://www.mediratour.com");
}
}
My webpage based on wordpress, I don't know if I have to change settings to prevent using external browser and opens it in My app.
Thanks
// Set this on your web view.
webView.setWebViewClient(new WebClient());
// Create this class.
public class WebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(final WebView webView, final String url) {
webView.loadUrl(url);
return true;
}
}
I think it helps.

how to track an webpage when it is redirected from another web page within a web view in android

I load "www.gmail.com" in a webview,after login the a new webpage will be loaded i.e. our gmail account page.
I have to track that url when I submit login details and the new webpage is loading,I don't need any hard coded value to redirect to any webpage,I want to get that url when a webpage is loaded from another webpage,how can I achieve this.Please help me.
This is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
title_text=(TextView)findViewById(R.id.urltxt);
showWeb=(WebView)findViewById(R.id.webview_details_body);
showWeb.setWebViewClient(new HelloWeb());
showWeb.getSettings().setBuiltInZoomControls(true);
showWeb.getSettings().setLoadWithOverviewMode(true);//show the webpage in fullsize with all info
showWeb.getSettings().setUseWideViewPort(true);
WebSettings webSettings = showWeb.getSettings();
webSettings.setJavaScriptEnabled(true);
showWebClick();
}
private void showWebClick() {
showWeb.loadUrl("http://www.gmail.com/");
}
public boolean onKeyDown(int keyCode,KeyEvent event){
if((keyCode==KeyEvent.ACTION_DOWN)&&showWeb.canGoBack()){
showWeb.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
public class HelloWeb extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView vw,String url){
vw.loadUrl(url);
s=vw.getUrl();
title_text.setText( s);
return super.shouldOverrideUrlLoading(vw, url);
}
}
}
You can achieve this using method getUrl() of webview client.
But, This is not always the same as the URL passed to WebViewClient.onPageStarted because although the load for that URL has begun, the current page may not have changed.
You can refer below link :
http://developer.android.com/reference/android/webkit/WebView.html#getUrl%28%29
So you have to call geturl method on onPageFinished method. It will be good.
#Override
public void onPageFinished(WebView view, String url) {
/*do your stuff here.*/
}
Your webview will most likely call either shouldOverrideUrlLoading or onLoadResource in its webviewclient with an url of its redirect.
If I understand your question correct, try overriding onLoadResource in your WebViewClient and look at the url parameter.
Using onLoadResource will also generate urls for other resources as well, such as images.

WebView will not function with loadUrl but works with loadData

I'm using standard addresses like google and facebook, but loadUrl does nothing, it just sits there at a white screen, but if i pipe html into it using loadData, it works fine. Any ideas or tips? I've got it enabling javascript, and I have this call:
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
TextView t;
t = (TextView)findViewById(R.id.pageTitle);
t.setText(view.getTitle());
}
});
Do i need to override anything else?
Hope this helps.
public class WebViewSampleActivity extends Activity {
WebView wb;
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wb=(WebView)findViewById(R.id.webView1);
wb.getSettings().setJavaScriptEnabled(true);
wb.getSettings().setLoadWithOverviewMode(true);
wb.getSettings().setUseWideViewPort(true);
wb.getSettings().setBuiltInZoomControls(true);
wb.getSettings().setPluginState(WebSettings.PluginState.ON);
wb.getSettings().setPluginsEnabled(true);
wb.setWebViewClient(new HelloWebViewClient());
wb.loadUrl("http://www.foo.com");
}
}
The discussed above are cool and pretty good but I've noticed this thing in newer versions of android studio the method loadUrl() does not work direct through. so we should identify the settings first then set true for JS as like this
wb.getSettings().setJavaScriptEnabled(true);
wb.getSettings().setLoadWithOverviewMode(true);

Categories

Resources