Android programming to achieve WebView Full-screen playback method (with source code) _android

Source: Internet
Author: User
Tags stub

This article describes the Android programming implementation of WebView Full-screen playback method. Share to everyone for your reference, specific as follows:

Recently because the project to use WebView load HTML5 video, start not full screen, did a long time to make! Then according to my understanding of how to achieve full screen bar.

First write the layout file Activity_main.xml:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
 xmlns:tools= "http:// Schemas.android.com/tools "
 android:id=" @+id/container "
 android:layout_width=" Fill_parent "
 android: layout_height= "Fill_parent"
 android:orientation= "vertical" >
 <framelayout android:id= 
  "@+id/" Video_view "
  android:layout_width=" fill_parent "
  android:layout_height=" Fill_parent "
  android: Visibility= "Gone"
  ></FrameLayout>
 <button 
  android:id= "@+id/video_landport"
  Android:layout_width= "Fill_parent"
  android:layout_height= "wrap_content"
  android:text= "full screen does not show the button, Click to toggle Horizontal screen "
  android:gravity=" center "
  />
 <webview 
  android:id=" @+id/video_webview "
  Android:layout_width= "Fill_parent"
  android:layout_height= "fill_parent"
  />
 </linearlayout >

Principle: Full screen when the video in the WebView into a view (layout of the Video_view control) inside, and then the WebView hidden away! This will achieve a full screen playback!
Now specifically to see how to achieve:

First put code Mainactivity.java:

public class Mainactivity extends activity {private framelayout videoview;//full-screen video load view private Button videolandport;
 Private WebView Videowebview;
 Private Boolean Islandport = True;//true Indicates that this is a vertical screen and false indicates a horizontal screen at this time.
 Private View Xcustomview;
 Private Xwebchromeclient xwebchromeclient;
 Private String URL = "http://look.appjx.cn/mobile_api.php?mod=news&id=12604";
 Private Webchromeclient.customviewcallback Xcustomviewcallback;
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title)//Remove the application title GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_
  Fullscreen, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  Setcontentview (R.layout.activity_main);
  Initwidget ();
  Initlistener ();
 Videowebview.loadurl (URL);
 private void Initlistener () {//TODO auto-generated Method Stub Videolandport.setonclicklistener (new Listener ()); } private void Initwidget () {//TODO auto-generated metHod stub videoview = (Framelayout) Findviewbyid (R.id.video_view);
  Videolandport = (Button) Findviewbyid (R.id.video_landport);
  Videowebview = (webview) Findviewbyid (R.id.video_webview);
  WebSettings ws = Videowebview.getsettings (); /** * setallowfileaccess Enable or disable WebView access to file data setblocknetworkimage Display Network image * Setbuiltinzoomcontrols settings support scaling Setcache Mode sets the buffered pattern * setdefaultfontsize Sets the default font size SETDEFAULTTEXTENCODINGNAME set the default encoding to use when decoding * setfixedfontfamily set the font to be used Setjavasciptenabled Settings Support JavaScript * Setlayoutalgorithm set layout setlighttouchenabled settings with the mouse to activate the option * setsupportzoom settings Whether to support Zoom */Ws.setbuiltinzoomcontrols (TRUE);//Hide Zoom button Ws.setlayoutalgorithm (WebSettings.LayoutAlgorithm.NARROW_COLU MNS)//Typesetting adaptation screen Ws.setusewideviewport (TRUE);//Can scale Ws.setloadwithoverviewmode (true) at any scale; The Setusewideviewport method sets the window recommended for use by WebView.
  The Setloadwithoverviewmode method is to set the mode of the page loaded by WebView.
  Ws.setsavepassword (TRUE); Ws.setsaveformdata (TRUE);//Save form Data ws.setjavascriptenabled (TRUE); Ws.setgeolocationenabled (TRUE);/enable geo-location Ws.setgeolocationdatabasepath ("/data/data/org.itri.html5webview/
  Databases/");/set the location of the database path ws.setdomstorageenabled (true);
  Xwebchromeclient = new Xwebchromeclient ();
  Videowebview.setwebchromeclient (xwebchromeclient);
 Videowebview.setwebviewclient (New Xwebviewclientent ()); Class Listener implements Onclicklistener {@Override public void OnClick (View v) {//TODO auto-generated Metho d Stub switch (V.getid ()) {case R.id.video_landport:if (islandport) {setrequestedorientation (activityinf
     o.screen_orientation_portrait);
    Videolandport.settext ("Full screen does not show the button, click Switch Horizontal Screen"); 
     }else {setrequestedorientation (activityinfo.screen_orientation_landscape);
    Videolandport.settext ("Full screen does not show the button, click to switch the vertical screen");
   } break;
   Default:break;
    @Override public boolean onKeyDown (int keycode, keyevent event) {if (keycode = = Keyevent.keycode_back) {
  if (Incustomview ()) {Hidecustomview ();   return true; }else {videowebview.loadurl ("About:blank");//Mtestwebview.loaddata ("", "text/html;
    Charset=utf-8 ", NULL);
     MainActivity.this.finish ();
   LOG.I ("Testwebview", "===>>>2");
  } return true;
   /** * To determine if it is full screen * @return/public boolean Incustomview () {return (Xcustomview!= null);
   /** * Full screen when pressing the back key to perform exit Full-screen method */public void Hidecustomview () {Xwebchromeclient.onhidecustomview ();
  /** * processing JavaScript dialog boxes, Web site icons, site titles, and Web page loading progress * @author * * public class Xwebchromeclient extends Webchromeclient {
  Private Bitmap Xdefaltvideo;
  Private View Xprogressvideo;
   @Override//Full screen will be called when playing network video Onshowcustomview (view view, Webchromeclient.customviewcallback callback) { if (islandport) {} else{//II = "1";//Setrequestedorientation (activityinfo.screen_orientation_portrait) 
   ; 
   } setrequestedorientation (Activityinfo.screen_orientation_landscape); Videowebview.setvisibiLity (View.gone);
    If a view already exists, immediately terminates and creates a new if (Xcustomview!= null) {Callback.oncustomviewhidden ();
   Return
   } videoview.addview (view);
   Xcustomview = view;
   Xcustomviewcallback = callback;
  Videoview.setvisibility (view.visible);      
   @Override//Video playback exit Full-screen will be called public void Onhidecustomview () {if (Xcustomview = null)//not Full-screen playback status return;
   Hide the custom view. 
   Setrequestedorientation (activityinfo.screen_orientation_portrait);
   Xcustomview.setvisibility (View.gone);
   Remove the custom view from its container.
   Videoview.removeview (Xcustomview);
   Xcustomview = null;
   Videoview.setvisibility (View.gone);
   Xcustomviewcallback.oncustomviewhidden ();
   Videowebview.setvisibility (view.visible);
  LOG.I (LogTag, "set it to Webvew"); //Video load Add default icon @Override public Bitmap Getdefaultvideoposter () {//log.i (LogTag, ' here in ' getdefaultvideoposte 
   R "); if (Xdefaltvideo = = null) {Xdefaltvideo = Bitmapfactory.decoDeresource (Getresources (), R.drawable.videoicon);
  return xdefaltvideo; }//Video load-time process loading @Override public View Getvideoloadingprogressview () {//log.i (LogTag, ' here in ' getvideoload
   Ingpregressview ");
    if (Xprogressvideo = = null) {Layoutinflater inflater = Layoutinflater.from (mainactivity.this);
   Xprogressvideo = inflater.inflate (r.layout.video_loading_progress, NULL); 
  return xprogressvideo; }//page title @Override public void Onreceivedtitle (WebView view, String title) {(mainactivity.this). Settitle (title
   ); //@Override//////////////////////////////////////////////WebView
activity.this). GetWindow (). Setfeatureint (window.feature_progress, newprogress*100);
   }/** * handles various notices, requests, etc. events * @author/public class Xwebviewclientent extends Webviewclient {@Override public boolean shouldoverrideurlloading (webview view, String URL) {log.i ("Webviewtest", "ShoulDoverrideurlloading: "+url);
   return false; }/** * This method is invoked when the onconfigurationchanged is switched * * @author/@Override public void Configuration newconfig) {L
   OG.I ("Testwebview", "=====<<< onconfigurationchanged >>>=====");
   Super.onconfigurationchanged (Newconfig);
    if (newconfig.orientation = = Configuration.orientation_landscape) {log.i ("WebView", "now is horizontal screen 1");
   Islandport = false;
    }else if (newconfig.orientation = = configuration.orientation_portrait) {log.i ("WebView", "now is vertical screen 1");
   Islandport = true;

 }
 }
}

Code inside I wrote a lot of comments, I believe we can understand, of course, I am a rookie, there may be some places to understand the wrong! You know, you can tell me. Then I added a function inside, that is, click the WebView above button to toggle the screen! There is a full screen when you click to force horizontal screen playback, click Back to return to the vertical screen! I do not use the mobile phone gravity sensor to switch the screen! Inside the code also added a playback progress of the layout file, we can see in the source!

Finally next Androidmanifest.xml set;

Access network permissions plus this sentence

Copy Code code as follows:
<uses-permission android:name= "Android.permission.INTERNET"/>

When you switch the screen in order to not recall the oncreate and other methods, to add this sentence:
Copy Code code as follows:
Android:configchanges= "Orientation|keyboardhidden|screensize"

Almost this can be achieved full-screen playback, if there is not understand the question can ask me,

Full instance code code click here to download the site.

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.