4

I have an attrs.xml file as follows:

<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="butBackColor" format="reference|color" /> </resources> 

Styles.xml contains:

<style name="GreyButtonTheme" parent="android:style/Widget.Button"> <item name="butBackColor">#3D3D3D</item> </style> 

I have custom_button.xml which contains a drawable selector:

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> ... <item android:left="5dp" android:right="5dp" android:top="5dp" android:bottom="5dp"> <shape> <solid android:color="@color/butGreyBack" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> 

Finally, I have a layout which contains a Button which I want to use my style with:

The code snippets above work fine for me - but when I change:

<solid android:color="@color/butGreyBack" /> 

to:

<solid android:color="?butBackColor" /> 

..then I get an "Error inflating class " exception. Why is the custom_button.xml selector failing? What do I need to do to get the ?butBackColor attribute to work?

The reason I have implemented it this way is that I would like to use different styles (colours) for different sets of buttons.

Here is the project which may be of use to somebody who wants a file browser dialog box:

FileBrowserTestDlg

1 Answer 1

2

The format for referencing the attribute is

?[<package_name>:][<resource_type>/]<resource_name> 

change this:

<solid android:color="?butBackColor" /> 

to:

<solid android:color="?attr/butBackColor" /> 

SparkyNZ wrote: The answer to this question is at the very end of the comments. The above lines didn't actually solve the problem. What I did learn is that it is not possible with the current Android SDK to use custom attributes within shape/drawable XML files. They work fine in View definitions, but not shape/drawables.

Sign up to request clarification or add additional context in comments.

16 Comments

Thanks. I changed it to <solid android:color="?attr/butBackColor" /> but I still have the same problem. If I change it back to <solid android:color="@color/butGreyBack" /> then it works - just tried that in case there were other offending lines.
Also tried full package name <solid android:color="?com.example.filebrowsertest:attr/butBackColor" /> and didn't like that either.
@SparkyNZ try to change attrs.xml to attr.xml
@ Rod_Algonquin: No luck. I renamed it from attr.xml to attrs.xml when following a tutorial earlier. Renamed it back and still ain't working. Do I need a namespace line or something? Shouldn't if the entire package is specified though. attr.xml is in my 'values' folder by the way.
I renamed butBackColor to but_back_color and that didn't work either (had problems with uppercase in the past with drawables).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.