23

I try to use different list item layout depending on the OS version.

So, I created different layouts associated with conditions. One of them is (in layout/search_result_list_item.xml)

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

it includes the standard "simple_list_item_1".

In my Java code, the layout is associated to the list like this :

    adapter = new SimpleCursorAdapter(getActivity(),
                                      R.layout.search_results_list_item,
                                      null,
                                      from,
                                      to,
                                      0);

When a list item is displayed, the following exception is thrown :

android.view.InflateException: Binary XML file line #2: Error inflating class include
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
   at android.support.v4.widget.ResourceCursorAdapter.newView(ResourceCursorAdapt

What is wrong ? can't be used as a root item ? despite ADT allows it.

DenisGL
  • 1,190
  • 1
  • 16
  • 31
  • I'm not sure about this, but did you try using the root as a linear layout with fill_parent for both height and width, and having the include inside that. – blindstuff Jan 11 '12 at 21:54
  • Yes, maybe it could work, but I would not like to overload my list view useless intermediate views (one for each row). – DenisGL Jan 12 '12 at 20:32
  • Then dont define your own xml, just pass simple_list_item_1 to the adapter instead of yours – blindstuff Jan 12 '12 at 21:48
  • The purpose is to use different layout according to the OS version – DenisGL Jan 13 '12 at 21:02
  • Have you ever found a solution to this? I'm having the same problem. Thanks in advance. :-) – Mira Weller Jun 23 '12 at 23:26

2 Answers2

33

If anyone else is wondering this is the answer:

<?xml version="1.0" encoding="utf-8"?>
<merge>
    <include xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        layout="@android:layout/simple_list_item_1">
    </include>
</merge>
Takhion
  • 2,706
  • 24
  • 30
  • Yep, having a `` tag as the root element lets you inflate the layout without problems. Thanks! – jenzz Sep 23 '13 at 09:50
  • 6
    I get ` can be used only with a valid ViewGroup root and attachToRoot=true` – Mickey Tin Jul 06 '15 at 14:16
  • 1
    @MickeyTin you are probably either trying to use the inflate method with the 3rd parameter (attachToRoot) set to `false`, or using the `` tag as a root as in [this answer](http://stackoverflow.com/questions/14039162/android-xml-merge-layout-error-on-inflate) – Takhion Jul 07 '15 at 12:53
  • Doesn't work with custom attributes - [Include layout with custom attributes](https://stackoverflow.com/questions/11265258/include-layout-with-custom-attributes) **|** [Data binding does not support include as a direct child of a merge element.](https://developer.android.com/topic/libraries/data-binding/index.html) – samus Mar 05 '18 at 18:05
0

You can provide Android OS verion specific resources using qualifiers on the resource folders.

See: http://developer.android.com/guide/topics/resources/providing-resources.html

The last item in the list of possible qualifiers is version (API level).

ChrisJD
  • 3,644
  • 1
  • 21
  • 20