1

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.

Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
  • Please share your style XML's. We can't help you with this information. – mehmetminanc Sep 10 '12 at 15:15
  • How we add styles progrmatically? http://stackoverflow.com/q/11507476/1012284 – Padma Kumar Sep 10 '12 at 15:39
  • Thanks. This is a short-term solution only. What if I was interested in changing more than just text appearance? Would the application of all styles apply for CodeFont? For instance, if I had specified a drawable background among other styles, then would they also work? It just seems odd to slip these styles in under the radar so to speak with SetTextApperance( ). – user1660503 Sep 10 '12 at 16:17

3 Answers3

0

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.

mkatic
  • 141
  • 3
  • 13
0

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>
Michael Celey
  • 12,645
  • 6
  • 57
  • 62
  • Sorry but this still didn't work. I think it might have something to do with the null attribute set I'm passing into the EditText CTOR. Padma Kumar pointed out an interesting fix by using SetTextAppearance. However, this only addresses the text color. I don't believe that drawables can also be set. – user1660503 Sep 10 '12 at 16:16
0

Ok. I finally found the solution. I had to piece it together from a few resources.

  1. 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( ).
  2. 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?
  3. 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);
Community
  • 1
  • 1