2

To start with some context, I'm trying to style the background color of a SearchView widget. A really insightful so answer to this problem has already been posted, and I learned immensely from it.

There is one gap in my understanding though, and I'm hoping someone can explain it to me. When I create a theme, such as the following:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="MyCustomTheme" parent="android:Theme">
        <item name="android:searchViewTextField">@android:color/white</item>
    </style>
</resources>

Eclipse compiles with an error saying it doesn't know about the attribute:

error: Error: No resource found that matches the given name: attr 'android:searchViewTextField'.

However, if I re-declare the attribute:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <declare-styleable name="CustomSearchView">
        <attr name="android:searchViewTextField" format="reference" />
    </declare-styleable>
</resources>

Eclipse responds with an error saying:

error: Attribute "android:searchViewTextField" has already been defined

Eclipse seems to be aware of the attribute, but conveniently forgets about that attribute when I want to use it. (I wonder if there is some context switching going on in the background)

At any rate, if I delete the problem code then I can see my custom theme inheriting searchViewTextField from its parent. I just don't understand why I can't supplant it with my own.

(The other answer mentions android.R.stylable, but that file is obsolete in api 16)

Thanks in advance.

Relevant Android sources:

themes.xml, attrs.xml, and search_view.xml (sorry, two link limitation).

Community
  • 1
  • 1
Nathan
  • 191
  • 3
  • 17

2 Answers2

0

The answer in the SO answer you linked explicitly says that you cannot specify searchViewTextField in your own theme because it is not a stylable resource. You need to modify it's value in code as per the other answer.

PacificSky
  • 3,422
  • 2
  • 25
  • 24
  • In the attrs.xml file, the searchViewTextField attribute is declared styleable within . Maybe this is leading to what I don't understand. – Nathan Nov 09 '12 at 05:30
  • Try using just "searchViewTextField", without the "android:" prefix. You should be able to override it in your custom theme which inherits from android:Theme. – PacificSky Nov 09 '12 at 06:00
  • I gave that a try, but Eclipse reported "Error: No resource found that matches the given name: attr 'searchViewTextField'." Sorry, but I just don't understand the mechanism that makes them usable, beyond . Perhaps the attribute was deleted in the Eclipse build. – Nathan Nov 12 '12 at 00:26
0

The question was, why can't I use the theme attribute "android:searchViewTextField"?

Out of suspicion that the compiled Android.jar file was not in tune with the published Android source, I ripped open the jar and started decompiling (a first for me). After poking around, it does appear that someone went into R$style.class with a machete and hacked out a massive chunk of the resources. As far as I can ascertain, it must have been done deliberately and perhaps even manually.

This kind of compiled source modification is not without precedent in other similar frameworks, but it sure as hell is confusing to anyone trying to debug the framework.

Ironically, auto-generated R files all have a header that says,

/* AUTO-GENERATED FILE.  DO NOT MODIFY.

Kinda pointless when you make it routine procedure to modify the files.

Nathan
  • 191
  • 3
  • 17