How to Enable Fullscreen mode in any videos in webview Android Studio Tutorial

This is not a UI source code post this is just a simple tutorial many users and my YouTube viewers are asking me about this for a long time so I think to make a tutorial about this So in simple WebView we cannot make fullscreen on a video but we can do that with some simple codes…

This is the Video Tutorial about this you can watch that…

1. Create a new project. 

1. Create a new project in Android Studio from File ⇒ New Project and select Empty Activity from templates.or you can choose your existing WebView project.

2. If you already know about WebView code on how to add WebView then skip this code is just for WebView to load a website…

3. Open activity_main.xml and past this code…

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>

4. Now open your WebView Contained java file in my case it is MainActivity.java so open that and past this simple WebView code.

public class MainActivity extends AppCompatActivity {

    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = findViewById(R.id.webView);

        webView.setWebViewClient(new Browser_Home());
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setAppCacheEnabled(true);

        loadWebSite();

    }

    private void loadWebSite() {
        webView.loadUrl("https://www.youtube.com");
    }

    private class Browser_Home extends WebViewClient {
        Browser_Home(){}

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }
    }
}

5. Now you can run this application and check if the WebView is working or not. As you can see here you can see a button for fullscreen but that button is not active you can not click on that.

6. Now back to project in onCreate past this code.

webView.setWebChromeClient(new ChromeClient());

7. Now in outside of OnCrearte past this code.

private class ChromeClient extends WebChromeClient {
        private View mCustomView;
        private WebChromeClient.CustomViewCallback mCustomViewCallback;
        protected FrameLayout mFullscreenContainer;
        private int mOriginalOrientation;
        private int mOriginalSystemUiVisibility;

        ChromeClient() {}

        public Bitmap getDefaultVideoPoster()
        {
            if (mCustomView == null) {
                return null;
            }
            return BitmapFactory.decodeResource(getApplicationContext().getResources(), 2130837573);
        }

        public void onHideCustomView()
        {
            ((FrameLayout)getWindow().getDecorView()).removeView(this.mCustomView);
            this.mCustomView = null;
            getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
            setRequestedOrientation(this.mOriginalOrientation);
            this.mCustomViewCallback.onCustomViewHidden();
            this.mCustomViewCallback = null;
        }

        public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback)
        {
            if (this.mCustomView != null)
            {
                onHideCustomView();
                return;
            }
            this.mCustomView = paramView;
            this.mOriginalSystemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
            this.mOriginalOrientation = getRequestedOrientation();
            this.mCustomViewCallback = paramCustomViewCallback;
            ((FrameLayout)getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
            getWindow().getDecorView().setSystemUiVisibility(3846 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }

8. This is the final code for this WebView Check that if you miss any code.

package com.monstertechno.webviewfullscreen;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

public class MainActivity extends AppCompatActivity {

    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = findViewById(R.id.webView);

        webView.setWebViewClient(new Browser_Home());
        webView.setWebChromeClient(new ChromeClient());
        WebSettings webSettings = webView.getSettings();

        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setAppCacheEnabled(true);

        loadWebSite();

    }

    private void loadWebSite() {
        webView.loadUrl("https://www.youtube.com");
    }

    private class Browser_Home extends WebViewClient {
        Browser_Home(){}

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }
    }

    private class ChromeClient extends WebChromeClient {
        private View mCustomView;
        private WebChromeClient.CustomViewCallback mCustomViewCallback;
        protected FrameLayout mFullscreenContainer;
        private int mOriginalOrientation;
        private int mOriginalSystemUiVisibility;

        ChromeClient() {}

        public Bitmap getDefaultVideoPoster()
        {
            if (mCustomView == null) {
                return null;
            }
            return BitmapFactory.decodeResource(getApplicationContext().getResources(), 2130837573);
        }

        public void onHideCustomView()
        {
            ((FrameLayout)getWindow().getDecorView()).removeView(this.mCustomView);
            this.mCustomView = null;
            getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
            setRequestedOrientation(this.mOriginalOrientation);
            this.mCustomViewCallback.onCustomViewHidden();
            this.mCustomViewCallback = null;
        }

        public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback)
        {
            if (this.mCustomView != null)
            {
                onHideCustomView();
                return;
            }
            this.mCustomView = paramView;
            this.mOriginalSystemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
            this.mOriginalOrientation = getRequestedOrientation();
            this.mCustomViewCallback = paramCustomViewCallback;
            ((FrameLayout)getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
            getWindow().getDecorView().setSystemUiVisibility(3846 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }
}

9. That’s it now run this application and play any videos you want you can see that the fullscreen button is now activated so now you can click on that…

Posts created 41

163 thoughts on “How to Enable Fullscreen mode in any videos in webview Android Studio Tutorial

  1. great work… thank you so much. I have just one problem, when i exit the fullscreen with back button it takes me all way back to the home page. is there a way i can make it to just go back once to the page where the video is.. thanks bro

  2. If you set the orientation to portrait then you can not play that or you have to create an other activity where you have to add the orientation code otherwise the video will play in portrait mode

  3. I am not understand about this video or code but i have required this full screen on my project
    So i just create empty project webview i past my code here so plz help me
    package np.com.shandesh;

    import android.support.v4.widget.SwipeRefreshLayout;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.Window;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;

    public class MainActivity extends AppCompatActivity
    {
    WebView mWebView;

    SwipeRefreshLayout swipe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
    swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
    {
    public void onRefresh(){
    LoadWeb();
    }
    });

    LoadWeb();

    }

    public void LoadWeb()
    {
    mWebView = (WebView) findViewById(R.id.webView);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setAppCacheEnabled(true);
    mWebView.loadUrl("http://shandesh.com.np/&quot;);
    swipe.setRefreshing(true);
    mWebView.setWebViewClient(new WebViewClient() {

    public void onReveivedError(WebView view, int errorCode, String description, String failingUrl){
    mWebView.loadUrl("file://android_asset/error.html");
    }

    public void onPageFinished(WebView view, String url)
    {
    //hide the swipe refreshlayout
    swipe.setRefreshing(false);
    }

    });

    }

    @Override
    public boolean onKeyDown(final int keyCode, final KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
    mWebView.goBack();
    return true;
    }
    return super.onKeyDown(keyCode, event);
    }
    }

  4. 353/5000
    How can I add the "back" function so that the full screen mode is disabled without going to the previous page? I want that when pressing the button back, do not go to the previous page, that only the whole screen will be disabled. When the full screen is disabled, the "back" button works to return to the previous page.

  5. Sorry I can not understand your question can you explain it more. Do please contact me with your problem use the messenger feature or the whatsapp to contatc, details are in the application and also in the site's hire section 😊

    1. When I leave the fullscreen it creates a white space on top, how can it be fixed?
      same error how to resolve it..??

  6. hi i use this code an the fullscreen butto is ative but no open fullscreen ,
    how do you detect click on button fullscreen??

  7. This is a great solution! Even better than the top answer here…
    https://stackoverflow.com/questions/15768837/playing-html5-video-on-fullscreen-in-android-webview

    No need to make another view to serve as a container for the fullscreen view. Plus, no need to override the URL, which is very nice.

    Just one piece of advice for people on newer devices experiencing a white space appearing when returning from fullscreen via rotating the device into portrait mode…

    Check that this rules has NOT been added to the parent of your webview in your XML layout:
    android:fitsSystemWindows="true" <– Remove that!

    Another solution is the following…
    Change this line:
    activity.getWindow().getDecorView().setSystemUiVisibility(3846 )
    to this:
    activity.getWindow().getDecorView().setSystemUiVisibility(3846 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

    — Essentially adding the layout stable flag to the .setSystemUiVisibility() command. It tells Android not to resize when the system bars hide and show
    Ref: https://developer.android.com/training/system-ui/immersive#java

  8. Hi @Monster Techno,

    Could you explain your idea and your properties also? I still confuse in some cases. I really want to understand your code rather than just copy and paste.
    And could you explain why we can't click on fullscreen button?

    Thanks

  9. I pasted the code to an existing browser app that I created but its not working. The code is 100% error free and it doesn't crash my app but it doesn't do anything. The little square at the bottom right corner doesn't turn white, it stays dim, or greyed out. I have a toolbar widget that might be causing the problem but I know it can still work with a toolbar, many browser apps have this feature. Please help me with this. This is one of the final pieces to my app and it will be complete. Thanks

  10. Thank you! work a magic.

    You can change orientation to landscape by adding this line to onPageStarted:
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    also add this line to onPageFinished to go back to portrait after the user exit the video:
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

  11. The soft keyboard is not showing after I exit the full screen. I tested on android studio avd and my real device.

  12. Whether it is a friend’s new profile picture, or a picture too ridiculous, too funny, or too adorable to leave without acknowledgement, here’s your guide on figuring out how to leave your mark.
    This almost always solicits a response along the lines of “so cute! I <3 you!” or “that was such a fun night!” — essentially anything that makes it clear that you and your BFFL had ridiculous amounts of fun that night, and everyone else should be “mad jeals.”
    바카라사이트

  13. Typically this requires an “aw you two look great” or “so cute! Hope it was a fun night, we will definitely have to meet up sometime!” Also translated into either “I probably still had more fun than you” or “why wasn’t I invited?”
    From the simple “photo creds” to the slightly more daring “wow, great picture. The photographer must be so talented and good looking,” this comment almost always demands acknowledgement. Often a banner ad for “Whatever, you probably didn’t pick a picture with me because I made you look bad.”
    카지노사이트

  14. “You look great!” “Wow, you’re beautiful.” “Gorgeous, as always.” There are no other comments. I don’t care if they look like they haven’t slept in a week and there’s an entire plate of spinach stuck in their teeth – don’t risk it.
    This is the comment you post on a picture that everyone else commented on at least six months ago. Typically, this comment is just blatant proof that you’ve been doing some facebook stalking.
    온라인카지노

  15. It’s the conversation that probably shouldn’t even be on a facebook wall, but definitey should not be on a picture. For future reference a picture is not the proper place to ask “do you want to hang out later today?”
    This is the one that seems nice the first time you read it, and then you realize – – it’s really not. It’s the comment that says “Great angle!” or “Wow, this picture makes you look great,” it’s the comment version of saying you don’t normally look like this, clearly this picture is false advertisement (a screenshot of this type of comment appears in the teaser spot on the home page).
    바카라사이트

  16. Commenting is a key aspect of the photo experience, whatever message they’re sending. But for those of us too lazy to come up with witticisms, there’s always the like button.
    It’s only funny when it’s someone else. Untag and pretend it doesn’t exist. The more you comment the more it shows up on news feeds.
    카지노사이트

  17. Thanks a lot for sharing this with all people you really understand
    what you are talking approximately! Bookmarked. Please also seek advice from my web site =).
    We will have a hyperlink exchange agreement among us

  18. Aw, this was an incredibly nice post. Spending some time and actual
    effort to produce a top notch article… but what can I say… I
    procrastinate a whole lot and don’t seem to get nearly anything done.

  19. I’m truly enjoying the design and layout of your blog.
    It’s a very easy on the eyes which makes it much more enjoyable for
    me to come here and visit more often. Did you hire out a developer to create your theme?
    Excellent work!

  20. My spouse and I stumbled over here from a different page and thought I might as well check things out. I like what I see so now i’m following you. Look forward to finding out about your web page repeatedly.|

  21. Hello terrific website! Does running a blog such as this require
    a massive amount work? I have very little knowledge of programming however I had been hoping
    to start my own blog soon. Anyways, should you have any suggestions or
    tips for new blog owners please share. I know this is off
    topic but I just wanted to ask. Appreciate it!

  22. Aw, this was an exceptionally nice post. Taking the time
    and actual effort to generate a great article… but what can I say… I procrastinate a whole lot and don’t seem to get anything done.

  23. Hi there, I found your website by means of Google
    even as looking for a similar matter, your site came up,
    it looks great. I have bookmarked it in my google bookmarks.

    Hi there, simply became aware of your blog thru Google, and
    located that it is truly informative. I am going to be careful for brussels.
    I will appreciate for those who proceed this in future. Many people will likely be benefited from your writing.
    Cheers!

  24. Definitely assume that that you simply said. Your favorite reason seemed to be in the internet the simplest thing to understand.
    I have faith that for your needs, I certainly get annoyed while people think about worries that they can plainly tend not to know about.

    You were able to hit the nail upon the best as well as defined
    out the whole thing with out side-effect , people can take a signal.
    Will probably come back to get more. Thanks

    my web-site: Fabulous Cooker Hood Extractor Fan

  25. Everyone loves what you guys are usually up too. This kind of clever work and exposure! Keep up the fantastic works guys I’ve included you guys to blogroll.|

  26. My significant other and so i stumbled right here from the different page and thought I
    might check things out. I love the things i see so
    i am just just following you. Enjoy exploring your online page
    for the second time.

    My web site: HeathUSubich

  27. Hmm is anyone else having problems with the pictures on this blog loading? I’m trying to figure out if its a problem on my end or if it’s the blog. Any feedback would be greatly appreciated.

  28. Anyway just wanted to say thanks for your website.
    Materials like this helps to keep me and helps me stay on track.
    I am hoping that you continue to grow, and can find it!
    Thanks and good luck again. Although I’m experimenting with
    trying to put my own spin anyway, I love keto so far.
    It is essential to be flexible with your daily diet,
    even if you’re locked into something like”keeping carbs low”.
    I do not wish to be the person eating out with friends which
    orders something weird the menu, or nothing at all
    off, although I would like to shed weight.
    It is just flat out not worth it if you ask me if your daily diet comes at the cost of your joy.

    That is GREAT. Been doing my very best to try and do quality study,
    so things like this helps. Anyone else believe that the biggest issue people have with weight reduction comes from
    them not putting in the job first? Like I do you wish to begin losing weight
    ASAP, however you have to be willing to do a bit of research.

    I’m sorry to say you’re just going to have issues
    if you do not do your part. xoxo

  29. I blog quite often and I truly thank you for your information. This article has really peaked my interest. I am going to take a note of your blog and keep checking for new information about once per week. I opted in for your RSS feed as well.

  30. Fascinating blog! Is your theme custom made or did you download it from somewhere? A theme like yours with a few simple tweeks would really make my blog shine. Please let me know where you got your theme. Thanks a lot|

  31. Leading Online Pharmacy – Contact us at +1 (917) 259-3352 for unbelievable rates, discount and offers on any medicine. Get it delivered free of cost at your door steps, call us today. Phone : +1 (917) 259-3352

  32. фитнес резинка в хмельницькому
    набір фітнес резинок prom
    резинкп жля фитнеса
    фитнес резинка в хмельницькому
    фитнеес резинки фирмы
    купить резинки для фитнеса харьков

    Check oᥙt my blog – латексні фітнес резинки (https://project1009827.turbo.site)

Leave a Reply to ailx Cancel reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top