1

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://schemas.android.com/apk/res/android" 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.

sep87x
  • 119
  • 9
  • Log says 01-11 16:48:25.511: E/AndroidRuntime(2037): android.view.InflateException: Binary XML file line #9: Error inflating class Something wrong at the 9th line of your XML – Oğuzhan Döngül Jan 11 '15 at 16:13
  • @oguzhand Well, the ninth line in my view resource is the first line in the view layout I included in my question. So basically #9 would refer to " – sep87x Jan 11 '15 at 16:18

3 Answers3

3

Actually that is Android issue and it got fixed in Lollipop version, Please refer Google issue tracker link, https://code.google.com/p/android/issues/detail?id=26251

So you should create separate drawable for each theme.

Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41
  • Unfortunately, I didn't see your reply before I finished my own response to my own question. So as far as I understood, this should be working in Android 5.0+ right? I run my applications on my tablet (using 4.4.2), so that's probably the reason why it didn't work. Thanks for your research nonetheless. Will check your answer as the correct one. – sep87x Jan 11 '15 at 17:25
2

So I kind of solved my problem using a reply on a earlier question. Apparently, Android cannot handle attribute references in drawables (which is heavily inconvenient). Instead, one should create a reference to the background drawable, create a drawable for every theme and reference each one to the corresponding. Basically, I ended up with the following setup:

Background resources

Backgroudn resource for every possible theme

Create new reference

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

<!-- reference which points to the corresponding background resource -->
<attr name="circleBackground" format="reference" />

Add the resource reference to the custom themes

<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>

    <item name="circleBackground">@drawable/background_circle_turquoise</item>
</style>

<style name="Theme.ColorScheme.Blue" parent="AppTheme">
    <item name="designBase400">@color/design_base_400_blue</item>
    <item name="designBase500">@color/design_base_500_blue</item>
    <item name="designBase700">@color/design_base_700_blue</item>

    <item name="circleBackground">@drawable/background_circle_blue</item>
</style> <!-- and so on ... -->

I'm not very satisfied with this solution since logically my first attempt should have been perfectly fine. I cannot say why Android doesn't accept attributes in drawables and I probably won't understand it either. Until someone is willing to enlighten me and provide a better solution I guess I have to stick to this.

Community
  • 1
  • 1
sep87x
  • 119
  • 9
0

Change format="reference" to format="color" .

natario
  • 24,954
  • 17
  • 88
  • 158
  • Sadly doesn't work either. Same error shows up, but [I found a link which says that it's not possible to reference attrs from a drawable resource.](http://stackoverflow.com/questions/8041537/how-to-reference-style-attributes-from-a-drawable) It also suggests to create one resource for every theme. That can't be true, right? – sep87x Jan 11 '15 at 16:50