Prodigga
Prodigga

Reputation: 1477

Using "?android:colorBackground" as a color crashes my app on Galaxy Note 3 (4.4.2) but not Nexus

Is there a minimum android version requirement to access this color or something? I need to set the background color of an object to the 'default' background color and '?android:colorBackground' works well on my Nexus 5 (Android 5.1.1) but it is crashing my Note.

I have a drawable called editor_border.xml as follows:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
            <!-- content background -->
            <item>
                <shape>
                    <solid android:color="?android:colorBackground" />
                    <corners android:radius="2dp" />
                </shape>
            </item>  
    </layer-list>

I use this drawable as the background for a layout. When inflating the layout, my app crashes with the folowing error"

10-07 22:23:52.634: E/AndroidRuntime(6512): android.view.InflateException: Binary XML file line #11: Error inflating class <unknown>

And further down

10-07 22:23:52.634: E/AndroidRuntime(6512): Caused by: android.content.res.Resources$NotFoundException: File res/drawable-v4/editor_border.xml from drawable resource ID #0x7f020005

10-07 22:23:52.634: E/AndroidRuntime(6512): Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x2

Removing the color or changing it to any other color works fine.

Upvotes: 2

Views: 1368

Answers (1)

hidro
hidro

Reputation: 12541

Custom attributes in drawable resource seem to only work from API 21. I can't find the source to back this up but a commit from Google I/O Schedule app demonstrates this.

A workaround would be to have 2 different drawable resources, one for API 21+ and another one pre-21, in which you should not use custom attributes.

drawable/background.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- content background -->
    <item>
        <shape>
            <solid android:color="@color/your_color" />
            <corners android:radius="2dp" />
        </shape>
    </item>  
</layer-list>

drawable-v21/background.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- content background -->
    <item>
        <shape>
            <solid android:color="?android:colorBackground" />
            <corners android:radius="2dp" />
        </shape>
    </item>  
</layer-list>

Upvotes: 2

Related Questions