Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TextView change textColor with Theme

I am trying to put all my Styles into a Theme for my app. However, I'm having trouble changing the textColor on a TextView. This is from my attrs.xml file.

<?xml version="1.0" encoding="utf-8"?>

<declare-styleable name="flat">
    <attr name="detail_name_view" format="reference" />

</declare-styleable>

This is from my styles.xml file

<style name="flat_theme" parent="android:Theme.NoTitleBar">
    <item name="detail_name_view">@style/flat_detail_name</item>
</style>
<style name="flat_detail_name" parent="@android:style/Widget.TextView">
    <item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
    <item name="android:textColor">@color/detail_group_black</item>
    <item name="android:textSize">16sp</item> 
    <item name="android:singleLine">true</item>
    <item name="android:ellipsize">end</item>
</style>

And this is from my layout.xml file

    <TextView
        android:id="@+id/detail_name"
        style="?detail_name_view"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_gravity="left|center_vertical"
        android:layout_marginLeft="5dp"
        android:layout_weight="1"
        android:gravity="left|center_vertical" />

I also have the manifest set up correctly with the following.

    <application
    android:name="com.example.blah"
    android:icon="@drawable/blah"
    android:label="@string/app_name"
    android:theme="@style/flat_theme"

The color never gets changed to the "@color/detail_group_black". Am I missing something simple? Or is the problem that Android is already creating a textColor attribute for the TextView and that's why the style isn't overriding it? My attribute-theme strategy seems to be working well for the rest of the app but not this. Any ideas? Thanks in advance.

Also, if it can be helped I'd rather not create a custom TextView class just for this change: its a fairly large project and I'm not sure what kind of problems I might run into if I extend TextView for everything.

Edit Also, if I change the TextView to have style="@style/flat_detail_name_view", it works fine. The reason I don't want to do that is so that I can change the appearance of these textviews just by changing the theme. Using style="?something_else" works on all my other views, so I really don't understand this and I'm probably missing something simple?

like image 384
pickle_inspector Avatar asked Nov 03 '22 17:11

pickle_inspector


1 Answers

I found my problem, in case anyone runs into a similar situation! Basically the reason this wasn't working is because I was inflating the view with the following code.

    private View buildGroupView(ExpandListGroup group) {
    LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    View view;
    switch(group.getType()){
        case ExpandListGroup.HEADER :
            view = inf.inflate(R.layout.three_col_header, null);
            break;
        default:
            view = inf.inflate(R.layout.detail_expandablelist_group_item, null);
            break;

    }

    return view;
}

And in my Adapter's getGroupView I was returning it.

    public View getGroupView(int groupPosition, boolean isLastChild, View convertView,
        ViewGroup parent) {
    View view = convertView;
    ExpandListGroup group = (ExpandListGroup) getGroup(groupPosition);

    view = buildGroupView(group);

    setGroupViewData(group, view);

    return view;
}

Apparently my problem was that I needed to replace LayoutInflater

inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

with ..

LayoutInflater inf = mActivity.getLayoutInflater();

As suggested by CommonsWare at the bottom of this post: Theme/Style is not applied when inflater used with ApplicationContext .

Thanks for your help.

like image 57
pickle_inspector Avatar answered Nov 11 '22 12:11

pickle_inspector