1

This is how it looks like unchecked:

pic

and checked:

pic

checked clearly doesn't match design, so how can I make it look like unchecked, when it's checked?

Gintas_
  • 4,940
  • 12
  • 44
  • 87

2 Answers2

1

You can tint it with the same color for both statement:

    switchButton.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            isTouched = true;
            switchButton.getThumbDrawable().setColorFilter(getResources().getColor(R.color.your_color), PorterDuff.Mode.SRC_IN);
            return false;
        }
    });
    switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            if (isTouched) {
                isTouched = false;
                if (isChecked) {
                    switchButton.getThumbDrawable().setColorFilter(getResources().getColor(R.color.your_color), PorterDuff.Mode.SRC_IN);
                }
                else {
                    switchButton.getThumbDrawable().setColorFilter(getResources().getColor(R.color.your_color), PorterDuff.Mode.SRC_IN);
                }
            }
        }
    });
Djordje Tankosic
  • 1,975
  • 2
  • 20
  • 21
1

Got it! I had to do some research, how android finds that darker color for the track...

switchview.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            switchview.getThumbDrawable().clearColorFilter();
            int colorPrimaryDark = getResources().getColor(R.color.colorPrimaryDark);
            int r = (colorPrimaryDark >> 16) & 0xFF;
            int g = (colorPrimaryDark >> 8) & 0xFF;
            int b = (colorPrimaryDark >> 0) & 0xFF;
            r = (r - 30 < 0) ? 0 : r - 30;
            g = (g - 30 < 0) ? 0 : g - 30;
            b = (b - 30 < 0) ? 0 : b - 30;
            int darker = Color.rgb(r, g, b);
            switchview.getTrackDrawable().setColorFilter(darker, PorterDuff.Mode.SRC_IN);
        }
    });

Now checked and unchecked will be exactly the same.

Gintas_
  • 4,940
  • 12
  • 44
  • 87