android:radius not working in landscape mode

Viewed 125

I'm trying to make soften the corners of a LinearLayout. It works in portrait mode, but when I switch to landscape mode, it seems to ignore my background drawable file and doesn't have rounded corners. Any ideas why?

I've tried just about everything to get it to work. I've altered almost every line in the landscape activity_main.xml file, but every time I switch to landscape mode, the corners revert back to 0 radius. This is driving me crazy!

Your help is greatly appreciated.

activity_main.xml landscape:

    <LinearLayout 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:orientation="horizontal"
    tools:context=".MainActivity"
    android:gravity="center"
    android:background="@color/mainBackground">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:orientation="horizontal"
        android:background="@color/background"
        android:layout_gravity="center_vertical"
        android:padding="10dp">

        <LinearLayout
            android:id="@+id/fragmentHolder"
            android:layout_width="300dp"
            android:layout_height="340dp"
            android:orientation="horizontal"
            android:layout_margin="10dp"
            android:layout_gravity="center"
            android:background="@drawable/rounded_corners_audio" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/background"/>

    </LinearLayout>
</LinearLayout>

activity_main.xml portrait

<LinearLayout 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="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:gravity="center"
    android:background="@color/mainBackground">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:orientation="vertical"
        android:background="@color/background"
        android:layout_gravity="center_horizontal"
        android:padding="10dp">

        <LinearLayout

            android:id="@+id/fragmentHolder"
            android:layout_width="340dp"
            android:layout_height="300dp"
            android:orientation="vertical"
            android:layout_margin="10dp"
            android:layout_gravity="center"
            android:background="@drawable/rounded_corners_audio"/>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/background"/>
    </LinearLayout>
</LinearLayout>

rounded_corners_audio.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="10dp"/>
    <solid android:color="@color/colorPrimary"/>
</shape>
1 Answers

Root cause: You got this behavior in the landscape mode because the height of LinearLayout is larger than the screen's height, so you just see a portion of the LinearLayout. You can reduce the height to see the rounded corner, for example.

<!-- Reduce height from 340dp to 200dp -->
<LinearLayout
    android:id="@+id/fragmentHolder"
    android:layout_width="300dp"
    android:layout_height="200dp"
    android:layout_gravity="center"
    android:layout_margin="10dp"
    android:background="@drawable/rounded_corners_audio"
    android:orientation="horizontal" />

Solution: Put your layout inside a ScrollView.

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/mainBackground"
        android:gravity="center"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_margin="10dp"
            android:background="@color/background"
            android:orientation="horizontal"
            android:padding="10dp">

            <LinearLayout
                android:id="@+id/fragmentHolder"
                android:layout_width="300dp"
                android:layout_height="340dp"
                android:layout_gravity="center"
                android:layout_margin="10dp"
                android:background="@drawable/rounded_corners_audio"
                android:orientation="horizontal" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/background" />

        </LinearLayout>
    </LinearLayout>
</ScrollView>
Related