Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Layout width and height being ignored

Tags:

android

layout

I have a dialog with a layout inside and a SurfaceTexture with a video stream. When I receive the width and height from the video, I resize my layout like this:

private void resizeView(final VideoFormatInfo info) {
        final Size size = calculateSize(info.getWidth(), info.getHeight());
        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {

                final ViewGroup.LayoutParams layoutParams = mInnerLayout.getLayoutParams();
                layoutParams.width = size.x;
                layoutParams.height = size.y;
                Log.i(TAG, String.format("run: setting innerlayout bounds to %d,%d", size.x, size.y));
                mInnerLayout.setLayoutParams(layoutParams);
            }
        });
    }

Now I have a fullscreen button that is supposed to resize the layout to the whole screen. But when I press it, the layout remains in a small area of the screen.

When I check the log the proper value on size.x and size.y is there (the bounds of the screen), yet the layout is not properly resized.

The innerlayout is added to a customView named "VideoPlayer". I set the color of the videoplayer background to red so when I switch to fullscreen the whole screen turns red, except for the video stream in the middle. This means that the underlying view is being properly resized but the innerLayout is not for some reason.

Funny thing is, I have another layout over the video render that creates a "flash effect" to simulate a camera flash when taking a snapshot. When that flash effect is triggered, then the video is resized to the whole screen.

So this is my layout tree:

VideoPlayerView (CustomView, not VideoView)
     innerLayout (RelativeLayout)
         videoSurfaceTexture (SurfaceTextureView)
         flashLayout (RelativeLayout)

I also set this for debugging:

 @Override
            public void onSurfaceTextureSizeChanged(final SurfaceTexture surfaceTexture, final int width, final int height) {
                Log.d(TAG, "onSurfaceTextureSizeChanged size=" + width + "x" + height + ", st=" + surfaceTexture);

                Log.i(TAG, String.format("innerlayout bounds are %d,%d", mInnerLayout.getLayoutParams().width, mInnerLayout.getLayoutParams().height));
            }

And the values on the inner layout are the proper values (those of the whole screen) when I press fullscreen, but the layout is not resized. I can tell it's the layout not being resized because I changed its background color to green and added some padding and I can see it in the center of screen taking a small space.

It looks as though somehow the view is not being updated with the layout changes.

I am running out of ideas here. I tried invalidate(), postInvalidate() and forceLayout() but those dont work.

like image 291
Gabriel Sanmartin Avatar asked Aug 16 '16 09:08

Gabriel Sanmartin


People also ask

What is layout width?

android:layout_widthSpecifies the basic width of the view. This is a required attribute for any view inside of a containing layout manager. Its value may be a dimension (such as "12dip") for a constant width or one of the special constants.

How do you find the width of a layout?

If you wanted to get the actual width of your view you would have to call getWidth() on your view once the layout has been inflated and displayed. Calling getWidth() before your layout has been displayed will result in 0.


1 Answers

You missed one important part of forceLayout():

This method does not call requestLayout() or forceLayout() on the parent.

So make the parent do a layout as well:

mInnerLayout.setLayoutParams(layoutParams);
mInnerLayout.forceLayout();
mInnerLayout.getParent().requestLayout();
like image 171
tynn Avatar answered Sep 19 '22 13:09

tynn