0

I have five different colors in colors.xml.

<color name="colorSU">#4130f0</color>
<color name="colorBM">#ff752d</color>
<color name="colorTM">#2a8cbd</color>
<color name="colorBE">#7400d5</color>
<color name="colorSE">#22B573</color>

Now I need to apply this in toggle "button" on. (Round toggle)

toggleButton = (Switch)view.findViewById(R.id.switch_filter);
toggleBtnUserRoleDrawable();

But the implementation below is not changing the round switch button color. It's always taking the default theme color.

 private void toggleBtnUserRoleDrawable() {
        String userRole = AppUtils.getUserRole(mSharedPreferences);
        switch (userRole) {
            case USER_TYPE_SE:
               // toggleButton.setBackground(getResources().getDrawable(R.drawable.toggle_button_se));
                //toggleButton.setTrackDrawable(getResources().getDrawable(R.color.colorSE));
                break;
            case USER_TYPE_TM:
                toggleButton.setBackground(getResources().getDrawable(R.drawable.colorTM));
                toggleButton.setTrackDrawable(getResources().getDrawable(R.color.colorTM));
                break;
            case USER_TYPE_BM:
                toggleButton.setBackground(getResources().getDrawable(R.drawable.colorBM));
                toggleButton.setTrackDrawable(getResources().getDrawable(R.color.colorBM));
                break;
            case USER_TYPE_BE:
                toggleButton.setBackground(getResources().getDrawable(R.drawable.colorBM));
                toggleButton.setTrackDrawable(getResources().getDrawable(R.color.colorBE));
                break;
            case USER_TYPE_SU:
                toggleButton.setBackground(getResources().getDrawable(R.drawable.colorSU));
                toggleButton.setTrackDrawable(getResources().getDrawable(R.color.colorSU));
                break;
        }
    }
Adinia
  • 3,722
  • 5
  • 40
  • 58
Walter
  • 189
  • 2
  • 16

1 Answers1

0

You can simply apply a ColorFilter on the drawable object as following:

case USER_TYPE_SU:
 int color = ContextCompat.getColor(this,R.color.colorSU)            
 toggleButton.getTrackDrawable().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);

  break;

You can do the same for getThumbDrawable() and getBackground()