0

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 the video view with half the size of my layout.

<RelativeLayout 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: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="300dp" />

</RelativeLayout>
Chit Khine
  • 830
  • 1
  • 13
  • 34

4 Answers4

1

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

DisplayMetrics displayMetrics = new DisplayMetrics();

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;
Kevin Murvie
  • 2,592
  • 1
  • 25
  • 43
0

you can get the height and width by using

DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    mScreenWidth = displayMetrics.widthPixels;
    mScreenHeight = displayMetrics.heightPixels;
sadat
  • 4,004
  • 2
  • 29
  • 49
0

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

    final RelativeLayout layout = (RelativeLayout)findViewById(R.id.your_layout);
    ViewTreeObserver vto = layout.getViewTreeObserver();

    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            VideoView videoView = layout.findViewById(R.id.videoViewSmile);
            ViewGroup.LayoutParams layoutParams = new ViewGroup.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

Joe Maher
  • 5,354
  • 5
  • 28
  • 44
0

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>
RaghavPai
  • 652
  • 9
  • 14