ThemeButtonUtils.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.Context;
  25. import android.content.res.ColorStateList;
  26. import android.graphics.Color;
  27. import android.graphics.PorterDuff;
  28. import android.widget.Button;
  29. import android.widget.ImageButton;
  30. import com.owncloud.android.R;
  31. import androidx.annotation.ColorInt;
  32. import androidx.annotation.Nullable;
  33. import androidx.core.content.ContextCompat;
  34. /**
  35. * Utility class with methods for client side button theming.
  36. */
  37. public final class ThemeButtonUtils {
  38. /**
  39. * sets the tinting of the given ImageButton's icon to color_accent.
  40. *
  41. * @param imageButton the image button who's icon should be colored
  42. */
  43. public static void colorImageButton(ImageButton imageButton, @ColorInt int color) {
  44. if (imageButton != null) {
  45. imageButton.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
  46. }
  47. }
  48. public static void colorPrimaryButton(Button button, Context context) {
  49. int primaryColor = ThemeColorUtils.primaryColor(null, true, false, context);
  50. int fontColor = ThemeColorUtils.fontColor(context, false);
  51. button.setBackgroundColor(primaryColor);
  52. if (Color.BLACK == primaryColor) {
  53. button.setTextColor(Color.WHITE);
  54. } else if (Color.WHITE == primaryColor) {
  55. button.setTextColor(Color.BLACK);
  56. } else {
  57. button.setTextColor(fontColor);
  58. }
  59. }
  60. /**
  61. * theme buttons based on accent color.
  62. *
  63. * @param buttons borderless buttons to be themed
  64. */
  65. public static void themeBorderlessButton(@Nullable Button... buttons) {
  66. if (buttons == null || buttons.length < 1) {
  67. return;
  68. }
  69. themeBorderlessButton(ThemeColorUtils.primaryAccentColor(buttons[0].getContext()), buttons);
  70. }
  71. /**
  72. * theme buttons based on given color.
  73. *
  74. * @param color theme color
  75. * @param buttons borderless buttons to be themed
  76. */
  77. public static void themeBorderlessButton(int color, @Nullable Button... buttons) {
  78. if (buttons == null || buttons.length < 1) {
  79. return;
  80. }
  81. Context context = buttons[0].getContext();
  82. int disabledColor = ContextCompat.getColor(context, R.color.disabled_text);
  83. ColorStateList colorStateList = new ColorStateList(
  84. new int[][]{
  85. new int[]{android.R.attr.state_enabled}, // enabled
  86. new int[]{-android.R.attr.state_enabled}, // disabled
  87. },
  88. new int[]{
  89. color,
  90. disabledColor
  91. }
  92. );
  93. for (Button button: buttons) {
  94. button.setTextColor(colorStateList);
  95. }
  96. }
  97. }