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
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.
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?
Try this
Well, I may be too late. But, what I do :
AndroidManifest.xml
android:configChanges="orientation"
WebViewContainerActivity.java
It does it's work efficiently, and I must use this because I don't understand HTML.
Why not
wrap_content
for width in the landscape layout and then set thelayout_gravity
to"center"
?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 thecss
file or asstyle
attribute in the tag):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.