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 ....
Related
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"
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);
I need to scroll and zoom webView horizontally, because i have a big canvas with ticket sales, and scroll isn't working on devise lower than 5.0, but i need to support versions 4.1+.
Here's my webView
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.setVerticalScrollBarEnabled(true);
webView.setHorizontalScrollBarEnabled(true);
webView.getSettings().setAppCacheEnabled(false);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
webView.addJavascriptInterface(new MyJavaScriptInterface(getContext()), "HtmlViewer");
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
// webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
if (Build.VERSION.SDK_INT >= 19) {
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
else {
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
// ((GiftsActivity)getActivity()).dismissSpinner();
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
// view.reload();
return false;
}
#Override
public void onPageFinished(WebView view, String url) {
webView.loadUrl("javascript:HtmlViewer.showHTML" +
"(document.getElementsByTagName('pre')[0].innerHTML);");
}
});
webView.loadUrl(url);
((GiftsActivity)getActivity()).dismissSpinner();
}
and xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView android:id="#+id/webViewPosterPlaces"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" />
</LinearLayout>
I tried to change layout to relative and changing orientation to horisontal, also i tried to put webview inside scrollview, but it won't works too.
By the way when i zoom in web view i also can't scroll horizontal.
vertical scroll works properly as a Zoom in, but zoom out don't work too
PROBLEM RESOLVED:
Need to remove this line: webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
Need to remove this line: webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN)
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 already has answers here:
open pdf in popup android
(2 answers)
Closed 9 years ago.
Iam developing an android app.I need to show my pdf file(which contains only one page) in popup window.here Iam starting new activity from popup.i have changed its theme in manifest.xml as:
<activity
android:name="com.example.myapp.Label"
android:label="#string/title_activity_label"
android:theme="#android:style/Theme.Dialog" >
</activity>
code of my Label.java is:
public class Label extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b=getIntent().getExtras();
String pdfurl=b.getString("url");
Boolean dilg=b.getBoolean("isDialog");
final String googleDocsUrl = "http://docs.google.com/viewer?url=";
WebView mWebView=new WebView(Label.this);
// mWebView.getSettings().setJavaScriptEnabled(true);
WebSettings webSettings = mWebView.getSettings();
webSettings.setPluginState(PluginState.ON);
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return false; // then it is not handled by default action
}
});
mWebView.loadUrl((googleDocsUrl + pdfurl));
setContentView(mWebView);
}
}
My android version is:4.2
It is opening new activity in pop-up but not opening PDF.Is there any mistake in my code?
You need to display a custom dialog with webview.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<WebView
android:id="#+id/webview"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
Display a dialog.
Dialog dialog = new Dialog(Activity.this);
dialog.setContentView(R.layout.web_dialog)
WebView wb = (WebView) dialog.findViewById(R.id.webview);
wb.getSettings().setJavaScriptEnabled(true);
WebSettings webSettings = wv.getSettings();
webSettings.setPluginState(PluginState.ON);
wb.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(image_urlpdf);
return false; // then it is not handled by default action
}
});
wb.loadUrl((googleDocsUrl + image_urlpdf));
dialog.setCancelable(true);
dialog.setTitle("WebView");
dialog.show();