Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a View background based on theme attribute crash the app

I'm trying to set a ListView background color based on the current theme attribute, but it crash every time the ListView is shown.It seems I'm doing something wrong but I can't see what...

Here's what I'm doing:

First, create the background color:

<resources>
    <color name="userlist_background_light">#fff0f0f0</color>
    <color name="userlist_background_dark">#ff040404</color>
</resources>

Second, create attributes for my custom themes:

<resources>
    <attr name="userlist_background" format="reference|color" />
</resources>

Third, setting this attribute in my themes:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="Light" parent="Theme.Sherlock.Light">
        <item name="userlist_background">@color/userlist_background_light</item>
    </style>

    <style name="Dark" parent="Theme.Sherlock">
        <item name="userlist_background">@color/userlist_background_dark</item>
    </style>
</resources>

And finally, using this attribute in the ListView xml:

<ListView
        android:id="@+id/user_bar"
        android:layout_width="0dip"
        android:layout_height="0dip"
        android:background="?attr/userlist_background"
        android:cacheColorHint="?userlist_background"
        android:visibility="gone" />

Even the Eclipse layout view crash. Of course, it works fine if I use a "@color/" directly in the background attribute. It even work if I use say, "?android:attr/colorBackground".

The message error is:

android.view.InflateException: Binary XML file line #8: Error inflating class android.view.ListView Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f010068 a=-1}

I'm pretty sure I'm doing something wrong, as it works with android attributes, but I haven't be able to find what during my Google searches.

I hope you'll be able to help me!

Many thanks,

Sébastien.

like image 802
Sébastien Avatar asked Aug 11 '12 12:08

Sébastien


People also ask

How to set theme android?

Settings. Under Display Options, tap Theme. Select the theme for this device: Light—White background with dark text.

Which xml is used to define custom themes and looks for the ui of application?

Styles and themes are declared in a style resource file in res/values/ , usually named styles. xml .

How to use theme in android studio?

To change default themes go to File and click on Settings. A new Settings dialog will appear, like this. Under the Appearance & Behaviour -> Appearance, you will find Theme. Choose the right theme from the drop-down and click on Apply and then Ok.

What is the use of styles and themes in android explain it?

A theme is nothing but an Android style applied to an entire Activity or application, rather than an individual View. Thus, when a style is applied as a theme, every View in the Activity or application will apply each style property that it supports.


1 Answers

Ok, I fixed it, and it was due to a mistake!

I have two themes.xml files, one for Honeycomb+, and one for Gingerbread-. I've only added the new attributes to the themes.xml targeting Gingerbread-, and was testing on ICS.

Maybe it'll help others who'll make the same mistake!

like image 103
Sébastien Avatar answered Sep 28 '22 10:09

Sébastien