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

Post a Comment

Popular posts from this blog

A Flash Light App in Sketchware

Create Music Player app in Sketchware

In Sketchware: How to create a calculator for any simple formula?

Simple car racing android game in Sketchware

Firebase auth in Sketchware for Login Activity