apply custom theme in android preference

3

I want to do few things in my preference screen.

  1. apply custom font to the title
  2. change some icons of preference
  3. change background color and font color when the preference is selected(or touched?)

First, I started with CheckBoxPreference. to apply a custom font, I make myCheckBoxPreference.

public class MyCheckBoxPreference extends CheckBoxPreference {
    public String robotoRegular = "fonts/Roboto-Regular.ttf";

    public MyCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }

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

    public MyCheckBoxPreference(Context context) {
        super(context);
    }

    @Override
    protected View onCreateView(ViewGroup parent) {
        View view = super.onCreateView(parent);

        TextView titleView = (TextView) view.findViewById(android.R.id.title);
        Typeface tf = Typeface.createFromAsset(view.getContext().getAssets(), robotoRegular);
        titleView.setTypeface(tf);

        return view;

    }
}

and it works fine.

to change the checkbox image, I made a theme and apply it.

in my styles.xml

<style name="MyTheme" parent="Theme.Sherlock.Light">
    <item name="android:checkboxStyle">@style/MyCheckbox</item>
</style>
<style name="MyCheckbox" parent="android:Widget.CompoundButton.CheckBox">
    <item name="android:button">@drawable/btn_check</item>
</style>

to change the background color of preference list, I also made a theme and apply it.

<style name="MyTheme" parent="Theme.Sherlock.Light">
    <item name="android:checkboxStyle">@style/MyCheckbox</item>
    <item name="android:listViewStyle">@style/MyListView</item>
</style>

<style name="MyListView" parent="android:Widget.ListView.White">
    <item name="android:listSelector">@drawable/list_selector_background</item>
</style>

it works all fine, too.

but I got stucked to change font dynamically. I tried using theme

<style name="MyTheme" parent="Theme.Sherlock.Light">
    <item name="android:textColorPrimary">#717171</item>
    <item name="android:textColorPrimaryInverse">#FFFFFF</item>
    <item name="android:textColorPrimaryDisableOnly">#dcdcdc</item>    
</style>

but not worked.

and I tried using onTouchListener in MyCheckBoxPreference. now onCreateView in MyCheckBoxPreference.java became as follow

@Override
protected View onCreateView(ViewGroup parent) {
    View view = super.onCreateView(parent);

    TextView titleView = (TextView) view.findViewById(android.R.id.title);
    Typeface tf = Typeface.createFromAsset(view.getContext().getAssets(), robotoRegular);
    titleView.setTypeface(tf);
    titleView.setTextColor(0xFF717171);

    view.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            TextView titleView = (TextView) v.findViewById(android.R.id.title);
            int actionMasked = event.getActionMasked();
            switch (actionMasked)
            {
                case MotionEvent.ACTION_DOWN:
                {
                    titleView.setTextColor(0xFFFFFFFF);
                    break;
                }
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                {
                    titleView.setTextColor(0xFF717171);
                    break;
                }
                default:
                {
                    Log.d("MyCheckBoxPreference", "action" + event);
                }
            }

            return false;
        }
    });

    return view;

}

At first, it seemed to work but it has a problem. if I touch an item and move my finger to another item, the font color remains white(0xFFFFFFFF). In that code, the title text color sets first #717171 in onCreateView. When the list item is selected the color changed in onTouch with MotionEvent.ACTION_DOWN. And I expected when I unselect the item MotionEvent.ACTION_UP or MotionEvent.ACTION_CANCEL make the font color to #717171. However, when I unselect the item, the font color is changed in the onCreateView. I think because the preference is changed, the system wants to create the view again.

This works only when preference is toggled. As I mentioned above when I touch an item and move my finger to another item, the font color remains white because preference status remains unchanged and MotionEvent.ACTION_CANCEL is not reached.

If I return true in OnTouchListener MotionEvent.ACTION_UP and MotionEvent.ACTION_CANCEL are reached but it consumes the event and background color is not changed.

This is my story to come to here.

I apply styles in ListView by making custom list_item.xml and set textColor with my listview_item_selector.xml But in the preference, it uses default ListView, so I don't know how to do it.

What should I do? Do I have to customize all the preferences? Any suggestions would be great. Thanks.

android
android-listview
themes
preference
asked on Stack Overflow Jan 16, 2013 by Ingtellect • edited Nov 16, 2018 by Mostafa Arian Nejad

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0