Enable Fullscreen for Youtube videos in WebView

To enable fullscreen mode for YouTube videos in your sketchware project, follow the steps given below.

1. Create a new project in Sketchware.

2. In VIEW area add a WebView webview1.

3. Create a more block extra.

4. In the more block extra, use an add source directly blocks and put codes to define a new WebChromeClient class with name CustomWebClient.
}

public class CustomWebClient extends WebChromeClient {
private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
protected FrameLayout frame;

// Initially mOriginalOrientation is set to Landscape
private int mOriginalOrientation = android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
private int mOriginalSystemUiVisibility;

// Constructor for CustomWebClient
public CustomWebClient() {}

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

public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback viewCallback) {
if (this.mCustomView != null) {
onHideCustomView();
return; }
this.mCustomView = paramView;
this.mOriginalSystemUiVisibility = MainActivity.this.getWindow().getDecorView().getSystemUiVisibility();
// When CustomView is shown screen orientation changes to mOriginalOrientation (Landscape).
MainActivity.this.setRequestedOrientation(this.mOriginalOrientation);
// After that mOriginalOrientation is set to portrait.
this.mOriginalOrientation = android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
this.mCustomViewCallback = viewCallback; ((FrameLayout)MainActivity.this.getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1)); MainActivity.this.getWindow().getDecorView().setSystemUiVisibility(3846);
}

public void onHideCustomView() {
((FrameLayout)MainActivity.this.getWindow().getDecorView()).removeView(this.mCustomView);
this.mCustomView = null;
MainActivity.this.getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
// When CustomView is hidden, screen orientation is set to mOriginalOrientation (portrait).
MainActivity.this.setRequestedOrientation(this.mOriginalOrientation);
// After that mOriginalOrientation is set to landscape.
this.mOriginalOrientation = android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; this.mCustomViewCallback.onCustomViewHidden();
this.mCustomViewCallback = null;
}
}

{

5. In onCreate event, use add source directly block and put codes to create a new CustomWebClient and set it as the WebChromeClient for the WebView.
webview1.setWebChromeClient(new CustomWebClient());

After that use webview loadUrl block to load youtube.

6. Now add a new event, 'On back button press', in LOGIC area of your app. In this event use if..else.. block. Add Webview goBack block as shown in image below. It should read, if WebView canGoBack then WebView goBack else Finish Activity.

7. Save and Run the project. You can now watch YouTube videos in fullscreen mode in your app.

Comments

  1. Sir I have created the quiz app using Json data. but after putting my own Json data the app crashes
    And make video how to automatically change the question in Json quiz app after certain delay of time and also show the timer

    ReplyDelete
    Replies
    1. Paste this code (oncreate) to not crashes app

      (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);

      Delete
    2. Thanks me later!

      I am "mark app studio philippines"

      Delete
  2. Hey bro can you create how to aeccac all widgets like button or image view in view pager ? Please...

    ReplyDelete
  3. Hello I can add Videos Ads in app Using Sketchware , I want to show other user how, I will Contact U

    ReplyDelete
  4. hello i need ur help please i want to creat a chat room app how i can do it? i mean i need to do if user press on image (+) will see pop ask about room name ? if user type name of room,then creat a room

    ReplyDelete
  5. Hi, I was wondering if it is possible to modify the video player of a webview instead of using the default device player

    ReplyDelete
  6. Help me!
    I think it works only on main screen!
    I'm trying to add it on second screen but it is giving errors!
    Kindly tell me that what should i do for running youtube in full screen on second screen!
    Thank you!

    ReplyDelete
    Replies
    1. Hello, I'm having this exact problem right now, did you get it right?

      Delete
    2. Copy the same come code and edit the webview1 into whichever webview you want

      Delete
    3. Just change the main activity name into current page where webview exists

      Delete
  7. can i active full screen option in any video

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. Sir plzz help

    How to get video view center of screen and how to change background color

    Plz plz help me

    ReplyDelete
    Replies
    1. WebView webview1; webview1=(WebView)findViewById(R.id.webview1); webview1.setBackgroundColor(0x00000000);

      Delete
  10. Its working but it is not displaying the ads by google in the website.. Is there any solution for that??

    ReplyDelete
  11. Thanks you so much bhai (Love you a lot) Thank you so much

    ReplyDelete
  12. Bro I want to use YouTubePlayerView in Sketchware Pro. I wanna enable full screen button click. I can view the button but I can't go to full screen while clicking on it.. It seems to a dummy button now.

    ReplyDelete
  13. Why click resize or press back is porce. Close app

    ReplyDelete
  14. its work but when i try to add this full screen + enable file pic .. just the full screen work

    ReplyDelete
  15. How to create video full screen of other activities without main activity


    This code only supports the main activity. Is there any solution for this?

    1)
    webview1.setWebViewClient(new Browser()); webview1.setWebChromeClient(new MyWebClient());


    2)

    } final class Browser extends WebViewClient { Browser() {} public boolean shouldOverrideUrlLoading(WebView paramWebView, String paramString) { paramWebView.loadUrl(paramString); return true; } } public class MyWebClient extends WebChromeClient { private View mCustomView; private WebChromeClient.CustomViewCallback mCustomViewCallback; protected FrameLayout mFullscreenContainer; private int mOriginalOrientation = MainActivity.this.getRequestedOrientation(); private int mOriginalSystemUiVisibility; public MyWebClient() {} public Bitmap getDefaultVideoPoster() { if (MainActivity.this == null) { return null; } return BitmapFactory.decodeResource(MainActivity.this.getApplicationContext().getResources(), 2130837573); } public void onHideCustomView() { ((FrameLayout)MainActivity.this.getWindow().getDecorView()).removeView(this.mCustomView); this.mCustomView = null; MainActivity.this.getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility); MainActivity.this.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 = MainActivity.this.getWindow().getDecorView().getSystemUiVisibility(); MainActivity.this.setRequestedOrientation(android.content.pm.ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE); this.mCustomViewCallback = paramCustomViewCallback; ((FrameLayout)MainActivity.this.getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1)); MainActivity.this.getWindow().getDecorView().setSystemUiVisibility(3846); }

    ReplyDelete
  16. I have been using Sketchware for quite some time, and I always questioned why I couldn't watch YouTube videos in fullscreen mode. I tried to enable it but did not gain. Reading your post has helped me a lot and now I can enjoy fullscreen mode. Dissertation Writing Services

    ReplyDelete

Post a Comment

Popular posts from this blog

Simple car racing android game in Sketchware

Creating a Drawing View in Sketchware

How to enable upload from webview in Sketchware?

List of Calendar Format symbols valid in Sketchware