Example usage for android.widget Switch getTrackDrawable

List of usage examples for android.widget Switch getTrackDrawable

Introduction

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

Prototype

public Drawable getTrackDrawable() 

Source Link

Document

Get the drawable used for the track that the switch slides within.

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  w  w  w.  j av a  2s.c  o  m
    }
}