Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the color of DatePicker Dialog

Tags:

android

I like to change the color of DatePicker Dialog. I load the dialog as

@SuppressLint("NewApi")
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

       @SuppressLint("NewApi")
    @Override
       public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current date as the default date in the picker
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);

            // Create a new instance of DatePickerDialog and return it
            return new DatePickerDialog(getActivity(), this, year, month, day);
      }

      public void onDateSet(DatePicker view, int year, int month, int day) {
           // Do something with the date chosen by the user


      }


}

@SuppressLint("NewApi")
public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getFragmentManager(), "datePicker");//show(getSupportFragmentManager(), "datePicker");
}

When it load the dialog, it is white color background. How can I change to display color as show in the second picture? Thanksenter image description hereenter image description here

like image 859
Bryanyan Avatar asked Jan 14 '23 17:01

Bryanyan


1 Answers

As per your query you have to create custom dialog theme and set in custom_theme.xml Now simply set as per your API version.

First, in values add a themes.xml like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyAppTheme" parent="@android:style/Theme.Light.NoTitleBar">
        <!-- Any customizations for your app running on pre-3.0 devices here -->
    </style>
</resources> 

Then, create a directory with the name "values-v11" (Android 3.0+ ) in the res directory and put a themes.xml like this

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyAppTheme" parent="@android:style/Theme.Holo.Light">
        <!-- Any customizations for your app running on 3.0+ devices here -->
    </style>
</resources>

Finally, create a directory with the name "values-v14" (Android 4.0+) in the res directory and create a themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyAppTheme" parent="@android:style/Theme.DeviceDefault.Light.NoActionBar">
        <!-- Any customizations for your app running on 4.0+ devices here -->
    </style>
</resources>  

For More Details check out the link and follow it.

Hope you get some idea form them and solve your issue.

Good Luck.

like image 121
Chintan Khetiya Avatar answered Jan 24 '23 04:01

Chintan Khetiya