Style / themes not working - android

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"

Related

Alias for mipmap launcher icon

Is it possible to somehow make alias for mipmap icon? For example we have icon named icon_app but in our lib we use ic_launcher (as it should be). So for drawables you are able to create reference with values/drawables.xml
I tried with following approach:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<item name="ic_launcher" type="mipmap">#mipmap/icon_app</item>
</resources>
but it seems its not working i tried naming it both drawables.xml and mipmaps.xml, it seems it always tries to reference it from drawable instead of mipmap folder?
Further attempts show that the code originally posted works fine when compiled onto device, but is just not recognised by Android Studio.
I was hoping to alias the system default launcher icon, and this seems to work:
<resources>
<item name="test_alias" type="mipmap">#android:mipmap/sym_def_app_icon</item>
</resources>
...as do these:
<resources>
<mipmap name="test_alias">#android:mipmap/sym_def_app_icon</mipmap>
<mipmap name="test_alias2">#mipmap/ic_test</mipmap>
</resources>
...where ic_test is a "normal" mipmap in my res/mipmap resources folder.
To explain the use case in my situation, this above code is in a library project. The test_alias mipmap is declared in the library so that I can use it to put up a Notification.
But the intention is that the developer using the library would be able to override this icon with their own launcher, or other icon of their choosing by adding something like this into their own code, overriding the library definition:
<resources>
<mipmap name="test_alias">#mipmap/ic_launcher</mipmap>
</resources>

Create a different directory other than res/values/styles.xml to define styles

As I asked in the title, is there any way to do so?
Now, when I put all styles into one file it looks a little crowded, I would like to separate styles.
For example:
res/values/styles_for_main_screen
res/values/styles_for_set_screen
And then in main_screen layout
<TextView
style="#styles_for_main_screen/text_view_custom_style">
</TextView>
This example obviously doesn't work, but it shows what I'd like to achieve.
I read in every tutorial that we need to put our custom styles into styles.xml file, but I wonder if there is a possibility to diversify styles in few xml files?
Every question I read was something like "how to do .... in styles.xml".
I can't find question similar to mine.
Example how it should be done, thanks #Frank N. Stein for the answer:
This how looks my custom xml res/values/styles_for_main_screen
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="custom_back">
<item name="android:background">#E81C1C</item>
<item name="android:text">whatr</item>
</style>
</resources>
and then to retrieve this style I just write:
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
style="#style/custom_back"
/>
so the convention looks like:
style="#(what I want to retrieve)/(name of style)"
from the android developers site
In XML: #[package:]style/style_name
You can call your style files whatever you want (if you respect the naming conventions and you put them all in the values folder/s), as you do with strings and colors.
Therefore, YES! You can have multiple ones, if so you desire.
Obviously, you will NOT specify the path to each file.
Referencing the style/s by using R.style.your_new_style is enough.
Remember that, android scans the files found int the /values directory by reading their content. For styles, every <style name="styleName" > ... </style> will be parser and a style object reference will be created.
Then, as Frank said, Yes. You can use whatever file name to write your custom styles.

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 get an other style after adding value's folders

I have an application, that need to run in different size devices
For this, I added the next folders: res/values-normal and res/values-large.
Everything works fine except the application style was modified.
For example the Spinner was like the Model1 and after adding the folders it became like the Model2
http://postimg.org/image/cf3qa2oqd/
If I remove these folders, all returns as before (Model1).
Thanks,
NB: I was not able to add the image here.
the solution is:
change the Application Theme in the Style file
<style name="AppBaseTheme" parent="android:Theme.Holo">
it works fine.

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

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.

Categories

Resources