Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert my linearLayout to GridView/GridLayout

How can I convert my linear layout as seen below to a grid view? So basically I need it to be 3 columns and 2 rows (in landscape orientation). No borders also.

The sample project is uploaded here.

enter image description here

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

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

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

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

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

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

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

    </LinearLayout>

UPDATE: And this is my onCreate() method and initialization in the class:

public class MultipleVideoPlayActivity extends Activity implements OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback {

    private static final String TAG = "MediaPlayer";
    private static final int[] SURFACE_RES_IDS = { R.id.video_1_surfaceview, R.id.video_2_surfaceview };

    private MediaPlayer[] mMediaPlayers = new MediaPlayer[SURFACE_RES_IDS.length];
    private SurfaceView[] mSurfaceViews = new SurfaceView[SURFACE_RES_IDS.length];
    private SurfaceHolder[] mSurfaceHolders = new SurfaceHolder[SURFACE_RES_IDS.length];
    private boolean[] mSizeKnown = new boolean[SURFACE_RES_IDS.length];
    private boolean[] mVideoReady = new boolean[SURFACE_RES_IDS.length];
    int i = 0; // index of video playout

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
         setContentView(R.layout.multi_videos_layout);

        // create surface holders
        for (int i = 0; i < mSurfaceViews.length; i++) {
            mSurfaceViews[i] = (SurfaceView) findViewById(SURFACE_RES_IDS[i]);
            mSurfaceHolders[i] = mSurfaceViews[i].getHolder();
            mSurfaceHolders[i].addCallback(this);
            mSurfaceHolders[i].setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }
    }
like image 652
Tina J Avatar asked Feb 07 '23 18:02

Tina J


1 Answers

The simplest way which worked for me was using nested linearLayout, as below (2x2). The only problem I have right now is I don't know how to set the height of horizontal layouts be exactly half of screen height!

Anyone has any ideas for that?

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <SurfaceView
            android:id="@+id/video_1_surfaceview"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_weight="1" />

        <SurfaceView
            android:id="@+id/video_2_surfaceview"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <SurfaceView
            android:id="@+id/video_3_surfaceview"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_weight="1" />

        <SurfaceView
            android:id="@+id/video_4_surfaceview"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>
like image 152
Tina J Avatar answered Feb 20 '23 19:02

Tina J