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());
viewThemeUtils.themeFAB(binding.takePhoto);
viewThemeUtils.colorMaterialButtonBackground(binding.send);
viewThemeUtils.colorMaterialButtonPrimaryFilled(binding.send);
cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(() -> {

View File

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

View File

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

View File

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

View File

@ -242,7 +242,7 @@ class SetStatusDialogFragment :
}
viewThemeUtils.colorMaterialButtonText(binding.clearStatus)
viewThemeUtils.colorMaterialButtonBackground(binding.setStatus)
viewThemeUtils.colorMaterialButtonPrimaryFilled(binding.setStatus)
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 scheme.Scheme
import javax.inject.Inject
import kotlin.math.roundToInt
@Suppress("TooManyFunctions")
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) {
withElementColor(button) { color ->
button.setBackgroundColor(color)
fun colorMaterialButtonPrimaryFilled(button: MaterialButton) {
withScheme(button) { scheme ->
button.backgroundTintList =
ColorStateList(
arrayOf(
intArrayOf(android.R.attr.state_enabled),
intArrayOf(-android.R.attr.state_enabled)
),
intArrayOf(
scheme.primary,
calculateDisabledColor(scheme.primary, SURFACE_OPACITY_BUTTON_DISABLED)
)
)
val disabledColor = ContextCompat.getColor(button.context, R.color.disabled_text)
val colorStateList = ColorStateList(
button.setTextColor(
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(theme.colorText, disabledColor)
intArrayOf(
scheme.onPrimary,
calculateDisabledColor(scheme.onPrimary, ON_SURFACE_OPACITY_BUTTON_DISABLED)
)
)
button.setTextColor(colorStateList)
button.iconTint = colorStateList
}
}
@ -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 {
private val THEMEABLE_PLACEHOLDER_IDS = listOf(
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 PROGRESS_LIGHTNESS_LIGHT_THEME: Float = 0.76f
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
}
}