make filled buttons Material 3

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
This commit is contained in:
Andy Scherzinger 2022-07-30 01:03:47 +02:00
parent 4d93a2099f
commit c10a0a7c65
No known key found for this signature in database
GPG Key ID: 6CADC7E3523C308B
6 changed files with 47 additions and 14 deletions

View File

@ -106,7 +106,7 @@ public class TakePhotoActivity extends AppCompatActivity {
setContentView(binding.getRoot()); setContentView(binding.getRoot());
viewThemeUtils.themeFAB(binding.takePhoto); viewThemeUtils.themeFAB(binding.takePhoto);
viewThemeUtils.colorMaterialButtonBackground(binding.send); viewThemeUtils.colorMaterialButtonPrimaryFilled(binding.send);
cameraProviderFuture = ProcessCameraProvider.getInstance(this); cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(() -> { cameraProviderFuture.addListener(() -> {

View File

@ -108,7 +108,7 @@ class PollCreateDialogFragment : DialogFragment(), PollCreateOptionsItemListener
viewThemeUtils.colorMaterialButtonText(binding.pollAddOptionsItem) viewThemeUtils.colorMaterialButtonText(binding.pollAddOptionsItem)
// TODO button also needs a disabled state handling for colors // TODO button also needs a disabled state handling for colors
viewThemeUtils.colorMaterialButtonText(binding.pollDismiss) viewThemeUtils.colorMaterialButtonText(binding.pollDismiss)
viewThemeUtils.colorMaterialButtonBackground(binding.pollCreateButton) viewThemeUtils.colorMaterialButtonPrimaryFilled(binding.pollCreateButton)
viewThemeUtils.themeCheckbox(binding.pollPrivatePollCheckbox) viewThemeUtils.themeCheckbox(binding.pollPrivatePollCheckbox)
viewThemeUtils.themeCheckbox(binding.pollMultipleAnswersCheckbox) viewThemeUtils.themeCheckbox(binding.pollMultipleAnswersCheckbox)

View File

@ -98,7 +98,7 @@ class PollResultsFragment : Fragment(), PollResultItemClickListener {
} }
private fun themeDialog() { private fun themeDialog() {
viewThemeUtils.colorMaterialButtonBackground(binding.editVoteButton) viewThemeUtils.colorMaterialButtonPrimaryFilled(binding.editVoteButton)
viewThemeUtils.colorMaterialButtonText(binding.pollResultsEndPollButton) viewThemeUtils.colorMaterialButtonText(binding.pollResultsEndPollButton)
} }

View File

@ -126,7 +126,7 @@ class PollVoteFragment : Fragment() {
} }
private fun themeDialog() { private fun themeDialog() {
viewThemeUtils.colorMaterialButtonBackground(binding.pollVoteSubmitButton) viewThemeUtils.colorMaterialButtonPrimaryFilled(binding.pollVoteSubmitButton)
viewThemeUtils.colorMaterialButtonText(binding.pollVoteEndPollButton) viewThemeUtils.colorMaterialButtonText(binding.pollVoteEndPollButton)
viewThemeUtils.colorMaterialButtonText(binding.pollVoteEditDismiss) viewThemeUtils.colorMaterialButtonText(binding.pollVoteEditDismiss)
} }

View File

@ -242,7 +242,7 @@ class SetStatusDialogFragment :
} }
viewThemeUtils.colorMaterialButtonText(binding.clearStatus) viewThemeUtils.colorMaterialButtonText(binding.clearStatus)
viewThemeUtils.colorMaterialButtonBackground(binding.setStatus) viewThemeUtils.colorMaterialButtonPrimaryFilled(binding.setStatus)
binding.customStatusInput.highlightColor = resources.getColor(R.color.colorPrimary) binding.customStatusInput.highlightColor = resources.getColor(R.color.colorPrimary)

View File

@ -63,6 +63,7 @@ import com.yarolegovich.mp.MaterialPreferenceCategory
import com.yarolegovich.mp.MaterialSwitchPreference import com.yarolegovich.mp.MaterialSwitchPreference
import scheme.Scheme import scheme.Scheme
import javax.inject.Inject import javax.inject.Inject
import kotlin.math.roundToInt
@Suppress("TooManyFunctions") @Suppress("TooManyFunctions")
class ViewThemeUtils @Inject constructor(private val theme: ServerTheme, private val colorUtil: ColorUtil) { class ViewThemeUtils @Inject constructor(private val theme: ServerTheme, private val colorUtil: ColorUtil) {
@ -229,21 +230,42 @@ class ViewThemeUtils @Inject constructor(private val theme: ServerTheme, private
} }
} }
fun colorMaterialButtonBackground(button: MaterialButton) { fun colorMaterialButtonPrimaryFilled(button: MaterialButton) {
withElementColor(button) { color -> withScheme(button) { scheme ->
button.setBackgroundColor(color) button.backgroundTintList =
ColorStateList(
val disabledColor = ContextCompat.getColor(button.context, R.color.disabled_text)
val colorStateList = ColorStateList(
arrayOf( arrayOf(
intArrayOf(android.R.attr.state_enabled), intArrayOf(android.R.attr.state_enabled),
intArrayOf(-android.R.attr.state_enabled) intArrayOf(-android.R.attr.state_enabled)
), ),
intArrayOf(theme.colorText, disabledColor) intArrayOf(
scheme.primary,
calculateDisabledColor(scheme.primary, SURFACE_OPACITY_BUTTON_DISABLED)
)
) )
button.setTextColor(colorStateList) button.setTextColor(
button.iconTint = colorStateList ColorStateList(
arrayOf(
intArrayOf(android.R.attr.state_enabled),
intArrayOf(-android.R.attr.state_enabled)
),
intArrayOf(
scheme.onPrimary,
calculateDisabledColor(scheme.onPrimary, ON_SURFACE_OPACITY_BUTTON_DISABLED)
)
)
)
button.iconTint = ColorStateList(
arrayOf(
intArrayOf(android.R.attr.state_enabled),
intArrayOf(-android.R.attr.state_enabled)
),
intArrayOf(
scheme.onPrimary,
calculateDisabledColor(scheme.onPrimary, ON_SURFACE_OPACITY_BUTTON_DISABLED)
)
)
} }
} }
@ -435,6 +457,15 @@ class ViewThemeUtils @Inject constructor(private val theme: ServerTheme, private
} }
} }
private fun calculateDisabledColor(color: Int, opacity: Float): Int {
return Color.argb(
(Color.alpha(color) * opacity).roundToInt(),
Color.red(color),
Color.green(color),
Color.blue(color)
)
}
companion object { companion object {
private val THEMEABLE_PLACEHOLDER_IDS = listOf( private val THEMEABLE_PLACEHOLDER_IDS = listOf(
R.drawable.ic_mimetype_package_x_generic, R.drawable.ic_mimetype_package_x_generic,
@ -443,5 +474,7 @@ class ViewThemeUtils @Inject constructor(private val theme: ServerTheme, private
private const val SWITCHCOMPAT_TRACK_ALPHA: Int = 77 private const val SWITCHCOMPAT_TRACK_ALPHA: Int = 77
private const val PROGRESS_LIGHTNESS_LIGHT_THEME: Float = 0.76f private const val PROGRESS_LIGHTNESS_LIGHT_THEME: Float = 0.76f
private const val PROGRESS_LIGHTNESS_DARK_THEME: Float = 0.28f private const val PROGRESS_LIGHTNESS_DARK_THEME: Float = 0.28f
private const val SURFACE_OPACITY_BUTTON_DISABLED: Float = 0.12f
private const val ON_SURFACE_OPACITY_BUTTON_DISABLED: Float = 0.38f
} }
} }