Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView not re sizing properly on orientation change

I am loading a video url into webview using webview.loadUrl(). The player is encoded in the html itself so I only have to load the url into web view.

In case if I dont give android:configChanges="orientation" in my manifest everything works fine. The webview resizes properly on orientation change and the video is adjusted to fit the screen properly, ofcourse then the video restarts.

Therefore I gave android:configChanges="orientation" in manifest so that the video doesnt reload. Now although the video doesn't reload and continues to play but the video/webview doesnt resize properly and half of the video goes off screen if the device was initially in portrait and then changed to landscape. In case the device was initially in landscape mode and then is changed to portrait mode only half of the screen is used.

Please reply if you can figure out a way or have any suggestions.

like image 762
user2041902 Avatar asked Oct 04 '22 04:10

user2041902


1 Answers

*if configchanges = "orientation" - not specified,* the android system handles the change and restarts the layout, so layout is freshly created and video loads from start.

if android:configchanges = "orientation" , - This tell the system that this activity will handle the orientation change by it self. so,the android system will not load the layout refreshly . so video continues to play .The width of the parent layout will not change . so width in portrait mode is used in landscape . so , ur webview seems small.

you need to override the onconfigurationchanged () in activity and handle the orientation change. You need to specify the layoutparams for layout.

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
            // for example the width of a layout  
            int width = 300;
            int height = LayoutParams.WRAP_CONTENT;
            WebView childLayout = (WebView) findViewById(R.id.webview);
            childLayout.setLayoutParams(new LayoutParams(width, height));
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        }
    }
like image 51
sowmia Avatar answered Oct 10 '22 02:10

sowmia