I'd like to load a website in webview with UTF-8, because I'm having accentuation problems, so I'm trying:
myWebView.loadDataWithBaseURL("http://www.planalto.gov.br/ccivil_03/constituicao/constituicaocompilado.htm", "", "text/html", "UTF-8", null);
but the result is a blank screen.
If I use loadURL("site") it loads, but I have accentuation problems. any ideas?
edit --
my webview setup:
WebView myWebView = findViewById(R.id.webView1);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setDefaultTextEncodingName("utf-8");
myWebView.setWebViewClient(new MyWebViewClient());
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
You will need to use loadUrl to load the link.
loadDataWithBaseUrl is not meant to load the given baseUrl into the WebView. It is meant to load the data that you pass as data into the WebView (documentation). Say you want to have "Hello World!" written in your WebView:
webview.loadDataWithBaseURL(url, "<html><body>Hello World.</body></html>", null, "UTF-8", null)
Regarding the encoding: when using loadUrl you should not need to set UTF-8 explicitly because it is the default. Using ISO-8859-1 worked for the specific page you want to load:
webSettings.defaultTextEncodingName = "ISO-8859-1"
Related
I have an html file in my website:
http://manizani.ir/hamyar_gas_php/map/map_app.html
I want to show it on my Android Application. I created an Activity and use a Webview to load above url. But nothing is loaded and it shows me just an empty activity.
I can load any other web pages successfully, but not about my url.
Please help me to solve it.
Thanks a lot
activity_map_web.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapWebActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<WebView
android:id="#+id/webview_intro"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</WebView>
</LinearLayout>
</RelativeLayout>
//////////////End of file//////////////////
MapWebActivity.java :
public class MapWebActivity extends AppCompatActivity {
WebView WV;
String url = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_web);
try {
WV = (WebView) findViewById(R.id.webview_intro);
WV.getSettings().setLoadsImagesAutomatically(true);
WV.getSettings().setJavaScriptEnabled(true);
WV.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
WV.getSettings().setBuiltInZoomControls(true);
WV.getSettings().setDomStorageEnabled(true);
WV.getSettings().setAppCacheEnabled(true);
WV.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
WV.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
WV.setWebViewClient(new WebViewClient());
url="http://manizani.ir/hamyar_gas_php/map/map_app.html";
WV.loadUrl(url);
} catch (Exception ex) {
Toast.makeText(this, ex.toString(), Toast.LENGTH_SHORT).show();
}
}
}
Mapbox use WebGL and may be is not support on your device.
webview log :
[INFO:CONSOLE(31)] "Error: Failed to initialize WebGL", source: https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.js (31)
more at here
This simple WebView doesn't work no matter what I did. In below code I also added my if condition if it matters.
String URL = "www.google.com.tr";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(isNetworkAvailable(this)) {
Toast.makeText(
MainActivity.this,
"Welcome",
Toast.LENGTH_LONG
).show();
mWebView = (WebView) findViewById(R.id.myWebView);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
mWebView.setVerticalScrollBarEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.loadUrl(URL);
}
else //Not connected
{
Toast.makeText(
MainActivity.this,
"Internet disconnected",
Toast.LENGTH_LONG
).show();
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:showIn="#layout/activity_main">
<WebView
android:id="#+id/myWebView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
This WebView shows text "Welcome" so I think program should show websites without any issue. I am at my wit's end. I even tried changing Layout type, adding ChromeView instead normal web view. And tried to change content view from match_parent to wrap_content. But no luck by doing this.
I have internet permission in my Manifest
You are missing the protocol information in your URL.
Instead of:
String URL = "www.google.com.tr";
Use:
String URL = "https://www.google.com.tr";
I guess you need to set
webSettings.setBlockNetworkLoads(false);
when I run this code, "google.com" open in web application (for examaple google chrome) and not in the webView in the dialog.
It's does work OK with some urls.
Why?
final Dialog dialog=new Dialog(this.activity,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
dialog.setContentView(R.layout.full_page_ad_between_chapters);
WebView webView = (WebView) dialog.findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://google.com");
R.layout.full_page_ad_between_chapters :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="350dp"
android:layout_height="1000dp"
android:layout_margin="5dp"
android:orientation="vertical" >
<Button
android:id="#+id/fullPageAdBetweenChaptersClose"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:text="סגור" />
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
You need to override the method shouldOverrideUrlLoading() on your WebViewClient.
WebView webView = (WebView) dialog.findViewById(R.id.webView1);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// check if we want to open the url into the WebView
if (iWantToOpenTheUrlIntoTheWebView(url)) {
return false;
}
// let the system handle the url outside of the app
return true;
}
});
This question has been asked many times, but none of the answers works for me.
I'm using webview playing video url = "http://youtu.be/gm4wlE04hbI" , the webview does not work and turn blank, it logs
01-30 00:00:31.640: W/AwContents(16221): nativeOnDraw failed; clearing to background color.
My layout is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/layout_base_top_bar" />
<WebView
android:id="#+id/web_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
</WebView>
</LinearLayout>`
And the code
webView = (WebView) findViewById(R.id.web_content);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setPluginState(PluginState.ON);
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webView.setWebChromeClient(new WebChromeClient(){
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
L.e(newProgress + "");
}
});
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
});
String url = getIntent().getStringExtra("url");
webView.loadUrl(url);
And close the hardware
android:hardwareAccelerated="false"
I hope some could tell me what's wrong with it ....
I have a webpage in ASP.NET and now I am creating an Android application using Xamarin through which user can access this webpage. I am using WebView.
Problem I am facing is that the content of webpage is not fitting in WebView, probably it seems a duplicate question but I did not got a proper answer in previous posts.
My layout code is :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="#+id/webview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
and MainActivity code is
namespace DsclQuery
{
[Activity (Label = "DsclQuery", MainLauncher = true, Theme = "#android:style/Theme.NoTitleBar")]
public class MainActivity : Activity
{
WebView webview;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
webview = FindViewById<WebView> (Resource.Id.webview1);
webview.LoadUrl ("http://www.dsclsugar.com/QuerySecure");
webview.SetWebViewClient (new dsclQueryWebViewClient());
webview.Settings.JavaScriptEnabled = true;
webview.Settings.UseWideViewPort = true;
webview.Settings.BuiltInZoomControls = true;
}
}
How can I do this ?
Try This code:
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("your url");
int scale = (int) (100 * webview.getScale());
webview.setInitialScale(scale);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setBuiltInZoomControls(true);
Its android native code, I think you can understand !
I hope it will useful. Thank you.