Andrew Cross
Andrew Cross

Reputation: 1911

Programmatically set layout_gravity for a FrameLayout's Child?

This seems like it should be trivial, but I can't figure out how to set the layout_gravity for a FrameLayout's child programmatically for the life of me.

There's a ton of similar questions on SO, but after trying a good 10 of them, none of the setGravity, new FrameLayout.LayoutParams, etc. solutions seem to work.

Below is a simplified version of what I'm trying to achieve.

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextureView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"/>
</FrameLayout>

However, for external reasons, I'm adding the TextureView to the FrameLayout programatically.

mCameraPreview = new CameraPreview(this, recordMode);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview_wrapper);
preview.addView(mCameraPreview);

When I try to set the layout_gravity on the mCameraPreview, it only gives me an option for gravity, which I don't want.

Is there something I'm missing here?

Upvotes: 9

Views: 7792

Answers (1)

Barend
Barend

Reputation: 17444

The layout_gravity attribute lands on the FrameLayout.LayoutParams, not on the view itself. You'll need something like:

mCameraPreview = new CameraPreview(this, recordMode);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview_wrapper);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
    FrameLayout.LayoutParams.MATCH_PARENT, 
    FrameLayout.LayoutParams.MATCH_PARENT, 
    Gravity.BOTTOM);
preview.addView(mCameraPreview, params);

Upvotes: 11

Related Questions