ThemeableSwitchPreference.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2017 Tobias Kaminsky
  6. * Copyright (C) 2017 Nextcloud GmbH.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.ui;
  22. import android.content.Context;
  23. import android.content.res.ColorStateList;
  24. import android.graphics.Color;
  25. import android.preference.SwitchPreference;
  26. import android.util.AttributeSet;
  27. import android.view.View;
  28. import android.view.ViewGroup;
  29. import android.widget.Switch;
  30. import com.owncloud.android.R;
  31. import com.owncloud.android.utils.ThemeUtils;
  32. import androidx.core.graphics.drawable.DrawableCompat;
  33. /**
  34. * Themeable switch preference
  35. */
  36. public class ThemeableSwitchPreference extends SwitchPreference {
  37. public ThemeableSwitchPreference(Context context) {
  38. super(context);
  39. }
  40. public ThemeableSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
  41. super(context, attrs, defStyleAttr);
  42. }
  43. public ThemeableSwitchPreference(Context context, AttributeSet attrs) {
  44. super(context, attrs);
  45. }
  46. @Override
  47. protected void onBindView(View view) {
  48. super.onBindView(view);
  49. if (view instanceof ViewGroup) {
  50. findSwitch((ViewGroup) view);
  51. }
  52. }
  53. private void findSwitch(ViewGroup viewGroup) {
  54. ColorStateList thumbColorStateList = null;
  55. ColorStateList trackColorStateList = null;
  56. for (int i = 0; i < viewGroup.getChildCount(); i++) {
  57. View child = viewGroup.getChildAt(i);
  58. if (child instanceof Switch) {
  59. Switch switchView = (Switch) child;
  60. if(thumbColorStateList == null && trackColorStateList == null) {
  61. int color = ThemeUtils.primaryAccentColor(getContext());
  62. int trackColor = Color.argb(77, Color.red(color), Color.green(color), Color.blue(color));
  63. int trackColorUnchecked = getContext().getResources().getColor(R.color.switch_track_color_unchecked);
  64. thumbColorStateList = new ColorStateList(
  65. new int[][]{new int[]{android.R.attr.state_checked}, new int[]{}},
  66. new int[]{color, Color.WHITE});
  67. trackColorStateList = new ColorStateList(
  68. new int[][]{new int[]{android.R.attr.state_checked},
  69. new int[]{}},
  70. new int[]{trackColor, trackColorUnchecked});
  71. }
  72. // setting the thumb color
  73. DrawableCompat.setTintList(switchView.getThumbDrawable(), thumbColorStateList);
  74. // setting the track color
  75. DrawableCompat.setTintList(switchView.getTrackDrawable(), trackColorStateList);
  76. break;
  77. } else if (child instanceof ViewGroup) {
  78. findSwitch((ViewGroup) child);
  79. }
  80. }
  81. }
  82. }