1

I have developed a settings page using PreferenceScreen in customlayout. There is one switchpreference which i want to change color. Currently it takes default color.But i want to change its color when user switches it on and off.When user switch on ,the on side should be red color and when user switch to off side it "off" side should be light gray.My Switchpreference code is as below:

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

<PreferenceCategory android:key="pref" >

    <SwitchPreference
        android:key="switchbutton"
        android:summaryOff="OFF"
        android:summaryOn="ON"
        android:title="start" />
</PreferenceCategory>

I a newbie to android programming so please co-operate.I will be glad if someone helps me .Thanks in advance!!

2 Answers2

1

I posted this answer in a similar question at Custom SwitchPreference in Android

One way of doing this is to subclass the SwitchPreference and override the onBindView method. In doing so, you'll want to still call super.onBindView(view) in that method, but then find the Switch in the child views and style it as appropriate:

package com.example;

import android.annotation.SuppressLint;
import android.content.Context;
import android.preference.SwitchPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;

import com.example.R;


public class CustomSwitchPreference extends SwitchPreference {

    @SuppressLint("NewApi")
    public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

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

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

    @Override
    protected void onBindView(View view) {

        super.onBindView(view);
        Switch theSwitch = findSwitchInChildviews((ViewGroup) view);
        if (theSwitch!=null) {
            //do styling here
            theSwitch.setThumbResource(R.drawable.new_thumb_resource);
        }

    }

    private Switch findSwitchInChildviews(ViewGroup view) {
        for (int i=0;i<view.getChildCount();i++) {
            View thisChildview = view.getChildAt(i);
            if (thisChildview instanceof Switch) {
                return (Switch)thisChildview;
            }
            else if (thisChildview instanceof  ViewGroup) {
                Switch theSwitch = findSwitchInChildviews((ViewGroup) thisChildview);
                if (theSwitch!=null) return theSwitch;
            }
        }
        return null;
    }
}
Community
  • 1
  • 1
Chris
  • 1,180
  • 1
  • 9
  • 17
0

Try this:

mViewSwitcher.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
// set color here
return false;
}
});
QArea
  • 4,955
  • 1
  • 12
  • 22