Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the text color of one of the buttons in DatePickerDialog?

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?

like image 813
Dishonered Avatar asked Jun 09 '18 07:06

Dishonered


3 Answers

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);
like image 190
ADM Avatar answered Sep 30 '22 07:09

ADM


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>
like image 41
Shehan Rangana Avatar answered Sep 30 '22 06:09

Shehan Rangana


        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.

like image 34
aotian16 Avatar answered Sep 30 '22 05:09

aotian16