Why can't I use this attribute in my android theme? - android

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

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.

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.

Related

No resource found that matches the given name '#android:style/Theme.MyStyle'

I'm new to android programming (developing on Android Studio 0.8.11) and I'm having issues applying a custom theme to an ActionBar.
My goal is to create a complete custom actionBar with different colours and resources. My min SDK is 16.
Here is the example theme definition in the /res/values/styles.xml:
<style name="Theme.MyStyle" parent="#android:style/Theme.Holo.Light.DarkActionBar" >
<item name="android:actionMenuTextColor">#00FF00</item>
</style>
I'm trying to applying this to all the app with the following line in the manifest xml:
<application> [...] android:theme="#android:style/Theme.MyStyle">
</application>
However when I try to lookout for "Theme.MyStyle" the auto-completion seems not to find it. In fact that part is reded out and the compiler says "No resource found that matches the given name '#android:style/Theme.MyStyle'" .
I'm just trying to figure out how to correct applying a theme, all the guides found even on android developer makes the theming easy but I can't find what's wrong!
Any tips? Thank you very much!
#android:style is for predefined android styles
Change this
android:theme="#android:style/Theme.MyStyle"
into
android:theme="#style/Theme.MyStyle"

Android windowEnterAnimation attribute does not exist

I've been trying to follow this answer for adding an animation to a dialog box. However I'm getting an error on the first style addition.
I've added the following to res/values/styles.xml (not completely sure if this is correct)
<style name="DialogAnimation">
<item name="android:windowEnterAnimation">#anim/slide_down_dialog.xml</item>
<item name="android:windowExitAnimation">#anim/slide_out_up</item>
</style>
The first item, android:windowEnterAnimation, does not seem to exist when I tab complete android:. This also applies to android:windowExitAnimation. The error I receive in the XML file is:
error: Error: No resource found that matches the given name (at 'android:windowEnterAnimation' with value '#anim/slide_down_dialog.xml').
All the questions I've found regarding animation like this uses windowEnter/ExitAnimation. I looked at the android docs and it supposedly has this attribute, but I cannot for the life of me get it to appear.
Thanks in advance for any help/advice. If this is a duplicate please point me in the right direction, I couldn't find any related questions.
you sholud put # letter before android and remove the .xml like:
<item name="#android:windowEnterAnimation">#anim/slide_down_dialog</item>
I think it is:
remove (.xml) to be like (#anim/slide_down_dialog)

?android:attr/selectableItemBackground

when writing my android app i used
android:background="?android:attr/selectableItemBackground"
I tried looking for attr.xml file that would be containing the source but i could not find it. Any ideas please on where i can find it. I found one attr.xml in
C:\Program Files (x86)\Android\android-sdk\platforms\android-13\data\res\values
but it did not have the attribute mentioned above. Can anyone lead me where I can find the xml resource with the attribute above?
I'm not that experienced with Android, but I think you can find what you're looking for in "data\res\values\themes.xml".
If you search in that file for "selectableItemBackground", you should find the following:
<style name="Theme">
...
<item name="selectableItemBackground">#android:drawable/item_background</item>
...
</style>
You'll also find respective entries for Theme.Holo.Light and Theme.Holo. The *"item_background"*, *"item_background_holo_light"*, and *"item_background_holo_dark"* drawables would be located in the "data\res\drawable" folder.
Hope that helps!

Using resources referenced in the current theme

I find myself again trying to use resources set in the current theme inside my Android application and running into difficulties.
Following the rough guidelines found here for theme attribute value reuse
I was originally trying to create a selector like so
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="?android:attr/selectableItemBackground" />
<item android:drawable="#color/transparent" />
</selector>
where the pressed state drawable is declared like so
<item name="selectableItemBackground">#android:drawable/item_background</item>
in the current ICS themes xml doc themes xml doc . This compiles fine in eclispse (whereas if i spell the attr wrong it will give an error or reffng #android:drawable/item_background directly will inform me the drawable itself is private).
When i run the app i get a
FATAL EXCEPTION: main
E / AndroidRuntime (18815): android.view.InflateException: Binary XML file line #14: Error inflating class <unknown>
without any other useful information apart from the xml file that is using this selector for an ImageViews android:background attribute value. I know that there is a bug where colorStateLists cant be used as backgrounds as mentioned here and maybe this also applys to drawableState lists referencing drawables from the theme. I know however it does work as the post mentioned describes.
Am i missing something here?? I always run into something i dont understand when trying to do stuff like this so would be glad of any pointers.
Thanks for any help
EDIT
as another experiement I tried setting an alias to the referenced drawable in the theme like
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="?android:attr/selectableItemBackground" ></bitmap>
and using this as the background directly but this does not work either. Looking at aliases they are onlyment to ref actual images so this makes sense
EDIT 2
another test an I have found using it directly
android:background="?android:attr/selectableItemBackground"
does actually work. So it seems that using it inside a alias or selector is where it gets unhappy. Strange as i would assume that both background and the same arg
EDIT 3
It seems the theme attr i was pointing at was actually a selector instelf, which im sure didnt help the issue! Thought i wouldve guessed looking at the name of it
I guess i answered the question myself in my edits - main issue i imagine was that i was trying to ref a selector inside a selector.

Style / themes not working

whenever I try to apply a VERY simple style in eclipse I get errors:
the style is as below (filename styles.xml in res/layout)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="commonStyle">
<item name="android:background">#EEEEEE</item>
</style>
</resources>
Regardless of whether I try to apply this to a LinearLayout (using style="#style/commonStyle") or to an activity in my manifest file (using android:theme="#style/commonStyle") I ALWAYS get the error "No resource found that matches the given name...".
If I try to apply an Android theme to an activity (say Theme.Black) it works just fine.
If I remove any usage of my style, my R.java file is generated as normal with the following contents:
public static final int styles=0x7f030002; (in the generated layout class)
I have NO idea what is going on. It seems that NOONE is having the same troubles as me (Ive spent about 4 hours searching using google, and came up with not even a remotely close answer).
Ive tried restarting eclipse, clean building etc. etc. and nothing works what so ever...
So what am I missing?
Try moving your styles.xml file to res/values.
http://developer.android.com/guide/topics/resources/style-resource.html
Styles need to be stored in the res/values folder :)
http://developer.android.com/guide/topics/ui/themes.html (See Defining Styles)
The name of the XML file is arbitrary,
but it must use the .xml extension and
be saved in the res/values/ folder.
I haven't used styles in my code yet, however generally you need to use
android:
Does it work when you use this?:
<style android:name="commonStyle">
Please verify the you call the style in the correct manner :
for example this might compile but will not work :
style="mainButtonText"
this is the correct way to call style:
style="#style/mainButtonText"

Categories

Resources