androidandroid-preferences

How to change text color of preference category in Android?


The textColor attribute isn't working. Here's my XML:

<PreferenceCategory
        android:title="Title"
        android:textColor="#00FF00">

Any ideas?


Solution

  • use this customize PreferenceCategory class :

    public class MyPreferenceCategory extends PreferenceCategory {
        public MyPreferenceCategory(Context context) {
            super(context);
        }
    
        public MyPreferenceCategory(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public MyPreferenceCategory(Context context, AttributeSet attrs,
                int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void onBindView(View view) {
            super.onBindView(view);
            TextView titleView = (TextView) view.findViewById(android.R.id.title);
            titleView.setTextColor(Color.RED);
        }
    }
    

    and add this at your Pref.xml file :

    <ali.UI.Customize.MyPreferenceCategory android:title="@string/pref_server" />