Android - Style not being applied to ListView items

    I came across this interesting problem where in the style specified by an attribute wasn't actually applied to the list view item. The items were inflated by an adapter and it used a simple layout with style specifications.

    style="?attr/listViewItemStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true" >

    listViewItemStyle is a reference attribute pointing to a style in a theme. In this application, the attribute was set fine in the theme and the theme was explicitly applied to the activity hosting the list view and yet the style wasn't applied to the list view items.

    Upon closer look, it turned out to be a context issue. The adapter which inflated the list view items used the application context instead of an activity context and hence was using the application's theme to apply styles to inflated views. In this case, the application was using a theme which didn't have a style assigned to listViewItemStyle.

No comments: