Skip to content Skip to sidebar Skip to footer

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

Solution 1:

*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();
        }
    }

Solution 2:

Try:

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

    webView.setLayoutParams(new RelativeLayout.LayoutParams(
                      ViewGroup.LayoutParams.WRAP_CONTENT,
                      ViewGroup.LayoutParams.WRAP_CONTENT));
}

Solution 3:

Just add the following line to your activity in the manifest file:

<activity android:configChanges="orientation|screenSize">

Solution 4:

facing same issue. and solve it with best way

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

        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int height = displayMetrics.heightPixels;
        int width = displayMetrics.widthPixels;
        mWebview.setLayoutParams(new LinearLayout.LayoutParams(
                width,
                height-100));

    }

Solution 5:

As far as I can see, there must be some error in H5 page, similar resize problem

Just use other app (like chrome browser) test your url.


Post a Comment for "WebView Not Re Sizing Properly On Orientation Change"