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
.
Just make the different style for CANCEL
button and place that style into your alertdialog's parent theme like below:
<style name="DatePickerTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">@color/that_blue_color</item>
<item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
</style>
<style name="NegativeButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">#f00</item>
</style>
I managed to do it like this
datePickerDialog.show();
datePickerDialog.
getButton(DatePickerDialog.BUTTON_NEGATIVE).setTextColor(Color.G
RAY);
只需在你的styles.xml 中添加以下内容
<style name="mdtp_ActionButton.Text" parent="Widget.MaterialComponents.Button.TextButton.Dialog"/>
<style name="DatePickerTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/that_blue_color</item>
</style>
In AndroidManifest.xml file add this line in particulate activity android:theme="@style/DatePickerTheme".
OR
Try this in the XML file:
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Date"
/>
Now in Java File:
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Initialize a new date picker dialog fragment
DialogFragment dFragment = new DatePickerFragment();
// Show the date picker dialog fragment
dFragment.show(getFragmentManager(), "Date Picker");
}
});
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
/*
Create a DatePickerDialog using Theme.
DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener listener,
int year, int monthOfYear, int dayOfMonth)
*/
// DatePickerDialog THEME_DEVICE_DEFAULT_LIGHT
DatePickerDialog dpd = new DatePickerDialog(getActivity(),
AlertDialog.THEME_DEVICE_DEFAULT_LIGHT,this,year,month,day);
// Return the DatePickerDialog
return dpd;
}
public void onDateSet(DatePicker view, int year, int month, int day){
// Do something with the chosen date
}
I hope it will help you..!
edt_dob.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Birthday_click = false;
Calendar now = Calendar.getInstance();
DatePickerDialog dpd = DatePickerDialog.newInstance((DatePickerDialog.OnDateSetListener) context,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
dpd.setMaxDate(Calendar.getInstance());
dpd.show(getFragmentManager(), "Datepickerdialog");
dpd.dismissOnPause(true);
dpd.setAccentColor(000000);
dpd.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
edt_dob.setText(null);
edt_dob.setInputType(InputType.TYPE_NULL);
int day = dayOfMonth;
int yy = year;
int month = ++monthOfYear;
String str_day = day < 10 ? "0" + day : "" + day;
String str_month = month < 10 ? "0" + month : "" + month;
String date_select = yy + "-" + str_month + "-" + str_day;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = null;
try {
date1 = format.parse(date_select);
} catch (ParseException e) {
e.printStackTrace();
}
String date = format.format(date1);
if (date.endsWith("01") && !date.endsWith("11"))
format = new SimpleDateFormat("MMMM d'st', yyyy");
else if (date.endsWith("02") && !date.endsWith("12"))
format = new SimpleDateFormat("MMMM d'nd', yyyy");
else if (date.endsWith("03") && !date.endsWith("13"))
format = new SimpleDateFormat("MMMM d'rd', yyyy");
else
format = new SimpleDateFormat("MMMM d'th', yyyy");
String yourDate = format.format(date1);
edt_dob.setText(yourDate);
}
});
}
});
Here's my solution.
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- ... -->
<item name="android:datePickerDialogTheme">@style/SpinnerDatePickerStyle</item>
</style>
<style name="SpinnerDatePickerStyle" parent="android:Theme.Material.Light.Dialog">
<item name="android:datePickerStyle">@style/myDatePickerStyle</item>
<item name="android:buttonBarPositiveButtonStyle">@style/DatePickerButtonStyle</item>
<item name="android:buttonBarNegativeButtonStyle">@style/DatePickerButtonStyle</item>
</style>
<style name="myDatePickerStyle" parent="android:Widget.Material.DatePicker">
<item name="android:datePickerMode">spinner</item>
</style>
<style name="DatePickerButtonStyle" parent="android:Widget.Material.Button.Borderless">
<item name="android:textColor">@color/black</item>
</style>
it's not buttonBarPositiveButtonStyle
but android:buttonBarPositiveButtonStyle
with android:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.