I have a date picker dialog whose theme is this
<style name="DatePickerTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">@color/that_blue_color</item>
</style>
I made this custom theme because i wanted to change the background color of datePickerDialog. I managed to change the color of background ,picker circle and text color of buttons as well. But i now want to change the text color of the CANCEL
button and leave the text color of OK
button unchanged. How can i do it?
You can get the Button
from Dialog
and modify it attributes using getButton()
. See the example below. Get the button after calling .show()
other wise it will give a null
.
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(ConstarintsActivity.this,
(view, year, monthOfYear, dayOfMonth) -> {
}, mYear, mMonth, mDay);
datePickerDialog.show();
datePickerDialog.getButton(DatePickerDialog.BUTTON_NEGATIVE).setTextColor(Color.GREEN);
You can make a different style for CANCEL
button like below.
<style name="DatePickerTheme" parent="Theme.MaterialComponents.Light.Dialog.Alert">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:buttonBarNegativeButtonStyle">@style/DatePickerTheme.ButtonBarNegativeButtonStyle</item>
</style>
<style name="DatePickerTheme.ButtonBarNegativeButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">@android:color/holo_red_light</item>
</style>
dialog.setOnShowListener(dialog -> {
int positiveColor = ContextCompat.getColor(view.getContext(), R.color.default_blue_color);
int negativeColor = ContextCompat.getColor(view.getContext(), R.color.font_hint);
dialog.getButton(DatePickerDialog.BUTTON_POSITIVE).setTextColor(positiveColor);
dialog.getButton(DatePickerDialog.BUTTON_NEGATIVE).setTextColor(negativeColor);
});
dialog.show();
I set button color in dialog's showListener
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With