1

I am trying to use SurfaceView. So I created a SurfaceView widget in the layout file as shown below in the code. The problem I have is how to set a weight to the SurfaceView? As shown below in the code I set :

android:layout_weight=".4"

but still the SurfaceView occupies the eintire screen, what i want is to make the SurfaceView occupies only 40% of the screen. I also refered to a post in this link SurfaceView in Layout but it does not show how to set the weight to the SurfaceView from the xml file.

Please let me know how to set weight to the SurfaceView from the xml file.

code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.example.surfaceview_00.MainActivity">

<SurfaceView
    android:id="@+id/mainActivity_surfaceView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight=".4"/>
</RelativeLayout>

update:

I also tried the below code but still i got one SurfaceView that occupies the whole screen. I expected after running the below code is to have two SurfaceViews each occupies half of the screen, but i got only one

code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.example.surfaceview_00.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="2">

    <SurfaceView
        android:id="@+id/mainActivity_SurfaceView1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <SurfaceView
        android:id="@+id/mainActivity_SurfaceView2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
</LinearLayout>

</RelativeLayout>
Community
  • 1
  • 1
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • " but still i got one SurfaceView that occupies the whole screen. " How do you know there is only one SurfaceView? – Code-Apprentice Nov 10 '16 at 15:54
  • @Code-Apprentice because i stated in the code iposted in the update section above. i have two surfaceview inside a linearlayout and each of them is assigned to layout_weoght=.5, and at run time i there is only one surfaceview appears and it occupies the whole screen – Amrmsmb Nov 11 '16 at 06:52

3 Answers3

2

RelativeLayout does not support layout_weight. You need to use the LinearLayoutfor that.

Read more at https://developer.android.com/guide/topics/ui/layout/linear.html

But basically change it to something like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/activity_main"
      android:layout_width="match_parent"
      android:orientation="vertical"
      android:layout_height="match_parent"
      android:weightSum="1">

         <SurfaceView
              android:id="@+id/mainActivity_surfaceView"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight=".4"/>

</LinearLayout>
malmling
  • 2,398
  • 4
  • 19
  • 33
1

android:layout_weight is only used by LinearLayout. Since you use RelativeLayout it is ignored.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

SurfaceView doesn't belong to App's view hierarchy. It creates separate layer on the background, and position it depending on its size. All other views are displayed over SurfaceView. To do what you want you need to use TextureView.

Maxim Metelskiy
  • 1,389
  • 14
  • 29