I still not understanding the parent role in the android styles I have put my XML code into the style XML which works well. I have been trying to get my head round android styles and I just not getting it. Any help would be great.
ps. I have been reading all about styles on the net. But the information is good but could be a lot better
http://imageshack.us/a/img843/3083/holo.png
<style name="LLayout1">
In your style.xml just replace the above line with below line
<style name="LLayout1" parent="android:Theme">
This is because every style in XML must have a parent type.
Related
I’m new to android and are looking at tutorials to learn android development. I'm trying to figure out how all the xml files and tags are fit together.
In a google tutorial for actionbars one custom style, in themes.xml, look like this:
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#android:style/Widget.Holo.ActionBar">
<item name="android:titleTextStyle">#style/MyActionBarTitleText</item>
</style>
I don't find or understand where "#style/MyActionBarTitleText" is defined and i don't get any compiling error in Eclipse. When a tag is referenced like this i thought it has to be defined in a xml file under my project somewhere but i cant find it?
Style XMLs are defined in res/drawable[-mdpi/ldpi/xhdpi]
Style defines look of your Activity. You can define color, themes, shapes in Styles. Read more about styles and other resources here
http://developer.android.com/guide/topics/resources/style-resource.html
http://developer.android.com/guide/topics/resources/available-resources.html
Yes you can find it in: res->values->styles.xml
and also you can change your application theme/style in styles.xml file
I am in bit confusion to adding parent style to new style. But both are working. But i want to know which is correct way first one or second one? or is there any other way ?
<style name="customView" parent="viewline">
<item>....
and
<style name="customView" parent="#style/viewline">
<item>....
Thanks
Both are correct as you said. At here there is no any drawback to use any of them.
<style name="customView" parent="viewline">
denotes that the parent style is in same file. And
<style name="customView" parent="#style/viewline">
denotes that parent style is somewhere (may be i another file) in style directory. Thats it.
I created an activity with a custom titlebar and a button in it, which is extended by all other activites. It works well so far, but when I use an activity whith theme.dialog the titlebar is not shown correct. There is a padding to the left and right of the title and height is limited to 25, I guess.
So I tried the same to the theme.dialog windowTitleSize as I did to the theme. But it showed the same result as if I just change only the title of the normal theme - a padding to the left and right and the wrong height.
After a lot of googling I found at least one solution that works here on stackoverflow. Unfortunately it fixes the padding for the titlebar at runtime with Class.forName() and getField(). I could change the height of the titlebar with layoutParams the same way, but would like to set it all up in xml.
I browsed themes.xml in the ..data/res/values but could not find where the limitation of the layout is set. Finally I tried to extend the normal theme and create a dialog myself. That brought me to Widget.PreferenceFrameLayout and the following errors in my xmls:
In styles.xml "no resource found..." on all "...android:border..."
<style name="MyPreference">
<item name="android:borderTop">20dp</item>
...
</style>
And in themes.xml "no resource found...android:preferenceFrameLayoutStyle"
<style name="MyThemeDialog" parent="android:Theme">
...
<item name="android:preferenceFrameLayoutStyle">#style/MyPreference</item>
...
</style>
How am I supposed to use the given themes and styles the right way to set up the titlebar in theme.dialog?
-----edit-----
I recognized, that the mentioned attributs are not available in the attrs.xml of api8, so sorry for the hasted asked question. Sometimes I just can't see the forest because of all the trees.
Thanks in advance for your help,
Christel
I tried changing the appearance of a spinner and I partly succeeded. I'm doing this via overriding parts of the theme. I managed to change the text size of the spinner item (i.e. the text size in the drop down button) with my themes.xml and styles.xml:
My themes.xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomTheme" parent="#android:Theme.Holo.Light">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:spinnerItemStyle">#style/CustomSpinnerItem</item>
</style>
</resources>
My styles.xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomSpinnerItem" parent="#android:Widget.TextView.SpinnerItem">
<item name="android:textAppearance">#style/CustomTextAppearance</item>
</style>
<style name="CustomTextAppearance">
<item name="android:textSize">30dp</item>
</style>
</resources>
However I cannot find the attributes that are responsible for the text appearance of the items in the dropdown list of the spinner. I tried dropDownItemStyle amongst other things. In my opinion the attribute names are not self-explanatory, so I wanted to know whether there is a documentation of what attribute does what in a style to find out which attributes to override. I found it very cumbersome to trace back all the styles used in a theme via the themes.xml and styles.xml of the platfrom and then try to find the right attributes via trial and error.
I know that one can change the appearance by passing layouts to the adapter, however, this is not really what I was looking for, since (as far as I know), you can only use inheritance in styles and not in layout xml files. If I created a custom layout for the adapter I'd have to create 9-patch images etc., which I think is a bit too time consuming in case I only want to change the text size.
Of course it's possible that I misunderstood the whole concept, since I'm new to Android ;)
You probably have found out the answer since you asked but for others looking at similar questions:
I do not know of a list of attribute names with good explanation of what they do (R.attr's page mostly gives information that is already in the name) but the way I do it is:
Start from the element I give to setDropDownViewResource(), in my case: android.R.layout.simple_spinner_dropdown_item and find.
Find its layout definition in \sdk\platforms\android-17 (specific platform version to avoid redundant results).
Get its style from the layout file. In this case: ?android:attr/spinnerDropDownItemStyle
We now have the attribute name we need.
It's better to do it that way rather than try to guess what attribute to use because you know which attribute the system itself use so it's very likely to be the correct one (unless there's a bug).
If I created a custom layout for the adapter I'd have to create
9-patch images etc.
Well, no, the layout determines what kind of GUI element you would have (a textfield, a spinner, an imagebutton, a custom element...), not how they are styled (nine-patch backgrounds, text colors...), so you still would have to mess with styles to get the right appearance.
For example, for visual consistency I ported the button, checkbox and spinner style from Theme.Holo to Gingerbread, yet I did not mess with layout, all I did was the aforementioned steps plus looking up the result (spinnerDropDownItemStyle in the above example) in themes.xml, which gave me the style name (e.g.: Widget.Holo.DropDownItem.Spinner).
Then I looked that up in styles.xml and imported it (and any parent*) in my project's styles.xml, searching and copying any Holo specific reference in my project and adjusting the namespace accordingly (add android: to attributes and replace ?android:attr with #style for what I copy to my styles.xml file).
So far I haven't had to mess with layouts at all (even the presence of radio buttons in spinner dialogs on Gingerbread is determined by an xml attribute: android:checkMark).
If a style has no parent attribute (like Widget.Holo.DropDownItem.Spinner) then its parent is the same style minus the last element (e.g.: Widget.Holo.DropDownItem)
Is there a way to format all TextViews, Buttons or whatever with a theme ?
Like in CSS when i want to format all a-tags
a{
/some css here/
}
I want to do that in android via xml when I'm applying a theme to my application.
Any ideas ?Thanks
http://bartinger.at/
Update 1.0:
I want to create a theme that formats the text in all TextViews green and in all EditTexts red. So that i just apply the theme and I never have to worry about the style attribute!
Update 1.1:
So I found some that piece of code and I think that's a good beginning
<item name="android:textViewStyle">#style/MyTextView</item>
<item name="android:buttonStyle">#style/MyButton</item>
I think thats the answer to my question. But I have another one. I want to write my own ActionBar and wanted to know how I can apply a default style or default attributes (again without adding the style attribute in the layout xml :P )
I have a class
public class ActionBar extends LinearLayout{ }
and I'm gonna use it like that in my application
<at.bartinger.uil.ActionBar>....</at.bartinger.uil.ActionBar>
The ActionBar should have some default attributes (like height and width) and then adding some custom style attributes which could change from app to app (like background)
yes you can you can apply a theme to the whole application and then all your textviews will have that style.
Inside the styles.xml file you have to define your CustomTheme
for example:
<style name="CustomTheme" parent="android:Theme.Light">
<item name="android:windowBackground">#color/custom_theme_color</item>
<item name="android:colorBackground">#color/custom_theme_color</item>
</style>
you add something like text
"android:textStyle="myStyle" and specify the details in Mystyle
You can apply a style read more here.
for the action abr you should look here http://developer.android.com/guide/topics/ui/actionbar.html
especially at the bottom it explains very well how to style the bar