First write the layout file activity_main.xml:
<LinearLayout xmlns:andro xmlns:tools="/tools" android: android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <FrameLayout android: android:layout_width="fill_parent" android:layout_height="fill_parent" android:visibility="gone" ></FrameLayout> <Button android: android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="The button is not displayed in full screen, click to switch horizontal screen" android:gravity="center" /> <WebView android: android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
Principle: When implementing full screen, put the video in the webview into a View (video_view control in the layout), and then hide the webview! This enables full-screen playback!
Now let’s take a look at how it is implemented:
Put the code first:
public class MainActivity extends Activity { private FrameLayout videoview;// Video loading view when full screen private Button videolandport; private WebView videowebview; private Boolean islandport = true;//true means that it is a vertical screen at this time, false means that it is a horizontal screen at this time. private View xCustomView; private xWebChromeClient xwebchromeclient; private String url = "/mobile_api.php?mod=news&id=12604"; private xCustomViewCallback; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE);//Remove the application title getWindow().setFlags(.FLAG_FULLSCREEN, .FLAG_FULLSCREEN); setContentView(.activity_main); initwidget(); initListener(); (url); } private void initListener() { // TODO Auto-generated method stub (new Listener()); } private void initwidget() { // TODO Auto-generated method stub videoview = (FrameLayout) findViewById(.video_view); videolandport = (Button) findViewById(.video_landport); videowebview = (WebView) findViewById(.video_webview); WebSettings ws = (); /** * setAllowFileAccess Enable or disable WebView access to file data setBlockNetworkImage Whether to display network images * setBuiltInZoomControls Set whether to support scaling setCacheMode Set buffering mode * setDefaultFontSize Set the default font size setDefaultTextEncodingName Set the default encoding used when decoding * setFixedFontFamily Sets the fixed font setJavaScriptEnabled Sets whether Javascript is supported * setLayoutAlgorithm Set layout mode setLightTouchEnabled Set the option to activate with the mouse * setSupportZoom Set whether to support zoom * */ (true);// Hide the zoom button (.NARROW_COLUMNS);// Typesetting adapts to the screen (true);// Can be scaled at any rate (true);// setUseWideViewPort method sets the recommended window for webview. The setLoadWithOverviewMode method is to set the mode of the page loaded by the webview. (true); (true);// Save form data (true); (true);// Enable geolocation ("/data/data/.html5webview/databases/");// Set the database path to locate (true); xwebchromeclient = new xWebChromeClient(); (xwebchromeclient); (new xWebViewClientent()); } class Listener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub switch (()) { case .video_landport: if (islandport) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); ("The button is not displayed in full screen, click to switch horizontal screen"); }else { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); ("The button is not displayed in full screen, click to switch vertical screen"); } break; default: break; } } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (inCustomView()) { hideCustomView(); return true; }else { ("about:blank"); // ("", "text/html; charset=UTF-8", null); (); ("testwebview", "===>>>2"); } } return true; } /** * Determine whether it is full screen * @return */ public boolean inCustomView() { return (xCustomView != null); } /** * Press the return key to execute the method of exiting the full screen */ public void hideCustomView() { (); } /** * Process Javascript dialog boxes, website icons, website titles, and web page loading progress, etc. * @author */ public class xWebChromeClient extends WebChromeClient { private Bitmap xdefaltvideo; private View xprogressvideo; @Override // Methods that will be called when playing online videos public void onShowCustomView(View view, callback) { if (islandport) { } else{ // ii = "1"; // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); (); //If a view already exists, terminate immediately and create a new one if (xCustomView != null) { (); return; } (view); xCustomView = view; xCustomViewCallback = callback; (); } @Override //The video playback will be called if it exits the full screen. public void onHideCustomView() { if (xCustomView == null)//Not full screen playback status return; // Hide the custom view. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); (); // Remove the custom view from its container. (xCustomView); xCustomView = null; (); (); (); //(LOGTAG, "set it to webVew"); } // Add default icon for video loading @Override public Bitmap getDefaultVideoPoster() { //(LOGTAG, "here in on getDefaultVideoPoster"); if (xdefaltvideo == null) { xdefaltvideo = ( getResources(), ); } return xdefaltvideo; } //The process loading when the video is loading @Override public View getVideoLoadingProgressView() { //(LOGTAG, "here in on getVideoLoadingPregressView"); if (xprogressvideo == null) { LayoutInflater inflater = (); xprogressvideo = (.video_loading_progress, null); } return xprogressvideo; } //Web title @Override public void onReceivedTitle(WebView view, String title) { ().setTitle(title); } // @Override // // Update window progress when the WebView progress changes// public void onProgressChanged(WebView view, int newProgress) { // ().getWindow().setFeatureInt(Window.FEATURE_PROGRESS, newProgress*100); // } } /** * Handle various notifications, requests and other events * @author */ public class xWebViewClientent extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { ("webviewtest", "shouldOverrideUrlLoading: "+url); return false; } } /** * This method will be called when horizontal and vertical screens are switched * @author */ @Override public void onConfigurationChanged(Configuration newConfig) { ("testwebview", "=====<<< onConfigurationChanged >>>====="); (newConfig); if( == Configuration.ORIENTATION_LANDSCAPE){ ("webview", "Now it's horizontal screen 1"); islandport = false; }else if( == Configuration.ORIENTATION_PORTRAIT){ ("webview", "Now it's vertical screen 1"); islandport = true; } } }
Finally, let’s talk about the settings;
Access to the network plus this sentence
<uses-permission android:name=""/>
When switching horizontal and vertical screens, in order not to call onCreate and other methods again, you need to add this sentence:
android:configChanges="orientation|keyboardHidden|screenSize"
Summarize
The above is what the editor introduced to you about Android using WebView to achieve full screen switching to play web videos. I hope it will be helpful to everyone. If you have any questions, please leave me a message. The editor will reply to everyone in time!