I'm having difficulty getting Android Styles to work.
As a test, I created an EditText control and associated it with a Style called "CodeFont" like such:
EditText et = new EditText(this, null, Resource.Style.CodeFont);
Next, I defined a style which inherits from a standard style and changes the text color to red like such:
<resources>
<style name="CodeFont" parent="#android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#FF0000</item>
<item name="android:typeface">monospace</item>
</style>
</resources>
What am I doing wrong? When the EditText appears, it no longer features the orange border on focus. This makes me think that the styling is working. However, the text color remains black.
Unless you need your EditText to be dynamically generated you should use XML and then define the style via XML. This is separates your GUI from your code which can have many benefits.
Like this you can simply specify the style like so:
style="#style/CodeFont"
See the Android developers' website for more information.
The border isn't showing up since you're using "#android:style/TextAppearance.Medium" as your parent style. TextAppearance.Medium is a textAppearance attribute style, not a widget style. If you want the orange border and everything to still show up but have a medium text size then you'd want to do something like this:
<style name="CodeFont" parent="#android:style/Widget.EditText">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#FF0000</item>
<item name="android:textAppearance">#android:style/TextAppearance.Medium</item>
<item name="android:typeface">monospace</item>
</style>
Ok. I finally found the solution. I had to piece it together from a few resources.
In Android 4.0 (Ice Cream Sandwich), there is a known problem with specifying the theme name in the attribute section of the activity: Cannot Resolve #style/Theme.Sherlock. If you use the attribute to set the theme then the compiler will return "No resource found that matches the given name (at 'theme' with value '#style/blah-blah'". So you want to set theme using the SetTheme( ) API within the Activity's OnCreate( ).
When you create your style, the style needs to be part of a theme which you referenced in Step 1. Make sure you group your styles within your theme (you can inherit from the standard Android themes if you like). For each of your style names, you will need to create an attribute reference as described in the link in Step 1. The following link is the "correct" way to define a style name resource attribute: How do I create my own resource names?
You need to reference your style from your theme via the attribute you defined in Step 2. For example:
EditText et1 = new EditText(this, null, Resource.Attribute.CodeFontRef);
Related
For a small tool, I have to write an Android app. There are no requirements on portability, it's sufficient when the app only runs on android version 6 or later.
I would love to group dialog elements into CardViews and I would love to have some reasonable layout (spacing, colors, etc). Is there a way to use a theme, standard layout, style, etc. that I could use without the need to apply "android:padding", "card_view:cardElevation", ect. attribute to every CardView?
If it's not possible to use some already existing defaults, I could use styles. When I use styles (following Google's "Styles and Themes" API Guide), I get error messages, when I move some attributes from the CardView definition from the layout xml to the style xml. For attributes that I move to the style xml, that begin with "android:", there is no error. For other attributes, I get an `No resource found that matches the given name-error.
<style name="CardGroups">
<item name="xmlns:card_view">"http://schemas.android.com/apk/res-auto"</item>
<item name="card_view:cardElevation">3dp</item>
<item name="android:layout_margin">3dp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
results in: Error:(20, 5) No resource found that matches the given name: attr 'card_view:cardElevation'.
You are correct that styles are the way to go to provide a common set of attributes to multiple Views.
You should not use custom namespaces in your style definitions. For custom attributes provided by libraries (such as cardElevation), you simply do not provide a namespace.
Thus your style should look like this:
<style name="CardGroups">
<item name="cardElevation">3dp</item>
<item name="android:layout_margin">3dp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
I've been trying to change the color of the EditText line on the bottom, but my EditText still doesn't wanna change the color, but it's using the style from Material.
I've tried to change the color following these answer: link
So I have an EditText with the Material form but not the colors I've set.
I've put appcompat-v7 in my Gradle config file, and it's already working for the color primary.
<style name="AppBaseTheme.MiddleTheme.Widget.EditText" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/middle_primary</item>
<!-- Those 3 lines are staying in red war in IntelliJ but it does build the project -->
<item name="android:colorControlNormal">#color/middle_sqool_primary</item>
<item name="android:colorControlActivated">#color/middle_sqool_primary</item>
<item name="android:colorControlHighlight">#color/middle_sqool_primary</item>
</style>
You have to set the colorControlActivated, colorControlHighlight and colorControlNormal in your app theme (or activity theme) not in your edittext style.
Something like this:
<style name="Theme.App.Base" parent="Theme.AppCompat.Light">
<item name="colorControlNormal">#color/middle_sqool_primary</item>
<item name="colorControlActivated">#color/middle_sqool_primary</item>
<item name="colorControlHighlight">#color/middle_sqool_primary</item>
</style>
As a workaround, try using android.support.v7.widget.AppCompatEditText rather than EditText. You should always use that for programmatically created views.
Pretty sure you made it, but be sure you are editing themes in the values folder and not in the values-v21 one (if any).
I had a similar issue. Wanted to change the highlight colour of the menu items. And none of these attributes didn't work (Support Library v25.0.0).
The only attribute that allowed me to change colour on Pre-L devices is this:
<item name="actionBarItemBackground">#drawable/YOUR_DRAWABLE</item>
I stopped searching for further workarounds because it's obvious that Support Library is NOT ready for production code.
I'm trying to put a style in all my app, so i created a theme with my style inside :
<resources>
<style name="MyTheme" parent="android:Theme.Holo.Light">
<item name="android:textAppearance">#style/subtitle</item>
</style>
<style name="subtitle parent="#android:style/TextAppearance">
<item name="android:textColor">#color/purple</item>
<item name="android:textSize">40sp</item>
</style>
</resources>
But textAppearance doesn't work it stay the same, but when i put something like textColor in my theme, it works
This is a quite old question, but the answer may help someone.
The key to solve this is in the "precedence order of styling techniques" here:
on the top is the highest precedence, at the bottom is the lowest precedence.
As we can see theme has the lowest precedence, in your example, your android:textAppearance property is being overridden by the default style of every view that accepts this attribute, the default style property is defined in every them for every specific view that accepts this attribute, in this case android:Theme.Holo.Light provides the default style for textView as android:textViewStyle... for buttons is android:buttonStyle (which inherits its textAppearance from TextView), and so on.
So if you are trying to apply that android:textAppearance property to a TextVew you should use <item name="android:textViewStyle">#style/subtitle</item> instead of <item name="android:textAppearance">#style/subtitle</item> inside MyTheme. Away to veryfy this is setting android:textViewStyle to null, that way your current code will work fine with textViews <item name="android:textViewStyle">null</item>
This post explains this precedence a bit deeper:
https://medium.com/androiddevelopers/whats-your-text-s-appearance-f3a1729192d
What I can see is, you have not declared the color in your xml for theme. Please add the following line within the <resources> and try. Your xml will look like:
<resources>
<style name="MyTheme" parent="android:Theme.Holo.Light">
<item name="android:textAppearance">#style/subtitle</item>
</style>
<color name="purple">code for your color</color>
<style name="subtitle parent="#android:style/TextAppearance">
<item name="android:textColor">#color/purple</item>
<item name="android:textSize">40sp</item>
</style>
I think this will do.
Depends on your target API you need to put your customization code in different /res/values-vxx/style.xml files.
For TextView, try android:textAppearanceSmall inside your theme instead.
I hope I can explain what I'm after. In essence, my users have asked me to allow different looks in my application, which I hope I can do with themes.
I hoped I could do something like this:
<style name="NewTheme" parent="android:Theme.Dark">
<item name="labelColor">#f90</item>
<item name="buttonColor">#fff</item>
<item name="buttonBg">#drawable/button</item>
</style>
<style name="OldTheme" parent="android:Theme.Dark">
<item name="labelColor">#fa0</item>
<item name="buttonColor">#88f</item>
<item name="buttonBg">#drawable/button_old</item>
</style>
And then reference these values in my styles.xml:
<style name="labelStyle">
<item name="android:textColor>#labelColor</item>
</style>
<style name="buttonStyle">
<item name="android:textcolor">#buttonColor</item>
<item name="android:background">#buttonBg</item>
</style>
I know this syntax is wrong, but what might be the right syntax? Basically, I want to create sets of attributes (color, background, a couple other things) and select them based on theme.
To work with themes and styles in Android you have to:
Define one or more themes in themes.xml and set the definitions of
your styles there.
Define custom attributes, a.k.a. custom styles, in attrs.xml.
Describe what the values of your custom styles are in styles.xml.
In your layout files, give your views a style attribute, which has a
custom style name as their values.
Set the theme of your application or activity in either
AndroidManifest.xml or in the Activity's onCreate(). This is done by calling setTheme() in the activity's onCreate() method, before any call to setContentView().
To change the theme, you simply need to restart your activity.
Iadvice you to look at this tutorial it deals with all that a programmer want to work on android themes (text color, text formatting, state list drawable etc ...)
I have a TextView and I want to apply a Style which I use for all TextView elements plus another style which I only use within a specific Activity. Is there any possibility to do that?
Just a little piece of information that might add to the overall value of the question - shamelessly copied from: http://developer.android.com/guide/topics/ui/themes.html#DefiningStyles
If you want to inherit from styles that you've defined yourself, you do not have to use the parent attribute. Instead, just prefix the name of the style you want to inherit to the name of your new style, separated by a period. For example, to create a new style that inherits the CodeFont style defined above, but make the color red, you can author the new style like this:
<style name="CodeFont.Red">
<item name="android:textColor">#FF0000</item>
</style>
Notice that there is no parent attribute in the tag, but because the name attribute begins with the CodeFont style name (which is a style that you have created), this style inherits all style properties from that style. This style then overrides the android:textColor property to make the text red. You can reference this new style as #style/CodeFont.Red.
You can continue inheriting like this as many times as you'd like, by chaining names with periods. For example, you can extend CodeFont.Red to be bigger, with:
<style name="CodeFont.Red.Big">
<item name="android:textSize">30sp</item>
</style>
A style under Android can have a parent style.
So just have MyActivityTextView define GeneralTextView as parent style, and change/add style properties.
Then you can use MyActivityTextView for some views and GeneralTextView for the others.
It's described in Defining Styles.
You can extend one style with another in your style.xml:
<style name="current_weekday_white" parent="current_day_white">
<item name="android:textColor">#FFABAB</item>
</style>
Inherit one style from another and copy elements from third.
<style name="Button.Style1" parent="android:style/Widget.Button">
<item name="android:gravity">center</item>
<item name="android:textSize">12sp</item>
<item name="android:background">#drawable/shape_button</item>
<!-- Here are attributes from third style -->
<item name="android:fontFamily">sans-serif-medium</item>
<item name="android:textColor">#112233</item>
</style>