Referencing attributes of theme from within <include tag

Viewed 79

I ran into a problem after I updated the targetSdkVersion and support libs versions of my apps to Oreo (27). The problem is that when I run my application on Android 4, API 19, I get runtime error. The problem is that it cannot find the referenced attribute's value. Here's the code:

styles.xml:

<style name="CustomStyle" parent="OtherCustomStyle">
    <item name="screenPrimaryColor">@color/primary</item>
    <item name="screenPrimaryColorDark">@color/primaryDark</item>
</style>

<attr name="screenPrimaryColor" format="reference" />
<attr name="screenPrimaryColorDark" format="reference" />

activity_test.xml:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:theme="@style/CustomStyle">

    <include layout="@layout/inner_test"/>
</FrameLayout>

inner_test.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/instant_sale_bottom_margin">

    <!-- HEADER -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="?attr/screenPrimaryColor"
        android:baselineAligned="false">

        <!-- REST OF MY LAYOUT -->

    </LinearLayout>
</LinearLayout>

As you can see the style is applied as a theme in the activity_test.xml. However, with this combination (support lib 27 and Android 4.4) it doesn't work and the app crashes when inflating the layout. It crashes because of android:background="?attr/screenPrimaryColor" in the included view. This was working fine before I updated the support libraries. Also this works well with the new support libs on devices running Android newer that 4.

I think that this may be a bug in the support lib. My question is whether any of you guys encountered this issue as well.

(BTW, I found the "solution", which is to set the same theme within the included view (inner_test.xml in my case). However, this is a dirty solution as inner_test.xml can be reused in different parts of the application.)

0 Answers
Related