-1

I converted a website into an app with WebView in Android Studio. Everything is fine but the youtube videos that was in my website does not go in full Screen. I am a beginner, so please help me in solving this.

     Main Activity Code  --- 


import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.KeyEvent;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

import java.net.URI;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    //initializing WebView
    private WebView mwebView;
    private FrameLayout customViewContainer;
    private WebChromeClient.CustomViewCallback customViewCallback;
    private View mCustomView;
    private MyWebviewClient mWebChromeClient;






    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        //WebView
        mwebView = (WebView) findViewById(R.id.myWebView);
        WebSettings webSettings = mwebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        //improve webView performance
        mwebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        mwebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        mwebView.getSettings().setAppCacheEnabled(true);
        mwebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        mwebView.setWebChromeClient(new  MyChromeBrowser());

        webSettings.setDomStorageEnabled(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSettings.setUseWideViewPort(true);
        webSettings.setSavePassword(true);
        webSettings.setSaveFormData(true);
        webSettings.setEnableSmoothTransition(true);

        mWebChromeClient = new myWebChromeClient();
            webView.setWebChromeClient(mWebChromeClient);



        mwebView.loadUrl("https://laughonlyblog.wordpress.com");
        //force links open in webview only



    }

    private class MyChromeBrowser extends WebChromeClient {
        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {

            super.onShowCustomView(view, callback);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

        }


        @Override
        public void onHideCustomView() {
            super.onHideCustomView();
        }
    }





    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
            mwebView.loadUrl("http://www.newsweek.com/us");
        } else if (id == R.id.nav_slideshow) {
            mwebView.loadUrl("http://www.newsweek.com/world");
        } else if (id == R.id.nav_manage) {
            mwebView.loadUrl("http://www.newsweek.com/tech-science");
        } else if (id == R.id.nav_gallery) {
            mwebView.loadUrl("http://www.newsweek.com/sports");
        } else if (id == R.id.nav_share) {
            mwebView.loadUrl("http://www.newsweek.com/about-newsweek");
        } else if (id == R.id.nav_send) {
            mwebView.loadUrl("http://www.newsweek.com/contact");
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    private class MyWebviewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
           /* if (Uri.parse(url).getHost().equals("www.laughonlyblog.wordpress.com/about/")) {
                //open url contents in webview
                return false;
            } else {
                //here open external links in external browser or app
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);




          } */
            view.loadUrl(url);
            return true;
        }

        //ProgressDialogue
        ProgressDialog pd = null;


        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            pd=new ProgressDialog(MainActivity.this);
            pd.setTitle("Please Wait..");
            pd.setMessage("Website is Loading..");
            pd.show();
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            pd.dismiss();
            super.onPageFinished(view, url);
        }
    }
    //goto previous page when pressing back button

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_BACK:
                    if (mwebView.canGoBack()) {
                        mwebView.goBack();
                    } else {
                        finish();
                    }
                    return true;
            }
        }
        return super.onKeyDown(keyCode, event);


    }


}
A Ghumaan
  • 3
  • 4
  • [This](https://stackoverflow.com/a/16199649/7461132) answers the question. – Abhi Jun 29 '17 at 19:09
  • @abhi First of all Thanks for replying . Can you tell me Where is video_progress.xml ? I can't find in my project . So,Can please help me . – A Ghumaan Jun 29 '17 at 19:31
  • video_progress.xml exists for the person who answered the question. He has given out his complete project. You need to only implement `showCustomView` & `hideCustomView` method of `WebChromeClient`. – Abhi Jun 29 '17 at 19:33

1 Answers1

1

To achieve this follows these steps :
1) Set WebChromeClient to your webview.
Exmaple : WebView.setWebChromeClient(new MyChromeBrowser());
2) Implement method onShowCustomView() and onHideCustomView() inside your WebChromeClient.
Example : `

private class MyChromeBrowser extends WebChromeClient {
    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        super.onShowCustomView(view, callback);
    }

    @Override
    public void onHideCustomView() {
        super.onHideCustomView();
    }
}`


3) Give android:hardwareAccelerated="true" in manifest file for your activity.
Example : <activity android:name=".MainActivity" android:hardwareAccelerated="true" />
Update :
To fix this try these :
change these lines : webView.setWebChromeClient(new MyWebChromeClient()); to

mWebChromeClient = new myWebChromeClient();
webView.setWebChromeClient(mWebChromeClient);

inside initWebView() method.

To let the system decide the best orientation in full-screen mode add this line to onShowCustomView(View view, CustomViewCallback callback) method:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);


UPDATE : 09-07-2017
Or you can use a VideoEnabledWebView library for full screen viedo player in webview and much more .
Link : GitHub

Amit Sharma
  • 645
  • 5
  • 13
  • First of all Thanks for helping . I follow the steps and When I run the app then the full screen icon is also showing but When I click on that then animation starts and it shows blank screen and When I press back and It comes out . So the full screen is not happening only icon is showing . Hope You understand and solve this issue . – A Ghumaan Jun 29 '17 at 21:32
  • yes i understand . i have updated the answer try again – Amit Sharma Jun 29 '17 at 21:43
  • Dear I do this but error is coming . I also try to make some changes but nothing happens . Maybe I doing wrong . So I am updating my script .Can you please check that. – A Ghumaan Jun 29 '17 at 22:04
  • I tred the way you tell me but error is coming . So Can you please tell me How to solve this issue. Thanks in advance and Waiting for your reply. – A Ghumaan Jun 30 '17 at 13:24
  • can you share your code on github or else where so that i can debug it – Amit Sharma Jul 01 '17 at 17:33
  • I upload to GitHub https://gist.github.com/Anakh1/1292c5cd9c6396c066924f9eeb4a50b4 .I don't know more about Github but I uploaded ,Please Check – A Ghumaan Jul 01 '17 at 20:57
  • I uploaded my code to github . Did you checked . I am waiting for your reply. – A Ghumaan Jul 04 '17 at 16:09
  • I will see it by tomorrow for sure .. currently i'm busy with my projects . – Amit Sharma Jul 04 '17 at 21:25
  • I m waiting for your reply. I hope that you will reply soon . Thanks in Advance – A Ghumaan Jul 08 '17 at 23:49
  • hi @AGhumaan , sorry for late reply i was realy busy in projects i was working on ... Now i found your solution here is the link which i implemented and tested working like charm : here is the link : https://github.com/cprcrack/VideoEnabledWebView – Amit Sharma Jul 09 '17 at 12:42
  • Thank You So Much For your help . You helped me a lot ,Once again Heartily Thank you . – A Ghumaan Jul 11 '17 at 21:54
  • Your most welcome bro . I like the way you try things until it's not solve . Keep it up . Happy coding :) – Amit Sharma Jul 11 '17 at 22:00
  • Can you help me in just one short error . The Video is playing right in full screen but Sometime It start blinking means it come in full screen and go, It was happening in repeat , Everytime When I play for first time and sometimes later also, My app is Completed at all ,Only one error is left to solve,So Can you please help me . Thanks in advance – A Ghumaan Jul 21 '17 at 21:00