ThemeCheckableUtils.java 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * @author Andy Scherzinger
  6. * Copyright (C) 2017 Tobias Kaminsky
  7. * Copyright (C) 2017 Nextcloud GmbH
  8. * Copyright (C) 2018 Andy Scherzinger
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. package com.owncloud.android.utils.theme;
  24. import android.content.res.ColorStateList;
  25. import android.graphics.Color;
  26. import com.owncloud.android.MainApp;
  27. import com.owncloud.android.R;
  28. import androidx.appcompat.widget.AppCompatCheckBox;
  29. import androidx.appcompat.widget.SwitchCompat;
  30. import androidx.core.graphics.drawable.DrawableCompat;
  31. import androidx.core.widget.CompoundButtonCompat;
  32. /**
  33. * Utility class with methods for client side checkable theming.
  34. */
  35. public final class ThemeCheckableUtils {
  36. public static void tintCheckbox(int color, AppCompatCheckBox... checkBoxes) {
  37. if (checkBoxes != null) {
  38. for (AppCompatCheckBox checkBox : checkBoxes) {
  39. CompoundButtonCompat.setButtonTintList(checkBox, new ColorStateList(
  40. new int[][]{
  41. new int[]{-android.R.attr.state_checked},
  42. new int[]{android.R.attr.state_checked},
  43. },
  44. new int[]{
  45. Color.GRAY,
  46. color
  47. }
  48. ));
  49. }
  50. }
  51. }
  52. public static void tintSwitch(SwitchCompat switchView, int color) {
  53. int trackColor = Color.argb(77, Color.red(color), Color.green(color), Color.blue(color));
  54. // setting the thumb color
  55. DrawableCompat.setTintList(switchView.getThumbDrawable(), new ColorStateList(
  56. new int[][]{new int[]{android.R.attr.state_checked}, new int[]{}},
  57. new int[]{color, Color.WHITE}));
  58. // setting the track color
  59. DrawableCompat.setTintList(switchView.getTrackDrawable(), new ColorStateList(
  60. new int[][]{new int[]{android.R.attr.state_checked}, new int[]{}},
  61. new int[]{trackColor, MainApp.getAppContext().getResources().getColor(R.color.switch_track_color_unchecked)}));
  62. }
  63. }