Created
October 27, 2015 13:54
-
-
Save LuizGadao/3200c23269ce32ab99c8 to your computer and use it in GitHub Desktop.
Fragment embed youtube video in Webview.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.mytest.luizgadao.testyoutubewebview; | |
import android.graphics.Bitmap; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.webkit.WebChromeClient; | |
import android.webkit.WebView; | |
import android.webkit.WebViewClient; | |
import android.widget.FrameLayout; | |
/** | |
* A placeholder fragment containing a simple view. | |
*/ | |
public class MainActivityFragment extends Fragment { | |
private WebView webView; | |
private FrameLayout customViewContainer; | |
private WebChromeClient.CustomViewCallback customViewCallback; | |
private View mCustomView; | |
private myWebChromeClient mWebChromeClient; | |
private myWebViewClient mWebViewClient; | |
public MainActivityFragment() { | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
return inflater.inflate(R.layout.fragment_main, container, false); | |
} | |
@Override | |
public void onViewCreated(View view, Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
customViewContainer = (FrameLayout) getView().findViewById(R.id.customViewContainer); | |
webView = (WebView) getView().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); | |
String playVideo= "<html><body><iframe class=\"youtube-player\" type=\"text/html\" width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/-eFM737WnHE\" frameborder=\"0\"></body></html>"; | |
webView.loadData(playVideo, "text/html", "utf-8"); | |
} | |
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. | |
} | |
} | |
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(getContext()); | |
//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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment