Peter E Conn | fc68648 | 2025-03-21 15:42:51 | [diff] [blame] | 1 | # How WebView Full Screen Works |
| 2 | |
| 3 | [TOC] |
| 4 | |
| 5 | Note: In this doc, `WebView` in code font means the `WebView` class that's a |
| 6 | subclass of `View`, whereas WebView in normal font means the WebView product in |
| 7 | general. |
| 8 | |
| 9 | ## How apps use full screen |
| 10 | |
| 11 | You can see an example of how to support WebView full screen in your application |
| 12 | in [the WebView |
| 13 | shell](https://source.chromium.org/chromium/chromium/src/+/main:android_webview/tools/system_webview_shell/apk/src/org/chromium/webview_shell/WebViewBrowserFragment.java;l=500;drc=cfb4fb0b1658a915db8c470751f511bb8360a9bc). |
| 14 | The app overrides `WebChromeClient#onShowCustomView` to do something like: |
| 15 | |
| 16 | ```java |
| 17 | @Override |
| 18 | public void onShowCustomView( |
| 19 | View view, WebChromeClient.CustomViewCallback callback) { |
| 20 | if (mFullscreenView != null) { |
| 21 | ((ViewGroup) mFullscreenView.getParent()).removeView(mFullscreenView); |
| 22 | } |
| 23 | mFullscreenView = view; |
| 24 | requireActivity() |
| 25 | .getWindow() |
| 26 | .addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 27 | requireActivity() |
| 28 | .getWindow() |
| 29 | .addContentView( |
| 30 | mFullscreenView, |
| 31 | new FrameLayout.LayoutParams( |
| 32 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 33 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 34 | Gravity.CENTER)); |
| 35 | } |
| 36 | ``` |
| 37 | |
| 38 | Essentially, when the web contents requests to go full screen, WebView creates a |
| 39 | `View` (a |
| 40 | [FullScreenView](https://source.chromium.org/chromium/chromium/src/+/main:android_webview/java/src/org/chromium/android_webview/FullScreenView.java)) |
| 41 | and hands it to the app to attach to the window. Usually the existing `WebView` |
| 42 | remains attached to the window, but obscured by the full screen `View`. |
| 43 | |
|
|