3

I have a layout below where i have a linear layout with gravity as center to make the child center aligned.I want to add a webview programatically and loading the youtube video in it.The issue is the webview's height and width is wrap_content,wrap_content.So in portrait mode webview works fine and aligns at center but when the change the orientation to landscape web view becomes fill_parent and covers the screen horizontally so the content doesn't look at center. here is the layout file

and here the activity code

Below are the image which shows result in portrait mode which is fine. Linear layout is in green color and webview is in blue color

and here is when u rotate the device to landscape mode where webview in blue will stretch itself to fill the entire screen width wise.

enter image description here

I want the webview to be placed center in landscape mode also likewise it is in portrait mode?Please help me understanding this and any fix?

anshul
  • 982
  • 1
  • 11
  • 33

4 Answers4

1

If you load video in WebView, so aligment in the WebView is html task. Wrap your video in the <div> and add appropriate style (in the css file or as style attribute in the tag):

<div style="margin:0 auto;width:500px;">
    //your video object is placed here
</div>

As well you can play a video in the VideoView but this is technique will be a little more difficult. Briefly, you have to add a FrameLayout above the WebView, get the coords where the VideoView should be placed through the the html file and apply LayoutParams with coords for VideoView. If orientation will be changed, VideoView should be refreshed with the new coords. If you interested in this, i can share more details.

validcat
  • 6,158
  • 2
  • 29
  • 38
1

Well, I may be too late. But, what I do :

AndroidManifest.xml

android:configChanges="orientation"

WebViewContainerActivity.java

@Override
public void onConfigurationChanged(Configuration newConfig) {
    /* WebView mainView is declared globally */
    ConstraintLayout lana = findViewById(R.id.main);
    lana.removeView(mainView);
    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int screenWidth = displaymetrics.widthPixels;
    int screenHeight = displaymetrics.heightPixels;
    lana.addView(mainView, 0,
            new ConstraintLayout.LayoutParams(
                    screenWidth,
                    screenHeight));
    super.onConfigurationChanged(newConfig);
}

It does it's work efficiently, and I must use this because I don't understand HTML.

exploitr
  • 843
  • 1
  • 14
  • 27
0

Why not wrap_content for width in the landscape layout and then set the layout_gravity to "center"?

dcow
  • 7,765
  • 3
  • 45
  • 65
  • you want me to create a separate layout for landscape mode also ?Please elaborate. – anshul Sep 06 '13 at 11:03
  • I thought that's what you were already doing, but if not then yes. – dcow Sep 06 '13 at 11:04
  • The other solution is to simply set the layout gravity on the WebView to center since you're adding it to a linear layout. – dcow Sep 06 '13 at 11:05
  • Also, you know you can inflate the WebView from an xml resource so you don't have to set all the parameters in code. In fact, I don't know why you don't just put the WebView into your layout file and then find it by its id in the Activity. – dcow Sep 06 '13 at 11:09
  • thats a sample only.Implement the same thing u suggest in the real project. – anshul Sep 06 '13 at 11:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36928/discussion-between-anshul-and-david-cowden) – anshul Sep 06 '13 at 11:14
0

Try this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:gravity="center"  >
<LinearLayout
   android:id="@+id/webViewPlaceholder"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="#00AC0F"
   >
</LinearLayout>

Nithinlal
  • 4,845
  • 1
  • 29
  • 40