jamesc
jamesc

Reputation: 12837

Android, How to theme font colour for just TextView Widgets?

I have just applied a black background image in my theme using

<style name="AppBaseTheme" parent="Theme.Sherlock.Light">
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:windowBackground">@drawable/background</item>
</style>

and now I wish to change the colour of just my text views to white so that they are visible against the dark background

I thought that setting the android:textColor for TextView would do the trick like so

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:windowBackground">@drawable/background</item>
    <item name="android:textViewStyle">@style/Ff1TextViewStyle</item>
</style>


<style name="Ff1SpinnerViewStyle" parent="android:Widget.TextView">
    <item name="android:textColor">@color/sysWhite</item>
    <item name="android:textStyle">bold</item>
</style>

But to my dismay this just totally stuffs up all sorts of things like spinner text and spinner dropdown text, ActionBar Drop Down menus by changing the text to white on white backgrounds all of which I was perfectly happy with before plus I'm sure other controls that I haven't used yet or haven't discovered yet could well end up with white text.

I find it hard to believe that android themes are so messed up that I am unable to confidently just set the text colour of TextView widgtes without affecting widgets that, what I can only assume, are children of TextView widgets so I am thinking that it must just be my lack of knowledge that is to blame and I am hoping and looking for enlightenment for what must be a common requirement.

Any help appreciated.

Upvotes: 3

Views: 13130

Answers (2)

hardartcore
hardartcore

Reputation: 17037

I'm not really sure if you can set a style only for TextView, because Actionbar , Spinner and other Views contains TextView as their childs too, so it will affect them too as you find out. In my opinion you should create a style and set it to your TextView's. Something similar to this :

 <style name="MyTextView" parent="@android:style/TextAppearance.Small">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textSize">12sp</item>
</style>

and use it in your TextView's xml declaration :

<TextView
   style="@style/MyTextView" />

or the second thing which you can do is to create your custom TextView

public class MyTextView extends TextView {

     public MyTextView(Context context){
         super(context);
         this.setTextColor(android.R.color.white);
     }
}

and use MyTextView in your xml files :

<com.my.package.name.MyTextView
   android:id="@+id/my_textview" />

Edit:

The thing why setting android:textViewStyle affects all other view's is just because if you look closer in android source you will see that every textview style used in other views is child of android.Widget.TextView . So the best thing which you can do in this situation in my opinion is to create a single style and set it to your TextView 's in xml.

Hope this information will help!

Upvotes: 9

ScouseChris
ScouseChris

Reputation: 4397

You can create a style and apply it only to a specific element in your TextViews e.g. in your layout xml file:

<TextView
    style="@style/Ff1SpinnerViewStyle"
    android:id="@+id/myView" />

You would apply a standard theme to your Activity, only the elements you override the style on in your layout file will have their appearance changed.

You won't need to override AppBaseTheme with any TextView specific attributes.

Upvotes: 1

Related Questions