Search code examples
javaandroidandroid-layoutandroid-viewandroidx

Why is my layout not filling the screen in landscape mode?


I have the following layout for a video player:

video_view.xml

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

    <com.google.android.exoplayer2.ui.AspectRatioFrameLayout
        android:id="@+id/aspectRatioLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center">

        <SurfaceView
            android:id="@+id/surfaceView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <ImageView
            android:id="@+id/posterImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@string/poster_image_description" />

        <com.google.android.exoplayer2.ui.SubtitleView
            android:id="@+id/subtitleView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <com.google.android.exoplayer2.ui.PlayerControlView
            android:id="@+id/nativeControls"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </com.google.android.exoplayer2.ui.PlayerControlView>

        <com.sdk.adapters.controls.ControlBar
            android:id="@+id/controls"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

        </com.sdk.adapters.controls.ControlBar>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout
                android:id="@+id/messageOverlay"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="#80000000">
                <TextView
                    android:id="@+id/messageOverlayText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:text="@string/message_overlay"
                    android:textColor="@android:color/white"
                    android:textSize="30sp" />
            </FrameLayout>
        </FrameLayout>
    </com.google.android.exoplayer2.ui.AspectRatioFrameLayout>
</FrameLayout>

The ControlBar is defined as:

control_bar_view.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#80000000">

    <!-- BEGIN: Strut -->
    <View
        android:id="@+id/exitPlaceholder"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent="0.1" />
    <!-- END: Strut -->

    <TextView
        android:id="@+id/broadcastName"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:text="@string/broadcast_name"
        android:textColor="#ffffff"
        android:textSize="18sp"
        app:layout_constraintEnd_toStartOf="@id/castButton"
        app:layout_constraintStart_toEndOf="@id/exitPlaceholder"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/siteName"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:text="@string/site_name"
        android:textColor="#ffffff"
        android:textSize="18sp"
        app:layout_constraintEnd_toStartOf="@id/castButton"
        app:layout_constraintStart_toEndOf="@id/exitPlaceholder"
        app:layout_constraintTop_toBottomOf="@id/broadcastName" />

    <ImageButton
        android:id="@+id/castButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="10dp"
        android:contentDescription="@string/send_to_google_chromecast"
        android:visibility="invisible"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toStartOf="@id/airplayButton"
        app:layout_constraintHeight_percent="0.1"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/airplayButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="10dp"
        android:contentDescription="@string/send_to_apple_airplay"
        android:visibility="invisible"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toStartOf="@id/optionsButton"
        app:layout_constraintHeight_percent="0.1"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/optionsButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="10dp"
        android:background="@android:color/transparent"
        android:contentDescription="@string/fullscreen"
        android:scaleType="fitStart"
        android:src="@drawable/closed_caption_gray"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent="0.1"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/skipBackButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/transparent"
        android:contentDescription="@string/skip_back_button"
        android:scaleType="fitStart"
        android:src="@drawable/back_30"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toStartOf="@id/playPauseToggle"
        app:layout_constraintHeight_percent="0.20"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/playPauseToggle"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/transparent"
        android:contentDescription="@string/play_button"
        android:scaleType="fitStart"
        android:src="@drawable/play"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toStartOf="@id/skipForwardButton"
        app:layout_constraintHeight_percent="0.25"
        app:layout_constraintStart_toEndOf="@id/skipBackButton"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/skipForwardButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/transparent"
        android:contentDescription="@string/skip_forward_button"
        android:scaleType="fitStart"
        android:src="@drawable/forward_30"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent="0.20"
        app:layout_constraintStart_toEndOf="@id/playPauseToggle"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/currentTime"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_margin="10dp"
        android:text="@string/null_time"
        android:textColor="#ffffff"
        android:textSize="18sp"
        app:layout_constraintBottom_toTopOf="@id/seekBar"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/remainingTime"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_margin="10dp"
        android:text="@string/null_time"
        android:textColor="#ffffff"
        android:textSize="18sp"
        app:layout_constraintBottom_toTopOf="@id/seekBar"
        app:layout_constraintEnd_toEndOf="parent" />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:progress="50"
        android:progressDrawable="@drawable/seek_bar"
        android:thumb="@drawable/seek_thumb"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/fullscreenButton"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/fullscreenButton"
        app:layout_constraintVertical_bias="0.0" />

    <ImageButton
        android:id="@+id/fullscreenButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="10dp"
        android:background="@android:color/transparent"
        android:contentDescription="@string/fullscreen"
        android:scaleType="fitStart"
        android:src="@drawable/fullscreen"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent="0.1" />


</androidx.constraintlayout.widget.ConstraintLayout>

In the layout editor in Android Studio, this looks good:

enter image description here

However when I run an actual app, the ControlBar (and only the ControlBar) isn't filling the screen! Everything else stretches to fill the screen perfectly:

enter image description here

What am I doing wrong?

If it helps, the ControlBar is a custom View which extends ConstraintLayout. Here's what the initialization code looks like:

ControlBar.java

public class ControlBar extends ConstraintLayout
{
    private BFPlayer    player;
    private TextView    broadcastName;
    // ... references for the various subviews ...
    private ImageButton fullscreenButton;

    public ControlBar ( Context context )
    {
        super(context);
        init(context);
    }

    public ControlBar ( Context context, AttributeSet attrs )
    {
        super(context, attrs);
        init(context);
    }

    public ControlBar ( Context context, AttributeSet attrs, int defStyleAttr )
    {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init ( Context context )
    {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View           view     = inflater.inflate(R.layout.bf_control_bar_view, null);
        addView(view);

        broadcastName = findViewById(R.id.broadcastName);
        // ... grab references to the various buttons ...
        fullscreenButton = findViewById(R.id.fullscreenButton);

        // ... set up listeners for the various buttons ...
    }

    // ... The rest of the class ...
}

What am I doing wrong? I'm sure it's something simple, but I've been staring for a minute and can't figure it out. (Note: I'm pretty new to Android development, and not very experienced. My job has me changing platforms and programming languages very often)


Solution

  • You're not telling it what the parent is when you inflate your View, so it doesn't apply the LayoutParams correctly:

    View view = inflater.inflate(R.layout.bf_control_bar_view, null);
    addView(view);
    

    Instead, tell it to inflate directly into the parent:

    View view = inflater.inflate(R.layout.bf_control_bar_view, this);