Skip to content Skip to sidebar Skip to footer

How Can I Make My Video View To Half The Screen Of My Device?

I am using a video view and a relative layout. The app will only run in portrait mode. Currently, I am giving exact dps to get the desired size. What I would like to have is to get

Solution 1:

First thing, you can get both height and width using an android.Util class called DisplayMetrics :

DisplayMetricsdisplayMetrics=newDisplayMetrics();

And then insert the current display info into the declared DisplayMetrics :

getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

Now you can get the height and width of your screen by using :

displayMetrics.widthPixels // Width
displayMetrics.heightPixels // Height

It is measured in pixels, and you can change the height by :

videoView.getLayoutParams().height = displayMetrics.heightPixels / 2;

Solution 2:

you can get the height and width by using

DisplayMetricsdisplayMetrics= context.getResources().getDisplayMetrics();
    mScreenWidth = displayMetrics.widthPixels;
    mScreenHeight = displayMetrics.heightPixels;

Solution 3:

Easiest way is to put in an invisible view and use layout_weights but that is not the correct way in my opinion. I would use a ViewTreeObserver

finalRelativeLayoutlayout= (RelativeLayout)findViewById(R.id.your_layout);
    ViewTreeObservervto= layout.getViewTreeObserver();

    vto.addOnGlobalLayoutListener(newViewTreeObserver.OnGlobalLayoutListener() {
        @OverridepublicvoidonGlobalLayout() {

            VideoViewvideoView= layout.findViewById(R.id.videoViewSmile);
            ViewGroup.LayoutParamslayoutParams=newViewGroup.LayoutParams(layout.getWidth(), layout.getHeight() / 2)

            videoView.setLayoutParams(layoutParams);
            videoView.invalidate();

            layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });

Not tested, but i hope you see the general idea

Solution 4:

Try using weight feature of linearlayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:opencv="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_smile"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="MyActivity">

<VideoView
    android:id="@+id/videoViewSmile"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5" />

<RelativeLayout
    android:id="@+id/remaining_fifty_percent"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5">
 </RelativeLayout>

 </LinearLayout>

Post a Comment for "How Can I Make My Video View To Half The Screen Of My Device?"