Search code examples
androidcheckboxtextcolorpreferencescreen

Android CheckBoxPreference title color


I am using the code in this answer.

But instead of setting the android:textColor for the Text View i use

style="?background_text_color_theme"

which has to set the text color depending on the theme of the application ( white text color for black theme and vice versa). This works in all other places except for CheckBoxPreference.

Changes I made to the link I gave:

      <TextView android:id="@+android:id/title"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:singleLine="true" 
        android:ellipsize="marquee" android:fadingEdge="horizontal"
        style="?background_text_color_theme" />

Here is my style.xml:

    <style name="background_text_color_theme_bl">
    <item name="android:textColor">#ffffff</item>

</style>    

     <style name="background_text_color_theme_wh">
    <item name="android:textColor">#000000</item>

</style>    

Themes.Xml:

    <style name="Theme.White"  parent="@android:style/Theme.Holo.Light">
    <item name="background_text_color_theme">@style/background_text_color_theme_wh</item>
</style> 

   <style name="Theme.Black"  parent="@android:style/Theme.Holo">
    <item name="background_text_color_theme">@style/background_text_color_theme_bl</item>
</style>

But the text color does not seem to be set according to the style. Can anyone tell me why is it happening?

Thank You.


Solution

  • Here is how I did it..Just in case some one needs it in the future.

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

    And in the preference xml use this for the checkbox preference:

      <packagename.CheckBoxPreferenceClass
            android:key="@string/imp"
            android:title="@string/title"/>