5

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.

user2041902
  • 593
  • 1
  • 6
  • 21

7 Answers7

3

*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();
        }
    }
Sowmia Sundararajan
  • 1,613
  • 2
  • 14
  • 17
  • new LayoutParams(width, height) doesnt work for api level less than 11. So i tried LinearLayout.LayoutParams(width, height) for the webview as well as the layout in which I have placed the webView but still the problem persists – user2041902 May 09 '13 at 06:06
  • @sowmia If you are unable to communicate because you can't comment, mail to my gmail id praveen.3490@gmail.com – pvn May 09 '13 at 06:51
  • post the layout xml and oncongifurationchanged method.i ll try to figure out the problem. You can also refer http://stackoverflow.com/questions/1002085/android-webview-handling-orientation-changes?rq=1 – Sowmia Sundararajan May 09 '13 at 08:51
1

Try:

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

    webView.setLayoutParams(new RelativeLayout.LayoutParams(
                      ViewGroup.LayoutParams.WRAP_CONTENT,
                      ViewGroup.LayoutParams.WRAP_CONTENT));
}
Arash Sharif
  • 542
  • 7
  • 16
  • This didn't work for me, on changing orientation the app collapsed to a narrow stip across the top of the screen. The app was still running and when I switched back the strip reoriented but stayed a strip. By the way for those who want to try it you also need: - import android.content.res.Configuration; import android.view.ViewGroup; import android.widget.RelativeLayout; – Mark Kortink Jun 11 '21 at 07:35
0

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

<activity android:configChanges="orientation|screenSize">
Nadav Finish
  • 1,120
  • 9
  • 7
0

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

    }
tej shah
  • 2,995
  • 2
  • 25
  • 35
  • This didn't work for me, on changing orientation the app disappeared and I was thrown to the home screen. The app was still running and when I switched to it it was cropped. By the way for those who want to try it you also need: - import android.content.res.Configuration; import android.util.DisplayMetrics; import android.widget.LinearLayout; – Mark Kortink Jun 11 '21 at 07:23
0

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.

allen WOE
  • 1
  • 4
0

In fragments you have to use FrameLayout for WebView

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    int width = FrameLayout.LayoutParams.MATCH_PARENT;
    int height = FrameLayout.LayoutParams.WRAP_CONTENT;
    your_webview.setLayoutParams(new FrameLayout.LayoutParams(width, height));
}
Wowo Ot
  • 1,362
  • 13
  • 21
  • This didn't work for me, on changing orientation the app disappeared and I was thrown to the home screen. The app was still running and when I switched to it it was cropped. By the way for those who want to try it you also need: - import android.content.res.Configuration; import android.widget.FrameLayout; – Mark Kortink Jun 11 '21 at 07:03
0

My answer is very similar to the above, most closely to @Arash Sharif. This is the variant that worked for me.

Context: My app is a web app and uses only HTML, CSS and JavaScript. I run it as an app on Android by wrapping it in a WebView control. The app is designed to run full screen and I need it to resize when the screen gets reoriented.

I work in Android Studio. I am neither an Android nor Java programmer, so my explanation may not be in the right lingo but I am pretty sure it is valid.

First you need to tell Android that you will handle screen events.

AndoidManifest.xml

        android:configChanges="orientation|screenSize|screenLayout|keyboardHidden">
        <activity android:name="com.babyclix.babyclix_test.MainActivity"
            android:configChanges="orientation|screenSize|screenLayout|keyboardHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Now Android will pass screen events to you to handle, so you need to over-ride the function that handles screen events.

MainActivity.java

// For screen handling on changes (eg. reorientation, resizing)
import android.content.res.Configuration;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

// your stuff

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mywebview.setLayoutParams(new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
    }

// more stuff

Mark Kortink
  • 1,770
  • 4
  • 21
  • 36