Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to implement <code>showCustomView</code> &amp; <code>hideCustomView</code> method of WebChromeClient, also You need <code>android:hardwareAccelerated="true"</code> in your AndroidManifest File. I am posting my sample project here. What i have done is kept one Framelayout(customContainer) in my main.xml, and adding view received in showCustomView here, and removing it in onHide. Also hiding/showing webview accordingly. Below code works perfectly on device.</p> <p>AndroidManifest.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.webview" android:versionCode="1" android:versionName="1.0"&gt; &lt;uses-sdk android:minSdkVersion="8"/&gt; &lt;uses-permission android:name="android.permission.INTERNET"/&gt; &lt;application android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:hardwareAccelerated="true"&gt; &lt;activity android:name="MyActivity" android:configChanges="orientation|keyboardHidden" android:hardwareAccelerated="true" android:label="@string/app_name"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN"/&gt; &lt;category android:name="android.intent.category.LAUNCHER"/&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p>main.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;WebView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/webView" android:layout_gravity="center" /&gt; &lt;FrameLayout android:id="@+id/customViewContainer" android:layout_width="fill_parent" android:layout_height="fill_parent" android:visibility="gone" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>video_progress.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/progress_indicator" android:orientation="vertical" android:layout_centerInParent="true" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;ProgressBar android:id="@android:id/progress" style="?android:attr/progressBarStyleLarge" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content"/&gt; &lt;TextView android:paddingTop="5dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="loading" android:textSize="14sp" android:textColor="?android:attr/textColorPrimary"/&gt; &lt;/LinearLayout&gt; </code></pre> <p>MyActivity.java</p> <pre><code>package com.example.webview; import android.app.Activity; import android.graphics.Bitmap; import android.os.Bundle; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.FrameLayout; public class MyActivity extends Activity { private WebView webView; private FrameLayout customViewContainer; private WebChromeClient.CustomViewCallback customViewCallback; private View mCustomView; private myWebChromeClient mWebChromeClient; private myWebViewClient mWebViewClient; /** * Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); customViewContainer = (FrameLayout) findViewById(R.id.customViewContainer); webView = (WebView) findViewById(R.id.webView); mWebViewClient = new myWebViewClient(); webView.setWebViewClient(mWebViewClient); mWebChromeClient = new myWebChromeClient(); webView.setWebChromeClient(mWebChromeClient); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setAppCacheEnabled(true); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setSaveFormData(true); webView.loadUrl("http://m.youtube.com"); } public boolean inCustomView() { return (mCustomView != null); } public void hideCustomView() { mWebChromeClient.onHideCustomView(); } @Override protected void onPause() { super.onPause(); //To change body of overridden methods use File | Settings | File Templates. webView.onPause(); } @Override protected void onResume() { super.onResume(); //To change body of overridden methods use File | Settings | File Templates. webView.onResume(); } @Override protected void onStop() { super.onStop(); //To change body of overridden methods use File | Settings | File Templates. if (inCustomView()) { hideCustomView(); } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (inCustomView()) { hideCustomView(); return true; } if ((mCustomView == null) &amp;&amp; webView.canGoBack()) { webView.goBack(); return true; } } return super.onKeyDown(keyCode, event); } class myWebChromeClient extends WebChromeClient { private Bitmap mDefaultVideoPoster; private View mVideoProgressView; @Override public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) { onShowCustomView(view, callback); //To change body of overridden methods use File | Settings | File Templates. } @Override public void onShowCustomView(View view,CustomViewCallback callback) { // if a view already exists then immediately terminate the new one if (mCustomView != null) { callback.onCustomViewHidden(); return; } mCustomView = view; webView.setVisibility(View.GONE); customViewContainer.setVisibility(View.VISIBLE); customViewContainer.addView(view); customViewCallback = callback; } @Override public View getVideoLoadingProgressView() { if (mVideoProgressView == null) { LayoutInflater inflater = LayoutInflater.from(MyActivity.this); mVideoProgressView = inflater.inflate(R.layout.video_progress, null); } return mVideoProgressView; } @Override public void onHideCustomView() { super.onHideCustomView(); //To change body of overridden methods use File | Settings | File Templates. if (mCustomView == null) return; webView.setVisibility(View.VISIBLE); customViewContainer.setVisibility(View.GONE); // Hide the custom view. mCustomView.setVisibility(View.GONE); // Remove the custom view from its container. customViewContainer.removeView(mCustomView); customViewCallback.onCustomViewHidden(); mCustomView = null; } } class myWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return super.shouldOverrideUrlLoading(view, url); //To change body of overridden methods use File | Settings | File Templates. } } } </code></pre> <p><strong><a href="https://github.com/akhgupta/WebviewVideo" rel="noreferrer">You can clone sample project from here..</a></strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload