How to show phone's portrait layout in tablet irrespective of it's orientation WITH ViewBinding WITHOUT locking orientation?

64 Views Asked by At

I have a fragment called ErrorFragment.

I have different layout files to be shown on phone for portrait (error_fragment_portrait_content.xml) and landscape(error_fragment_portrait_content.xml) mode.

But I want to use the same layout (phone's portrait layout) in tablet mode irrespective of tablet's orientation.

Please note that I don't want to lock the orientation. I want orientation to be switched, but layout to be shown should remain the same.

And I want to use the ViewBinding for my fragment.

I know a straightforward solution is to just copy the layout file error_fragment.xml from res/layout/ folder to res/layout-sw600dp. But this is adding up to the apk size. I want to avoid this.

I tried another approach as well. This is the code snippet of my error_fragment.xml:-

<?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">

    <include
        android:id="@+id/error_fragment_content"
        layout="@layout/error_fragment_portrait_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

In res/layout/ folder, I am including layout="@layout/error_fragment_portrait_content"

In res/layout-land folder, I am including layout="@layout/error_fragment_landscape_content"

In res/layout-sw600dp folder, I am including layout="@layout/error_fragment_portrait_content"

But the binding file generated for ErrorFragment (ErrorFragmentBinding) gets confused whether to create and instance of ErrorFragmentPortraitContentBinding or ErrorFragmentLandscapeContentBinding? It's generating an instance of ViewDataBinding. So getting a build error with this approach.

So, I want to know how can I achieve my desired results?

0

There are 0 best solutions below