Sunday, 11 January 2015

Custom view background using attrs causes crash



I've been experimenting with styles in Android recently (and blatantly failed) trying to use attrs in custom view background resources. So basically, I want to make the user set a custom theme if he or she wants to. I have a main fragment which hosts a (support) ViewPager. Each page in the ViewPager gets inflated with a custom view using a custom background. The view's background points to a style reference which is basically a color justified by the selected theme.


Attributes



<attr name="designBase400" format="reference" />
<attr name="designBase500" format="reference" />
<attr name="designBase700" format="reference" />


One of the few custom styles



<style name="Theme.ColorScheme.Turquoise" parent="AppTheme">
<item name="designBase400">@color/design_base_400_turquoise</item>
<item name="designBase500">@color/design_base_500_turquoise</item>
<item name="designBase700">@color/design_base_700_turquoise</item>
</style>

<style name="AppTheme" parent="AppBaseTheme">
<item name="windowActionBar">false</item>
<item name="android:spinnerItemStyle">@style/SpinnerItem</item>
<item name="android:spinnerDropDownItemStyle">@style/SpinnerItem.DropDownItem</item>
</style>


Corresponding colors



<color name="design_base_500_turquoise">#009688</color>
<color name="design_base_700_turquoise">#00796B</color>
<color name="design_base_400_turquoise">#26A69A</color>


Background resource



<shape xmlns:android="http://ift.tt/nIICcg" android:shape="oval">
<solid android:color="?attr/designBase400" />
</shape>


The view using the background resource (only the important part)



<RelativeLayout
android:id="@+id/view_entry_hour_container"
android:layout_width="@dimen/entry_view_circle_dimen"
android:layout_height="@dimen/entry_view_circle_dimen"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="false"
android:background="@drawable/background_circle" >

<TextView
android:id="@+id/view_entry_hour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white"
android:textSize="@dimen/entry_view_hour_size"
android:textStyle="bold" />

</RelativeLayout>


Now I have a sample theme set in the ApplicationManifest.xml (say @style/Theme.ColorScheme.Turquoise) and the application crashes immediately, raising an InflationException. The stack trace can be found here. I don't have the foggiest idea on what could have went wrong.


No comments:

Post a Comment