How to change MaterialTimePicker's buttons text color independently?
Solution 1:
I think i found a method.
From the source code we can see that these two buttons (OK and CANCEL) are using borderlessButtonStyle
:
<Button
android:id="@+id/material_timepicker_cancel_button"
style="?attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginEnd="8dp"
android:minWidth="72dp"
android:text="@android:string/cancel"
app:layout_constraintEnd_toStartOf="@id/material_timepicker_ok_button"
app:layout_constraintTop_toTopOf="@id/material_timepicker_mode_button" />
So we can simply override the borderlessButtonStyle
style in the TimePicker style to change the colors. Like this :
themes.xml :
<style name="ThemeOverlay.App.TimePicker" parent="ThemeOverlay.MaterialComponents.TimePicker">
<item name="borderlessButtonStyle">@style/borderlessButtonStyle</item>
</style>
<style name="borderlessButtonStyle" parent="Widget.MaterialComponents.Button.TextButton">
<item name="android:textColor">@color/white</item>
</style>
then in the end add this line in your main app's theme :
<style name="Theme.StackOverflow" parent="Theme.MaterialComponents.Light.NoActionBar">
...
<item name="materialTimePickerTheme">@style/ThemeOverlay.App.TimePicker</item>
...
</style>
This will work. Please feel free to improve my answer .