0

I want to do this: when the CheckBoxPreference is unchecked, the text color of the CheckBoxPreference's title become gray and if checked, the title's text color reverts to original color (depends on the theme).

What I did until now: I created a new class extending from CheckBoxPreference.

public class CustomCheckBoxPreference extends CheckBoxPreference{

    TextView txtTitle;
    int originalTextColor;

    public CustomCheckBoxPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onBindView(View view) {

        txtTitle = (TextView) view.findViewById(android.R.id.title);
        originalTextColor = txtTitle.getCurrentTextColor();

        setOnPreferenceClickListener(new OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                if (isChecked()) {
                    txtTitle.setTextColor(originalTextColor);  //it doesn't work
                }
                else {
                    txtTitle.setTextColor(Color.GRAY);  //it doesn't work
                }
                return true;
            }
        });

        super.onBindView(view);
    }
}

When I run the application, the txtTitle.setTextColor(..) apparently didn't work, the text color didn't change at all. I also have confirmed with the debugger that the onPreferenceClick method was called.

null
  • 8,669
  • 16
  • 68
  • 98
  • you are using a check isChecked() but where is it defined? instead of using just icChecked() you need to use ((CheckBoxPreference)preference).isChecked() for checking purpose – Rajen Raiyarela Oct 08 '14 at 11:20
  • @RajenRaiyarela: I extended from the CheckBoxPreference class, that's where it is come from. – null Oct 15 '14 at 11:44

1 Answers1

0

Even I did the same and didn't work for me, I too don't know the reason.

But it will work, if you remove onPreferenceClickListener() and use only if else.

protected void onBindView(View view) {

    txtTitle = (TextView) view.findViewById(android.R.id.title);
    originalTextColor = txtTitle.getCurrentTextColor();


            if (isChecked()) {
                txtTitle.setTextColor(originalTextColor); 
            }
            else {
                txtTitle.setTextColor(Color.GRAY); 
            }
            return true;

    super.onBindView(view);

}
quest
  • 447
  • 2
  • 7
  • 20