ThemeTextInputUtils.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.EditText;
  29. import com.google.android.material.textfield.TextInputEditText;
  30. import com.google.android.material.textfield.TextInputLayout;
  31. import com.owncloud.android.R;
  32. import androidx.core.content.ContextCompat;
  33. /**
  34. * Utility class with methods for client side text input theming.
  35. */
  36. public final class ThemeTextInputUtils {
  37. /**
  38. * Sets the color of the (containerized) text input TextInputLayout to {@code color} for hint text, box stroke and
  39. * highlight color.
  40. *
  41. * @param textInputLayout the TextInputLayout instance
  42. * @param textInputEditText the TextInputEditText child element
  43. * @param color the color to be used for the hint text and box stroke
  44. */
  45. public static void colorTextInput(TextInputLayout textInputLayout, TextInputEditText textInputEditText, int color) {
  46. textInputEditText.setHighlightColor(color);
  47. colorTextInputLayout(textInputLayout, color);
  48. }
  49. /**
  50. * Sets the color of the TextInputLayout to {@code color} for hint text and box stroke.
  51. *
  52. * @param textInputLayout the TextInputLayout instance
  53. * @param color the color to be used for the hint text and box stroke
  54. */
  55. private static void colorTextInputLayout(TextInputLayout textInputLayout, int color) {
  56. textInputLayout.setBoxStrokeColor(color);
  57. textInputLayout.setDefaultHintTextColor(new ColorStateList(
  58. new int[][]{
  59. new int[]{-android.R.attr.state_focused},
  60. new int[]{android.R.attr.state_focused},
  61. },
  62. new int[]{
  63. Color.GRAY,
  64. color
  65. }
  66. ));
  67. }
  68. public static void themeEditText(Context context, EditText editText, boolean themedBackground) {
  69. if (editText == null) {
  70. return;
  71. }
  72. int color = ContextCompat.getColor(context, R.color.text_color);
  73. if (themedBackground) {
  74. if (ThemeColorUtils.darkTheme(context)) {
  75. color = ContextCompat.getColor(context, R.color.themed_fg);
  76. } else {
  77. color = ContextCompat.getColor(context, R.color.themed_fg_inverse);
  78. }
  79. }
  80. setEditTextColor(context, editText, color);
  81. }
  82. public static void setEditTextColor(Context context, EditText editText, int color) {
  83. editText.setTextColor(color);
  84. editText.setHighlightColor(context.getResources().getColor(R.color.fg_contrast));
  85. }
  86. public static void colorEditText(EditText editText, int color) {
  87. if (editText != null) {
  88. editText.setTextColor(color);
  89. editText.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
  90. }
  91. }
  92. }