Android Video does not match the width in the portrait of the video image and does not occupy the entire screen in the landscape

Android video does not match the width of the video when in portrait, how can I make the width of my video according to the width of my video when in portrait, and when I change the screen orientation to landscape, I would like the video to fill the entire screen. how the YouTube app does it. when in the portrait the video should start from the top to the middle of the screen, and when in the landscape fill the entire screen. here is what i tried, i also have full screenshot links

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:weightSum="100" > <VideoView android:id="@+id/videoview" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_gravity="center" android:layout_weight="53" > </VideoView> <LinearLayout android:id="@+id/linearLayout" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="7" android:background="@drawable/top_bar" android:orientation="horizontal" > <LinearLayout android:id="@+id/settings" android:layout_width="50dp" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" android:paddingBottom="5dp" android:paddingTop="5dp" > <ImageView android:id="@+id/set" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/desc" android:src="@drawable/settings" /> </LinearLayout> <LinearLayout android:id="@+id/low_tab" android:layout_width="70dp" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" android:paddingBottom="5dp" android:paddingTop="5dp" > <ImageView android:id="@+id/low" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/desc" android:src="@drawable/low" /> </LinearLayout> <LinearLayout android:id="@+id/high_tab" android:layout_width="80dp" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" android:paddingBottom="5dp" android:paddingTop="5dp" > <ImageView android:id="@+id/high" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/desc" android:src="@drawable/high" /> </LinearLayout> <LinearLayout android:id="@+id/audio_tab" android:layout_width="70dp" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" android:paddingBottom="5dp" android:paddingTop="5dp" > <ImageView android:id="@+id/audio" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/desc" android:src="@drawable/audio" /> </LinearLayout> <LinearLayout android:id="@+id/full_screen" android:layout_width="30dp" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" android:paddingBottom="5dp" android:paddingTop="5dp" > <ImageView android:id="@+id/full" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/desc" android:src="@drawable/full" /> </LinearLayout> <LinearLayout android:id="@+id/fresh" android:layout_width="30dp" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" android:paddingBottom="5dp" android:paddingTop="5dp" > <ImageView android:id="@+id/refresh" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/desc" android:src="@drawable/refresh" /> </LinearLayout> </LinearLayout> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="7" android:background="@drawable/comment_bar" android:orientation="horizontal" > <LinearLayout android:id="@+id/liveblog_tab" android:layout_width="80dp" android:layout_height="wrap_content" android:background="@drawable/select" android:gravity="center" android:orientation="vertical" android:paddingBottom="5dp" android:paddingTop="5dp" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/desc" android:src="@drawable/live_blog" /> </LinearLayout> <LinearLayout android:id="@+id/addcom_tab" android:layout_width="100dp" android:layout_height="wrap_content" android:background="@drawable/select" android:gravity="center" android:orientation="vertical" android:paddingBottom="5dp" android:paddingLeft="15dp" android:paddingTop="5dp" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/desc" android:src="@drawable/add_comment" /> </LinearLayout> </LinearLayout> <FrameLayout android:id="@+id/frame" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="33" > <LinearLayout android:id="@+id/liveblog" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <WebView android:id="@+id/browser" android:layout_width="fill_parent" android:layout_height="fill_parent" > </WebView> </LinearLayout> <LinearLayout android:id="@+id/comments" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout> </FrameLayout> </LinearLayout> 

enter image description here

+4
source share
2 answers

You need to have two different layouts. One for the portrait and one for the landscape. Create two xml files with the same name and put them in the "layout-land" folders for the landscape and the "layout port" for the portrait.

And here you can see how to handle orientation changes.

It can be a layout to make video viewing in full screen.

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <VideoView android:id="@+id/myvideoview" android:layout_width="fill_parent" android:layout_alignParentRight="true" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" android:layout_height="fill_parent"> </VideoView> </RelativeLayout> 

EDIT: This is how you can handle this in a method.

 @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(); setContentView(Your Landscape layout); } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show(); setContentView(Your portrait layout); } } 
+11
source

For linearlayout, I'm not sure. But using relativelayout , you can do it as shown below.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <VideoView android:id="@+id/videoview" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" > </VideoView> //your other xml content .... .... .... </RelativeLayout> 

And if you want the videoview to be filled with height. then you need to do this code:

 <VideoView android:id="@+id/videoview" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_alignParentBottom="true"> </VideoView> //your other xml content .... .... .... </RelativeLayout> 

Let me know if this helped. Thanks.

+1
source

Source: https://habr.com/ru/post/1496987/


All Articles