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 how to add WebView then skip this code this code is just for WebView to load a website...

3. Open activity_main.xml and past this code...

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

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.

7. Now in outside of OnCrearte past this code.

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



8. 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...

This is a simple tutorial thanks for your time...


Comments

  1. Replies
    1. This is a simple app code just copy and past so it will work properly you dont need to get the .aia file 😉

      Delete
    2. 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

      Delete
    3. Can you send me your email, Jasin? I can help you.

      Delete
    4. @loc phan. I don't know if any of my reply messages have been posting because they're not showing up . My Email is jjcyr55@gmail.com Please help. Maybe I'm not putting the code in the right place, tried everything . Thanks.

      Delete
    5. Can I put the code in a new empty activity and somehow include it in my main browsing activity ?

      Delete
  2. i want full screen in landscape orientation, my activity has fixed portrait orientation

    ReplyDelete
    Replies
    1. 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

      Delete
    2. You can explain it. I also want full screen video in landscape orientation instead of portrait orientation

      Delete
    3. I ALSO MAKE A TUTORIAL FOR THIS JUST FOLLOW THIS LINK- https://monstertechno.page.link/pPWNxYnvwe3ofNi76

      Delete
  3. 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

    ReplyDelete
    Replies
    1. You need to setup webview onback button listener so it only go back to the last page

      Delete
  4. How To Play in Full Screen Landscape Mode ?

    if i screen rotate then webview has been reloading problem . how to fix it ?

    ReplyDelete
    Replies
    1. Yes you need to save the state when you rotate the activity and then restore the activity after rotation it have separate code

      Delete
  5. Replies
    1. I can not understand your question can you elaborate it??

      Delete
  6. It shows cannot resolve symbol R . please help

    ReplyDelete
    Replies
    1. Clean the project and build it again It can solve your R problem

      In the toolbar click build and click clean project after that click rebuild project 😉

      Delete
  7. Replies
    1. Send me the error snapshot here-> https://www.facebook.com/imsumandeyy

      Delete
  8. private void loadWebsite() in this function getApplication() error

    ReplyDelete
    Replies
    1. Send me the error snapshot here-> www.facebook.com/imsumandeyy

      Delete
  9. Replies
    1. I see that you contact me in my email I can help you please elaborate your problem using snapshot so I can batter understand your problem 😊😊

      Delete
  10. It worked perfectly, great job, but when I touch in the middle of video, it no reproduce. Can you help me?

    ReplyDelete
    Replies
    1. I can not understand your question can you explain it more clearly what do you mean by touching in the middle of the video??

      Delete
  11. 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/");
    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);
    }
    }

    ReplyDelete

  12. 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.

    ReplyDelete
    Replies
    1. 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 😊

      Delete
  13. When I leave the fullscreen it creates a white space on top, how can it be fixed?

    ReplyDelete
    Replies
    1. I have a similar problem as the video screen goes black when leaving full screen, but audio plays on.

      Delete
  14. thankyou man its too good! :) Absolutly work fine.
    can you please make tutorial on (New Tab " + ")

    ReplyDelete
  15. Thanks a lot!! Works like a charm!

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

    ReplyDelete
  17. Gr8 job , it really helpful for me thanks

    ReplyDelete
  18. 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

    ReplyDelete
    Replies
    1. Wow awesome buddy you solved the bugs and that is amazing...

      Delete
  19. 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

    ReplyDelete
  20. Replies
    1. I have added a simplified version which supports screen rotation here https://stackoverflow.com/a/56186877/6478047

      Delete
  21. how to change orientation when entered full screen?

    ReplyDelete
    Replies
    1. I ALSO MAKE A TUTORIAL FOR THIS JUST FOLLOW THIS LINK- https://monstertechno.page.link/pPWNxYnvwe3ofNi76

      Delete
  22. 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);

    ReplyDelete

Post a Comment