I finally understood what themes are and how to apply them to a dialog.
I would like to create a theme and I need help from you to achieve what I want. I basically want to create a theme to apply to alert dialogs and to dialogs throughout my application.
In my theme, I need to define the title background color, title text size, main body background color and main body text size. What items should I use?
Here is the starting code to prove that I put some work into it :)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="myGeneralDialogStyle" parent="#android:style/Theme.Dialog">
<item name="?"></item>
</style>
</resources>
IF all you got is the XML you shown above, then if your questions is about this "?" sign in name="?" part, then only thing you can use here are defined attributes. In your case you can i.e. use android:* ones. If you need to define own attributes then see here: Declaring a custom android UI element using XML
Related
My calculator app consists of 30 buttons. I want to provide themes for the calculator keypad. A theme changes button background (gradients, not image backgrounds) and font. Some themes have the same color for all buttons while some have a color for numbers, another color for operators and so on.
The color change is using selectors from res/drawable/*.xml
How do I change the theme via the code?
Hopefully avoiding typing:
button.setBackground(Drawable background);
button.setTypeface(font);
30 times. And if I have 5 themes, then 30 * 5 * 2 = 300 lines of codes!!
I'm new to this and if there is no other way I'll go with the 150 lines.
Also how do I save the user theme selection? Using preferences?
You can create a custom XML theme which will change all of your XML components. After creating a new theme, go into the Android Manifest file and change the theme. For example:
<activity
android:name="com.myapp.MyActivity"
...
android:theme="#style/MyCustomTheme" />
To create the theme, go to res/values/themes.xml and create a new theme with an identifier:
<resources>
...
<style name="MyCustomTheme" parent="android:style/Theme">
<item name="android:textColorPrimary">#ffff0000</item>
</style>
...
</resources>
By using this method, you can create an extensive library of different themes and change to what theme you want.
NOTE: This is not just for changing the background, but it can also be used to change the theme of the buttons. Visit this website for more information:
http://janrain.com/blog/introduction-to-android-theme-customization/
EDIT: As that user commented, it is possible that you can put the function to change the theme of the button in a for() loop.
For your case, I have derived this from the link above. It will change the texture of the buttons in your XML file rather than in Java.
"Using a Custom Nine-Patch With Buttons
A nine-patch drawable is a special kind of image which can be scaled in width and height while maintaining its visual integrity. Nine-patches are the most common way to specify the appearance of Android buttons, though any drawable type can be used.
Example nine-patch PNG.
Notice the one pixel black lines around the edge, they control the scaling of the image.
Save this bitmap as MyApplication/res/drawable/my_nine_patch.9.png
Define a new style (you can define the new style in the same file that you defined your custom theme from Creating a Custom Android Theme above) …:
<resources>
...
<style name="MyCustomButton" parent="android:Widget.Button">
<item name="android:background">#drawable/my_nine_patch</item>
</style>
...
</resources>
Apply the new button style to the buttonStyle attribute of your custom theme:
<resources>
...
<style name="MyCustomTheme" parent=...>
...
<item name="android:buttonStyle">#style/MyCustomButton</item>
</style>
...
</resources>
Now the buttons in the activities your theme is applied to have custom images. However, you may notice that they don’t change appearance when selected. Read Selector Drawables below for an introduction to using multiple drawables to define one drawable that changes based on state."
From here, you can change certain components of the theme (such as the button texture as an image).
After you have a theme that looks good, apply it in the Android Manifest as I mentioned above.
I will FURTHER edit this if it still does not answer your question.
I'm trying to make a custom titlebar for my first Android application.
While I can find lots on the web about how to make them so you can change colours etc, I want my titlebar to look the same as the "standard" titlebar, but with a button that I can configure. This means copying the device's currently active themes to be able to style it in exactly the same way.
Not all devices simply use a gradient in the titlebar style, so adding a gradient (as suggested in other SO questions) doesn't really make sense.
Does anyone have any pointers how to read the style information?
try to extend an existing theme e.g.
create your own style which can ofcourse extend from existing from an existing theme. change the windowNoTitle to true.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="noTitleBarStyle" parent="android:Theme.Black">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#color/darkGrey</item>
<item name="android:colorForeground">#ff3333</item>
</style></resources>
or try to do it runtime as discussed here
Android - change custom title view at run time
I hope this helps.
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
I have a couple custom preference items -- one that displays a swatch of the currently selected color, and another one that displays a thumbnail.
I have a custom layout for these that matches up very well, and have found that I can make the text appearance match by using android:textAppearance="?android:attr/textAppearanceLarge" as part of the TextView's xml. The problem is, while these generally look fine, they must not be the appearance the 'official' preferences use, since the colors end up wrong on some devices. In particular I'm porting my application to the Nook Color, and it uses a light grey background and black text color on the preference screen instead of black background/light grey text. My text color in this situation stays the same, but the rest of my layout is themed appropriately.
I'm really unsure what I'm supposed to do here to make my text match up with the 'official' theme. Should I be using obtainStyledAttributes and running though my layout to set things? The tutorials I've seen on using that so far have been really baffling, and it seems like there must be a textAppearance or style I can set in the XML to fix this.
You've to define your own application theme which inherits from the official theme you want. Actually, you can just define a theme MyTheme extending the Theme.Light for example.
Create an res/values/styles.xml file like that:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="#android:style/Theme.Light">
</style>
</resources>
Then, you just have to apply your theme using the android:theme attribute of the application entity of your AndroidManifest.xml:
<application android:theme="#style/MyTheme">
[...]
</application>