Example usage for android.widget Switch getThumbDrawable

List of usage examples for android.widget Switch getThumbDrawable

Introduction

In this page you can find the example usage for android.widget Switch getThumbDrawable.

Prototype

public Drawable getThumbDrawable() 

Source Link

Document

Get the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track.

Usage

From source file:com.owncloud.android.ui.ThemeableSwitchPreference.java

@RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
private void findSwitch(ViewGroup viewGroup) {
    for (int i = 0; i < viewGroup.getChildCount(); i++) {
        View child = viewGroup.getChildAt(i);

        if (child instanceof Switch) {
            Switch switchView = (Switch) child;

            int color = ThemeUtils.primaryAccentColor();
            int trackColor = Color.argb(77, Color.red(color), Color.green(color), Color.blue(color));

            // setting the thumb color
            DrawableCompat.setTintList(switchView.getThumbDrawable(),
                    new ColorStateList(new int[][] { new int[] { android.R.attr.state_checked }, new int[] {} },
                            new int[] { color, Color.WHITE }));

            // setting the track color
            DrawableCompat.setTintList(switchView.getTrackDrawable(),
                    new ColorStateList(new int[][] { new int[] { android.R.attr.state_checked }, new int[] {} },
                            new int[] { trackColor, Color.parseColor("#4D000000") }));

            break;
        } else if (child instanceof ViewGroup) {
            findSwitch((ViewGroup) child);
        }//from   ww  w.j a  va2  s.  c  o m
    }
}