1

Any issue I've found related to this has always been resolved by correcting the layout files paths, and I'm pretty sure mine are correct.

I have a very simple layout made that is supposed to have a split view for large landscape devices. So I have:

  • layout/main.xml
  • layout/main_twopane.xml
  • values/main.xml
  • values-large-land/main.xml

And I am loading a Nexus 7 Tablet in landscape (which is classified as a large device). The problem is it is always loading main.xml when according to the Android documentation what I have should load the two pane layout for large devices on landscape.

Here is the code:

layout/main.xml

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

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_catches"
    android:name="com.cohenadair.anglerslog.fragments.CatchesFragment"
    tools:layout="@layout/fragment_catches"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

layout/main_twopane.xml

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

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/fragment_catches"
        android:name="com.cohenadair.anglerslog.fragments.CatchesFragment"
        tools:layout="@layout/fragment_catches"
        android:layout_width="200dp"
        android:layout_height="match_parent" />

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/fragment_catch"
        android:name="com.cohenadair.anglerslog.fragments.CatchFragment"
        tools:layout="@layout/fragment_catch"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" />

</LinearLayout>

values/main.xml

<resources>
    <item name="main_layout" type="layout">@layout/main</item>
    <bool name="has_two_panes">false</bool>
</resources>

values-large-land/main.xml

<resources>
    <item name="main_layout" type="layout">@layout/main_twopane</item>
    <bool name="has_two_panes">true</bool>
</resources>

The code that loads the layout, in MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

And in each individual fragment:

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View aView = super.onCreateView(inflater, container, savedInstanceState);

    // set the list's adapter
    ArrayAdapter adapter = new ArrayAdapter<Catch>(this.getActivity(), android.R.layout.simple_list_item_1, Logbook.getSharedLogbook().getCatches());
    setListAdapter(adapter);

    return aView;
}

And

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_catch, container, false);
}
cohenadair
  • 2,072
  • 1
  • 22
  • 38
  • Please post your Java code where you are loading this layout. – CommonsWare Aug 17 '15 at 18:42
  • Hmm? I was under the impression the system automatically loaded the layout depending on the device size/orientation. The documentation link in the question doesn't mention anything about loading the layout (if it does, I missed it). – cohenadair Aug 17 '15 at 18:46
  • "I was under the impression the system automatically loaded the layout depending on the device size/orientation" -- only in response to some Java code somewhere that is trying to load the layout, such as `setContentView()` on an `Activity`, or `inflate()` on a `LayoutInflater`. – CommonsWare Aug 17 '15 at 18:47
  • 1
    Nexus 7 Tablet - I believe this has issues where it thinks it's a phone (size, telephony, the works) with certain update version. Please verify with this code: http://stackoverflow.com/a/5016350/360211 Or an app such as [this](https://play.google.com/store/apps/details?id=com.jotabout.screeninfo) – weston Aug 17 '15 at 18:49
  • @CommonsWare I've added the code that loads each layout. I think it's the right code. – cohenadair Aug 17 '15 at 18:52
  • Well the Nexus 9" worked, and the 7" said it was a phone, so you must be correct. – cohenadair Aug 17 '15 at 19:33

1 Answers1

2

The problem is it is always loading main.xml

That is because you are telling Android to load main:

setContentView(R.layout.main);

If you want it to load main_layout, as your values/... stuff suggests, then change that to:

setContentView(R.layout.main_layout);

I am not quite certain why you are going the layout resource alias approach here. It would be simpler, IMHO, for you to have res/layout/main.xml and res/layout-large-land/main.xml, then just request R.layout.main in your Java code, and dump the aliases. But, your aliases should work, but if and only if you request the alias.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I'm using aliases because eventually I'm going to be reusing fragments several times, and I don't want duplicate layout.xml files. Switching to `R.layout.main_layout` didn't seem to work. I'm going to change devices as a comment above mentioned a possible bug in the Nexus 7 Tablet. – cohenadair Aug 17 '15 at 18:59
  • 1
    This is your problem I think, either way, no need to switch devices, just verify it actually thinks it is large. – weston Aug 17 '15 at 19:26
  • This is the solution. Thanks, CommonsWare. And Weston, you're right, Android does think the Nexus 7 simulator is a phone. Thank you for that. – cohenadair Aug 17 '15 at 19:32