I am using the HTML5 video element for playing video in the Android WebView. And this works great for me but the only problem with using this is that the video element automatically a gray play button adds.
I've tried searching for an API and could not find anything that helps my case. I also tried using CSS with the following style:
video.mobile_controls::-webkit-media-controls-fullscreen-button
{
display: inline !important; // Also used "display:none"
}
Further i tried poking in the shadow dom but i couldn't find anything related to this.
So the question is how do i remove this gray button.
Here is an image for reference:
The issue is the video poster. But there's a better way of fixing this by extending from the WebChromeClient and overriding the getDefaultVideoPoster();
Here is the solution:
import android.graphics.Bitmap;
import android.webkit.WebChromeClient;
public class WebChromeClientCustomPoster extends WebChromeClient {
#Override
public Bitmap getDefaultVideoPoster() {
return Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888);
}
}
And then using this client instead by doing:
WebChromeClientCustomPoster chromeClient = new WebChromeClientCustomPoster();
mWebView.setWebChromeClient(chromeClient);
After some dirty hacks we found that misusing the poster attribute fixed this issue. We resolved this issue by doing the following:
videoElement.setAttribute("poster", "nope");
The video element will use the value "nope" as its poster. And because nope is not a valid URL the video element will not replace the poster and will not show a poster.
Our team had tried to solve the same issue for a while too. What ultimately worked for us is setting this setting on the WebView webView.settings.mediaPlaybackRequiresUserGesture = false. Hope it helps others running into this too.
How do I remove the overlay play icon (triangle) that's visible in the center of the video for a second or so every time a video starts playing in an Android WebView?
Thanks in advance!
If you said about this picture
This is picture I had when tested my app on Android 6.0.
You can hide this picture. For example:
WebView mWebView = (WebView) findViewById(R.id.web_view);
mWebView.setWebChromeClient(new WebChromeClientCustomPoster());
Chrome client class:
private class WebChromeClientCustomPoster extends WebChromeClient {
#Override
public Bitmap getDefaultVideoPoster() {
return Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888);
}
}
More info read api
I found a solution.
Just add the 'poster' attribute.
e.g. poster="https://via.placeholder.com/1x1" or poster="noposter"
Note: Empty value are ignored. (poster="")
Neither poster="noposter" nor poster="null" work for me.
I made it work by creating a placeholder image that has just a white background color and assigned it to the video per DOM.
document.getElementById("myVideo").poster = "noposter.png";
Please note that setting poster to an invalid URL (such as "noposter") might trigger a network call and an error event from the video player.
In my projects, I'm setting the poster to a tiny transparent GIF:
poster="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
i would like to load an animated gif. I found out that i could load it in a WebView. Now i have created a class extending from WebView. It looks like this:
public class GifWebView extends WebView {
public GifWebView(Context context, String path) {
super(context);
loadUrl(path);
setBackgroundColor(Color.TRANSPARENT);
setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
}
}
Unfortunately the background is black. Do you know a solution? I tried it on Android 4.1.2. I have already searched on stackoverflow but I did not find a way to solve the problem.
You will need to change the background of the document you are providing. Since webview loads the document above its background
I'm struggling to create a WebView with transparent background.
webView.setBackgroundColor(0x00FFFFFF);
webView.setBackgroundDrawable(myDrawable);
Then I load a html page with
<body style="background-color:transparent;" ...
The background color of the WebView is transparent but as soon as the page is loaded, it's overwritten by a black background from the html page. This only happens on android 2.2, it works on android 2.1.
So is there something to add in the html page code to make it really transparent ?
This worked for me,
mWebView.setBackgroundColor(Color.TRANSPARENT);
At the bottom of this earlier mentioned issue there is an solution.
It's a combination of 2 solutions.
webView.setBackgroundColor(Color.TRANSPARENT);
webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
When adding this code to the WebViewer after loading the url, it works (API 11+).
It even works when hardeware acceleration is ON
I had the same issue with 2.2 and also in 2.3. I solved the problem by giving the alpa value in html not in android. I tried many things and what I found out is setBackgroundColor(); color doesnt work with alpha value. webView.setBackgroundColor(Color.argb(128, 0, 0, 0)); will not work.
so here is my solution, worked for me.
String webData = StringHelper.addSlashes("<!DOCTYPE html><head> <meta http-equiv=\"Content-Type\" " +
"content=\"text/html; charset=utf-8\"> </head><body><div style=\"background-color: rgba(10,10,10,0.5); " +
"padding: 20px; height: 260px; border-radius: 8px;\"> $$$ Content Goes Here ! $$$ </div> </body></html>");
And in Java,
webView = (WebView) findViewById(R.id.webview);
webView.setBackgroundColor(0);
webView.loadData(webData, "text/html", "UTF-8");
And here is the Output screenshot below.
Actually it's a bug and nobody found a workaround so far. An issue has been created. The bug is still here in honeycomb.
Please star it if you think it's important : http://code.google.com/p/android/issues/detail?id=14749
This is how you do it:
First make your project base on 11, but in AndroidManifest set minSdkVersion to 8
android:hardwareAccelerated="false" is unnecessary, and it's incompatible with 8
wv.setBackgroundColor(0x00000000);
if (Build.VERSION.SDK_INT >= 11) wv.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
this.wv.setWebViewClient(new WebViewClient()
{
#Override
public void onPageFinished(WebView view, String url)
{
wv.setBackgroundColor(0x00000000);
if (Build.VERSION.SDK_INT >= 11) wv.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
}
});
For safety put this in your style:
BODY, HTML {background: transparent}
worked for me on 2.2 and 4
The most important thing was not mentioned.
The html must have a body tag with background-color set to transparent.
So the full solution would be:
HTML
<body style="display: flex; background-color:transparent">some content</body>
Activity
WebView wv = (WebView) findViewById(R.id.webView);
wv.setBackgroundColor(0);
wv.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
wv.loadUrl("file:///android_asset/myview.html");
below code works fine Android 3.0+ but when you try this code below android 3.0 then your app forcefully closed.
webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
You try below code on your less then API 11.
webview.setBackgroundColor(Color.parseColor("#919191"));
Or
you can also try below code which works on all API fine.
webview.setBackgroundColor(Color.parseColor("#919191"));
if (Build.VERSION.SDK_INT >= 11) {
webview.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
}
above code use full for me.
Try
webView.setBackgroundColor(0);
Following code work for me, though i have multiple webviews and scrolling between them is bit sluggish.
v.setBackgroundColor(Color.TRANSPARENT);
Paint p = new Paint();
v.setLayerType(LAYER_TYPE_SOFTWARE, p);
Use this
WebView myWebView = (WebView) findViewById(R.id.my_web);
myWebView.setBackgroundColor(0);
After trying everything given above. I found it doesn't matter either you specify
webView.setBackgroundColor(Color.TRANSPARENT) before or after loadUrl() /loadData().
The thing that matters is you should explicitly declare android:hardwareAccelerated="false" in the manifest.
Tested on IceCream Sandwich
Just use these lines .....
webView.loadDataWithBaseURL(null,"Hello", "text/html", "utf-8", null);
webView.setBackgroundColor(0x00000000);
And remember a point that Always set background color after loading data in webview.
webView.setBackgroundColor(0x00000000);
webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
this will definitely work..
set background in XML with Editbackground.
Now that background will be shown
This didn't work,
android:background="#android:color/transparent"
Setting the webview background color as worked
webView.setBackgroundColor(0)
Additionally, I set window background drawable as transparent
set the bg after loading the html(from quick tests it seems loading the html resets the bg color.. this is for 2.3).
if you're loading the html from data you already got, just doing a .postDelayed in which you just set the bg(to for example transparent) is enough..
If webview is scrollable:
Add this to the Manifest:
android:hardwareAccelerated="false"
OR
Add the following to WebView in the layout:
android:background="#android:color/transparent"
android:layerType="software"
Add the following to the parents scroll view:
android:layerType="software"
Try
webView.setBackgroundColor(Color.parseColor("#EDEDED"));
I was trying to put a transparent HTML overlay over my GL view but it has always black flickering which covers my GL view. After several days trying to get rid of this flickering I found this workaround which is acceptable for me (but a shame for android).
The problem is that I need hardware acceleration for my nice CSS animations and so webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); is not an option for me.
The trick was to put a second (empty) WebView between my GL view and the HTML overlay. This dummyWebView I told to render in SW mode, and now my HTML overlays renders smooth in HW and no more black flickering.
I don't know if this works on other devices than My Acer Iconia A700, but I hope I could help someone with this.
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
RelativeLayout layout = new RelativeLayout(getApplication());
setContentView(layout);
MyGlView glView = new MyGlView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
dummyWebView = new WebView(this);
dummyWebView.setLayoutParams(params);
dummyWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
dummyWebView.loadData("", "text/plain", "utf8");
dummyWebView.setBackgroundColor(0x00000000);
webView = new WebView(this);
webView.setLayoutParams(params);
webView.loadUrl("http://10.0.21.254:5984/ui/index.html");
webView.setBackgroundColor(0x00000000);
layout.addView(glView);
layout.addView(dummyWebView);
layout.addView(webView);
}
}
This worked for me. try setting the background color after the data is loaded. for that setWebViewClient on your webview object like:
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view, url);
webView.setBackgroundColor(Color.BLACK);
}
});
Try out:
myWebView.setAlpha(0.2f);
If nothing helps, then most probably you have fixed sized webView, change the width and height to wrap_content or match_parent, it should work. That worked for me when I tried to load a Gif.
You can user BindingAdapter like this:
Java
#BindingAdapter("setBackground")
public static void setBackground(WebView view,#ColorRes int resId) {
view.setBackgroundColor(view.getContext().getResources().getColor(resId));
view.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
}
XML:
<layout >
<data>
<import type="com.tdk.sekini.R" />
</data>
<WebView
...
app:setBackground="#{R.color.grey_10_transparent}"/>
</layout>
Resources
<color name="grey_10_transparent">#11e6e6e6</color>
myWebView.setAlpha(0);
is the best answer. It works!
I've looked through dozens of pages if similar questions, none of them have any answers, so hopefully this one will be different.
I have a webview, and I do not want the zoom of the view to change from the initial zoom level I have it set to. The only thing which changes the zoom level currently is when a text box is focused.
I need to be able to do this through Java code, not using the viewport meta tag.
Just so I don't have the common responses, I have the following in my code to disable zooming, and the zoom controls:
mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.getSettings().setSupportZoom(false);
I'm thinking that a possible solution is to check to see when an onFocus or even an onClick event occurs within the WebView and then zoomOut, but I'm not even sure if that is possible?
Any suggestions would be appreciated.
UPDATE This answer was written almost 6 years ago, with all the new android versions that came since then, this is most likely outdated.
This thing caused a major headache, but finally was solved thanks to setDefaultZoom(ZoomDensity.FAR);
One thing which is important is that onCreate and loadUrl get called before the WebSettings, otherwise it caused a force close situation. Here the ENTIRE code including imports (for the novice Java users)
package com.my.app;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebSettings.ZoomDensity;
import com.phonegap.*;
public class MyDroidActivity extends DroidGap {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
WebSettings settings = appView.getSettings();
settings.setBuiltInZoomControls(false);
settings.setSupportZoom(false);
settings.setDefaultZoom(ZoomDensity.FAR);
}
}
I solved this on HTC phones by adding a WebViewClient with an empty listener for onScaleChanged. My app is PhoneGap, so this is what it looks like, but adding the listener should look the same in a non-PhoneGap app:
public class Main extends DroidGap {
private class NoScaleWebViewClient extends GapViewClient {
public NoScaleWebViewClient(DroidGap ctx) {
super(ctx);
}
public void onScaleChanged(WebView view, float oldScale, float newScale) {
Log.d("NoScaleWebViewClient", "Scale changed: " + String.valueOf(oldScale) + " => " + String.valueOf(newScale));
}
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.init();
setWebViewClient(appView, new NoScaleWebViewClient(this));
// disables the actual onscreen controls from showing up
appView.getSettings().setBuiltInZoomControls(false);
// disables the ability to zoom
appView.getSettings().setSupportZoom(false);
appView.getSettings().setDefaultZoom(ZoomDensity.FAR);
appView.setInitialScale(100);
}
}
Strangely, the onScaleChange listener never gets called -- by listening for the zoom, it blocks the zoom from happening. I've found that I need all the other calls (setSupportZoom, setDefaultZoom, setInitialScale) in order for this to work, and removing any of them reverts to the old, buggy behavior.
I had the same trouble. I needed to find a way to scale content of webview to exact value, everything worked fine until user starts to input text. There are methods that work on relatively new devices android 4.0+ but fails on old ones. The only way that works everywhere is setting the zoom value not in Java but in viewport like this
<meta name="viewport" content="initial-scale=.80; maximum-scale=.80; minimum-scale=.80;" />
It works on every device I tested.
Did you try to disable the user-scalable in the viewport tag? Not sure if that will work for you, but it works for me. I did not need to do anything on the java side.
<meta name="viewport" content="user-scalable=no, width=device-width" />
I have encountered this problem too, and I solved it like this:
myWebview.getSettings().setDefaultZoom(ZoomDensity.FAR);
It's runing normally on Sumsung Galaxy Tab. I hope this will help you.
The WebView has one special "thing", which I think it will trigger many questions and answers here. What happens is, that when an URL is loaded, the default Android Browser kicks in through an Intent to handle this. The zooming takes part in this browser, not in your Webview.
Solution: You need to add a WebviewClient to tell Android that you handle the browsing yourself. An example:
// Use WebView and disable zooming
public class MyWebView extends Activity {
// nested class
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true
}
}
private WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.setInitialScale(500); // added after user comment
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.getSettings().setSupportZoom(false);
mWebView.loadUrl("http://www.google.com");
}
}
My main.xml looks like this
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
This code disabled zooming on my HTC Desire running Android 2.2. Tapping into HTML Input fields makes no difference.
The whole topic of WebView/HelloWebViewClient as well as an important hint to handle the "Back" button correctly is documented in Hello Views, Web View. It should be required reading for anybody who uses WebView.
I believe you can set the zoom level with WebView.setInitialScale method. It takes an int as scale so I guess you would want to do something like myWebView.setInitialScale(100).
This issue has been fixed by a firmware update on HTC devices, it was (apparently) being caused by the Sense UI overriding default Android functionality incorrectly.
It is very difficult to provide information on exactly when this was corrected, however my web application no longer zooms when a text box is clicked on any HTC device with the latest firmware.
The following two lines of code will disable the "zoom" aspects of an android webview:
// disables the actual onscreen controls from showing up
mWebView.getSettings().setBuiltInZoomControls(false);
// disables the ability to zoom
mWebView.getSettings().setSupportZoom(false);
This was headache for me too, but fortunately I have found this article: How to stop zoom in on input focus on mobile devices.
Set font size of the text in the input element to 16px (or more) in the css file.
input {
font-size: 16px;
}
It is rather hack, but if nothig else works ...